Skip to content

Commit 7298e27

Browse files
committed
[ISSUE-5161] moved the safeguard to be the first op, added tests
1 parent eb1a011 commit 7298e27

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

trace/noop.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ var _ Tracer = noopTracer{}
3838
// Start carries forward a non-recording Span, if one is present in the context, otherwise it
3939
// creates a no-op Span.
4040
func (t noopTracer) Start(ctx context.Context, name string, _ ...SpanStartOption) (context.Context, Span) {
41+
if ctx == nil {
42+
// Prevent trace.ContextWithSpan from panicking.
43+
ctx = context.Background()
44+
}
45+
4146
span := SpanFromContext(ctx)
4247
if _, ok := span.(nonRecordingSpan); !ok {
4348
// span is likely already a noopSpan, but let's be sure
4449
span = noopSpanInstance
4550
}
4651

47-
if ctx == nil {
48-
// Prevent trace.ContextWithSpan from panicking.
49-
ctx = context.Background()
50-
}
51-
5252
return ContextWithSpan(ctx, span), span
5353
}
5454

trace/noop/noop.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ type Tracer struct{ embedded.Tracer }
5252
// span context. If the span context in ctx is for a non-recording span, that
5353
// span instance will be returned directly.
5454
func (t Tracer) Start(ctx context.Context, _ string, _ ...trace.SpanStartOption) (context.Context, trace.Span) {
55-
span := trace.SpanFromContext(ctx)
56-
5755
if ctx == nil {
5856
// Prevent trace.ContextWithSpan from panicking.
5957
ctx = context.Background()
6058
}
6159

60+
span := trace.SpanFromContext(ctx)
61+
6262
// If the parent context contains a non-zero span context, that span
6363
// context needs to be returned as a non-recording span
6464
// (https://github.com/open-telemetry/opentelemetry-specification/blob/3a1dde966a4ce87cce5adf464359fe369741bbea/specification/trace/api.md#behavior-of-the-api-in-the-absence-of-an-installed-sdk).

trace/noop/noop_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ func TestTracerStartPropagatesSpanContext(t *testing.T) {
101101
assert.False(t, span.IsRecording(), "recording span returned")
102102
}
103103

104+
func TestStartSpanWithNilContext(t *testing.T) {
105+
tp := NewTracerProvider()
106+
tr := tp.Tracer("NoPanic")
107+
108+
// nolint:staticcheck // no nil context, but that's the point of the test.
109+
assert.NotPanics(t, func() { tr.Start(nil, "should-not-panic") })
110+
}
111+
104112
type recordingSpan struct{ Span }
105113

106114
func (recordingSpan) IsRecording() bool { return true }

trace/noop_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package trace
66
import (
77
"context"
88
"testing"
9+
10+
"github.com/stretchr/testify/assert"
911
)
1012

1113
func TestNewNoopTracerProvider(t *testing.T) {
@@ -78,3 +80,11 @@ func TestNonRecordingSpanTracerStart(t *testing.T) {
7880
t.Errorf("SpanContext not carried by nonRecordingSpan. got %#v, want %#v", got, want)
7981
}
8082
}
83+
84+
func TestStartSpanWithNilContext(t *testing.T) {
85+
tp := NewNoopTracerProvider()
86+
tr := tp.Tracer("NoPanic")
87+
88+
// nolint:staticcheck // no nil context, but that's the point of the test.
89+
assert.NotPanics(t, func() { tr.Start(nil, "should-not-panic") })
90+
}

0 commit comments

Comments
 (0)