Skip to content

Commit 5f9f780

Browse files
committed
Use the noop provider instead of the disabled exporter
1 parent 2296e57 commit 5f9f780

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

exporters/trace/jaeger/jaeger.go

+19-22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"go.opentelemetry.io/otel/api/global"
2525
"go.opentelemetry.io/otel/api/kv"
2626
"go.opentelemetry.io/otel/api/kv/value"
27+
apitrace "go.opentelemetry.io/otel/api/trace"
2728
gen "go.opentelemetry.io/otel/exporters/trace/jaeger/internal/gen-go/jaeger"
2829
export "go.opentelemetry.io/otel/sdk/export/trace"
2930
sdktrace "go.opentelemetry.io/otel/sdk/trace"
@@ -38,7 +39,7 @@ type options struct {
3839
// Process contains the information about the exporting process.
3940
Process Process
4041

41-
//BufferMaxCount defines the total number of traces that can be buffered in memory
42+
// BufferMaxCount defines the total number of traces that can be buffered in memory
4243
BufferMaxCount int
4344

4445
Config *sdktrace.Config
@@ -57,7 +58,7 @@ func WithProcess(process Process) Option {
5758
}
5859
}
5960

60-
//WithBufferMaxCount defines the total number of traces that can be buffered in memory
61+
// WithBufferMaxCount defines the total number of traces that can be buffered in memory
6162
func WithBufferMaxCount(bufferMaxCount int) Option {
6263
return func(o *options) {
6364
o.BufferMaxCount = bufferMaxCount
@@ -87,22 +88,19 @@ func WithDisabled(disabled bool) Option {
8788

8889
// NewRawExporter returns a trace.Exporter implementation that exports
8990
// the collected spans to Jaeger.
91+
//
92+
// It will IGNORE Disabled option.
9093
func NewRawExporter(endpointOption EndpointOption, opts ...Option) (*Exporter, error) {
94+
uploader, err := endpointOption()
95+
if err != nil {
96+
return nil, err
97+
}
98+
9199
o := options{}
92100
opts = append(opts, WithDisabledFromEnv(), WithProcessFromEnv())
93101
for _, opt := range opts {
94102
opt(&o)
95103
}
96-
if o.Disabled {
97-
return &Exporter{
98-
o: o,
99-
}, nil
100-
}
101-
102-
uploader, err := endpointOption()
103-
if err != nil {
104-
return nil, err
105-
}
106104

107105
service := o.Process.ServiceName
108106
if service == "" {
@@ -142,7 +140,15 @@ func NewRawExporter(endpointOption EndpointOption, opts ...Option) (*Exporter, e
142140

143141
// NewExportPipeline sets up a complete export pipeline
144142
// with the recommended setup for trace provider
145-
func NewExportPipeline(endpointOption EndpointOption, opts ...Option) (*sdktrace.Provider, func(), error) {
143+
func NewExportPipeline(endpointOption EndpointOption, opts ...Option) (apitrace.Provider, func(), error) {
144+
o := options{}
145+
for _, opt := range opts {
146+
opt(&o)
147+
}
148+
if o.Disabled {
149+
return &apitrace.NoopProvider{}, func() {}, nil
150+
}
151+
146152
exporter, err := NewRawExporter(endpointOption, opts...)
147153
if err != nil {
148154
return nil, nil, err
@@ -184,9 +190,6 @@ var _ export.SpanSyncer = (*Exporter)(nil)
184190

185191
// ExportSpan exports a SpanData to Jaeger.
186192
func (e *Exporter) ExportSpan(ctx context.Context, d *export.SpanData) {
187-
if e.o.Disabled {
188-
return
189-
}
190193
_ = e.bundler.Add(spanDataToThrift(d), 1)
191194
// TODO(jbd): Handle oversized bundlers.
192195
}
@@ -344,16 +347,10 @@ func getBoolTag(k string, b bool) *gen.Tag {
344347
//
345348
// This is useful if your program is ending and you do not want to lose recent spans.
346349
func (e *Exporter) Flush() {
347-
if e.o.Disabled {
348-
return
349-
}
350350
e.bundler.Flush()
351351
}
352352

353353
func (e *Exporter) upload(spans []*gen.Span) error {
354-
if e.o.Disabled {
355-
return nil
356-
}
357354
batch := &gen.Batch{
358355
Spans: spans,
359356
Process: e.process,

exporters/trace/jaeger/jaeger_test.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -317,21 +317,12 @@ func Test_spanDataToThrift(t *testing.T) {
317317
}
318318
}
319319

320-
func TestNewRawExporterWithDisabled(t *testing.T) {
321-
const (
322-
collectorEndpoint = "http://localhost"
323-
)
324-
// Create Jaeger Exporter
325-
exp, err := NewRawExporter(
326-
WithCollectorEndpoint(collectorEndpoint),
320+
func TestNewExporterPipelineWithDisabled(t *testing.T) {
321+
tp, fn, err := NewExportPipeline(
322+
WithCollectorEndpoint("http://localhost:14268/api/traces"),
327323
WithDisabled(true),
328324
)
329-
325+
defer fn()
330326
assert.NoError(t, err)
331-
assert.EqualValues(t, true, exp.o.Disabled)
332-
333-
// Ensure we can still normally invoke function
334-
assert.NoError(t, exp.upload([]*gen.Span{}))
335-
exp.ExportSpan(context.Background(), &export.SpanData{})
336-
exp.Flush()
327+
assert.IsType(t, &apitrace.NoopProvider{}, tp)
337328
}

0 commit comments

Comments
 (0)