Skip to content

Commit a2c75c6

Browse files
authored
Unexport NoopXXX trace types (open-telemetry#1134)
* Unexport NoopXXX trace types The change unexports the noop implementations and provide the NoopProvider function for user to construct noop providers. Users can access noop tracer and noop spans by using the provider. This change removes the types users should never be directly using from the package. It improves the usability of the API by reducing the API surface to half and helping the user to focus on the canonical APIs. Fixes open-telemetry#1133 * Provide noop tracer and span for internal use * Remove obsolete doc * Use noop span instead of nil * Fix the broken build
1 parent f1dad21 commit a2c75c6

File tree

12 files changed

+93
-44
lines changed

12 files changed

+93
-44
lines changed

api/global/internal/trace.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"sync"
3737

3838
"go.opentelemetry.io/otel/api/trace"
39+
"go.opentelemetry.io/otel/internal/trace/noop"
3940
)
4041

4142
// tracerProvider is a placeholder for a configured SDK Provider.
@@ -120,5 +121,5 @@ func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanOptio
120121
if t.delegate != nil {
121122
return t.delegate.Start(ctx, name, opts...)
122123
}
123-
return trace.NoopTracer{}.Start(ctx, name, opts...)
124+
return noop.Tracer.Start(ctx, name, opts...)
124125
}

api/global/trace_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@ import (
1919

2020
"go.opentelemetry.io/otel/api/global"
2121
"go.opentelemetry.io/otel/api/trace"
22+
"go.opentelemetry.io/otel/internal/trace/noop"
2223
)
2324

2425
type testTracerProvider struct{}
2526

2627
var _ trace.Provider = &testTracerProvider{}
2728

2829
func (*testTracerProvider) Tracer(_ string, _ ...trace.TracerOption) trace.Tracer {
29-
return &trace.NoopTracer{}
30+
return noop.Tracer
3031
}
3132

3233
func TestMultipleGlobalTracerProvider(t *testing.T) {
3334
p1 := testTracerProvider{}
34-
p2 := trace.NoopProvider{}
35+
p2 := trace.NoopProvider()
3536
global.SetTracerProvider(&p1)
36-
global.SetTracerProvider(&p2)
37+
global.SetTracerProvider(p2)
3738

3839
got := global.TracerProvider()
39-
want := &p2
40+
want := p2
4041
if got != want {
4142
t.Fatalf("Provider: got %p, want %p\n", got, want)
4243
}

api/trace/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func SpanFromContext(ctx context.Context) Span {
3636
if span, has := ctx.Value(currentSpanKey).(Span); has {
3737
return span
3838
}
39-
return NoopSpan{}
39+
return noopSpan{}
4040
}
4141

4242
// ContextWithRemoteSpanContext creates a new context with a remote

api/trace/context_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import (
2121

2222
"go.opentelemetry.io/otel/api/trace"
2323
"go.opentelemetry.io/otel/codes"
24+
"go.opentelemetry.io/otel/internal/trace/noop"
2425
"go.opentelemetry.io/otel/label"
2526
)
2627

2728
func TestSetCurrentSpanOverridesPreviouslySetSpan(t *testing.T) {
28-
originalSpan := trace.NoopSpan{}
29-
expectedSpan := mockSpan{}
30-
3129
ctx := context.Background()
30+
originalSpan := noop.Span
31+
expectedSpan := mockSpan{}
3232

3333
ctx = trace.ContextWithSpan(ctx, originalSpan)
3434
ctx = trace.ContextWithSpan(ctx, expectedSpan)
@@ -47,7 +47,7 @@ func TestCurrentSpan(t *testing.T) {
4747
{
4848
name: "CurrentSpan() returns a NoopSpan{} from an empty context",
4949
ctx: context.Background(),
50-
want: trace.NoopSpan{},
50+
want: noop.Span,
5151
},
5252
{
5353
name: "CurrentSpan() returns current span if set",
@@ -110,7 +110,7 @@ func (mockSpan) RecordError(ctx context.Context, err error, opts ...trace.ErrorO
110110

111111
// Tracer returns noop implementation of Tracer.
112112
func (mockSpan) Tracer() trace.Tracer {
113-
return trace.NoopTracer{}
113+
return noop.Tracer
114114
}
115115

116116
// Event does nothing.

api/trace/noop_span.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -22,58 +22,58 @@ import (
2222
"go.opentelemetry.io/otel/label"
2323
)
2424

25-
type NoopSpan struct {
25+
type noopSpan struct {
2626
}
2727

28-
var _ Span = (*NoopSpan)(nil)
28+
var _ Span = noopSpan{}
2929

3030
// SpanContext returns an invalid span context.
31-
func (NoopSpan) SpanContext() SpanContext {
31+
func (noopSpan) SpanContext() SpanContext {
3232
return EmptySpanContext()
3333
}
3434

3535
// IsRecording always returns false for NoopSpan.
36-
func (NoopSpan) IsRecording() bool {
36+
func (noopSpan) IsRecording() bool {
3737
return false
3838
}
3939

4040
// SetStatus does nothing.
41-
func (NoopSpan) SetStatus(status codes.Code, msg string) {
41+
func (noopSpan) SetStatus(status codes.Code, msg string) {
4242
}
4343

4444
// SetError does nothing.
45-
func (NoopSpan) SetError(v bool) {
45+
func (noopSpan) SetError(v bool) {
4646
}
4747

4848
// SetAttributes does nothing.
49-
func (NoopSpan) SetAttributes(attributes ...label.KeyValue) {
49+
func (noopSpan) SetAttributes(attributes ...label.KeyValue) {
5050
}
5151

5252
// SetAttribute does nothing.
53-
func (NoopSpan) SetAttribute(k string, v interface{}) {
53+
func (noopSpan) SetAttribute(k string, v interface{}) {
5454
}
5555

5656
// End does nothing.
57-
func (NoopSpan) End(options ...SpanOption) {
57+
func (noopSpan) End(options ...SpanOption) {
5858
}
5959

6060
// RecordError does nothing.
61-
func (NoopSpan) RecordError(ctx context.Context, err error, opts ...ErrorOption) {
61+
func (noopSpan) RecordError(ctx context.Context, err error, opts ...ErrorOption) {
6262
}
6363

6464
// Tracer returns noop implementation of Tracer.
65-
func (NoopSpan) Tracer() Tracer {
66-
return NoopTracer{}
65+
func (noopSpan) Tracer() Tracer {
66+
return noopTracer{}
6767
}
6868

6969
// AddEvent does nothing.
70-
func (NoopSpan) AddEvent(ctx context.Context, name string, attrs ...label.KeyValue) {
70+
func (noopSpan) AddEvent(ctx context.Context, name string, attrs ...label.KeyValue) {
7171
}
7272

7373
// AddEventWithTimestamp does nothing.
74-
func (NoopSpan) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, name string, attrs ...label.KeyValue) {
74+
func (noopSpan) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, name string, attrs ...label.KeyValue) {
7575
}
7676

7777
// SetName does nothing.
78-
func (NoopSpan) SetName(name string) {
78+
func (noopSpan) SetName(name string) {
7979
}

api/trace/noop_trace.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import (
1818
"context"
1919
)
2020

21-
type NoopTracer struct{}
21+
type noopTracer struct{}
2222

23-
var _ Tracer = NoopTracer{}
23+
var _ Tracer = noopTracer{}
2424

2525
// Start starts a noop span.
26-
func (NoopTracer) Start(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span) {
27-
span := NoopSpan{}
26+
func (noopTracer) Start(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span) {
27+
span := noopSpan{}
2828
return ContextWithSpan(ctx, span), span
2929
}

api/trace/noop_trace_provider.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@
1414

1515
package trace
1616

17-
type NoopProvider struct{}
17+
type noopProvider struct{}
1818

19-
var _ Provider = NoopProvider{}
19+
var _ Provider = noopProvider{}
2020

2121
// Tracer returns noop implementation of Tracer.
22-
func (p NoopProvider) Tracer(_ string, _ ...TracerOption) Tracer {
23-
return NoopTracer{}
22+
func (p noopProvider) Tracer(_ string, _ ...TracerOption) Tracer {
23+
return noopTracer{}
24+
}
25+
26+
// NoopProvider returns a noop implementation of Provider.
27+
// The Tracer and Spans created from the noop provider will
28+
// also be noop.
29+
func NoopProvider() Provider {
30+
return noopProvider{}
2431
}

bridge/opentracing/bridge.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
otelpropagation "go.opentelemetry.io/otel/api/propagation"
3131
oteltrace "go.opentelemetry.io/otel/api/trace"
3232
"go.opentelemetry.io/otel/codes"
33+
"go.opentelemetry.io/otel/internal/trace/noop"
3334
otelparent "go.opentelemetry.io/otel/internal/trace/parent"
3435
"go.opentelemetry.io/otel/label"
3536

@@ -300,7 +301,7 @@ var _ ot.TracerContextWithSpanExtension = &BridgeTracer{}
300301
func NewBridgeTracer() *BridgeTracer {
301302
return &BridgeTracer{
302303
setTracer: bridgeSetTracer{
303-
otelTracer: oteltrace.NoopTracer{},
304+
otelTracer: noop.Tracer,
304305
},
305306
warningHandler: func(msg string) {},
306307
propagators: nil,
@@ -579,8 +580,7 @@ func otSpanReferenceTypeToString(refType ot.SpanReferenceType) string {
579580
// fakeSpan is just a holder of span context, nothing more. It's for
580581
// propagators, so they can get the span context from Go context.
581582
type fakeSpan struct {
582-
oteltrace.NoopSpan
583-
583+
oteltrace.Span
584584
sc oteltrace.SpanContext
585585
}
586586

@@ -609,7 +609,8 @@ func (t *BridgeTracer) Inject(sm ot.SpanContext, format interface{}, carrier int
609609
}
610610
header := http.Header(hhcarrier)
611611
fs := fakeSpan{
612-
sc: bridgeSC.otelSpanContext,
612+
Span: noop.Span,
613+
sc: bridgeSC.otelSpanContext,
613614
}
614615
ctx := oteltrace.ContextWithSpan(context.Background(), fs)
615616
ctx = otelcorrelation.ContextWithMap(ctx, bridgeSC.baggageItems)

exporters/trace/jaeger/jaeger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func NewExportPipeline(endpointOption EndpointOption, opts ...Option) (apitrace.
156156
opt(&o)
157157
}
158158
if o.Disabled {
159-
return &apitrace.NoopProvider{}, func() {}, nil
159+
return apitrace.NoopProvider(), func() {}, nil
160160
}
161161

162162
exporter, err := NewRawExporter(endpointOption, opts...)

exporters/trace/jaeger/jaeger_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestInstallNewPipeline(t *testing.T) {
6969
options: []Option{
7070
WithDisabled(true),
7171
},
72-
expectedProvider: &apitrace.NoopProvider{},
72+
expectedProvider: apitrace.NoopProvider(),
7373
},
7474
}
7575

@@ -108,7 +108,7 @@ func TestNewExportPipeline(t *testing.T) {
108108
options: []Option{
109109
WithDisabled(true),
110110
},
111-
expectedProviderType: &apitrace.NoopProvider{},
111+
expectedProviderType: apitrace.NoopProvider(),
112112
},
113113
{
114114
name: "always on",
@@ -173,7 +173,7 @@ func TestNewExportPipelineWithDisabledFromEnv(t *testing.T) {
173173
)
174174
defer fn()
175175
assert.NoError(t, err)
176-
assert.IsType(t, &apitrace.NoopProvider{}, tp)
176+
assert.IsType(t, apitrace.NoopProvider(), tp)
177177
}
178178

179179
func TestNewRawExporter(t *testing.T) {

internal/trace/noop/noop.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package noop provides noop tracing implementations for tracer and span.
16+
package noop
17+
18+
import (
19+
"context"
20+
21+
"go.opentelemetry.io/otel/api/trace"
22+
)
23+
24+
var (
25+
// Tracer is a noop tracer that starts noop spans.
26+
Tracer trace.Tracer
27+
28+
// Span is a noop Span.
29+
Span trace.Span
30+
)
31+
32+
func init() {
33+
Tracer = trace.NoopProvider().Tracer("")
34+
_, Span = Tracer.Start(context.Background(), "")
35+
}

propagators/b3_integration_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"go.opentelemetry.io/otel/api/propagation"
2525
"go.opentelemetry.io/otel/api/trace"
26+
"go.opentelemetry.io/otel/internal/trace/noop"
2627
"go.opentelemetry.io/otel/propagators"
2728
)
2829

@@ -64,7 +65,7 @@ func TestExtractB3(t *testing.T) {
6465
}
6566

6667
type testSpan struct {
67-
trace.NoopSpan
68+
trace.Span
6869
sc trace.SpanContext
6970
}
7071

@@ -94,7 +95,10 @@ func TestInjectB3(t *testing.T) {
9495
req, _ := http.NewRequest("GET", "http://example.com", nil)
9596
ctx := trace.ContextWithSpan(
9697
context.Background(),
97-
testSpan{sc: tt.sc},
98+
testSpan{
99+
Span: noop.Span,
100+
sc: tt.sc,
101+
},
98102
)
99103
propagator.Inject(ctx, req.Header)
100104

0 commit comments

Comments
 (0)