Skip to content

Commit eb1a011

Browse files
committed
[ISSUE-5161] Fix panics if a nil context is provided to the .Start() method
1 parent 82b49b4 commit eb1a011

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

internal/global/trace.go

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ func (t *tracer) setDelegate(provider trace.TracerProvider) {
136136
// Start implements trace.Tracer by forwarding the call to t.delegate if
137137
// set, otherwise it forwards the call to a NoopTracer.
138138
func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
139+
if ctx == nil {
140+
// Prevent trace.ContextWithSpan from panicking.
141+
ctx = context.Background()
142+
}
143+
139144
delegate := t.delegate.Load()
140145
if delegate != nil {
141146
return delegate.(trace.Tracer).Start(ctx, name, opts...)

trace/noop.go

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func (t noopTracer) Start(ctx context.Context, name string, _ ...SpanStartOption
4343
// span is likely already a noopSpan, but let's be sure
4444
span = noopSpanInstance
4545
}
46+
47+
if ctx == nil {
48+
// Prevent trace.ContextWithSpan from panicking.
49+
ctx = context.Background()
50+
}
51+
4652
return ContextWithSpan(ctx, span), span
4753
}
4854

trace/noop/noop.go

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ type Tracer struct{ embedded.Tracer }
5454
func (t Tracer) Start(ctx context.Context, _ string, _ ...trace.SpanStartOption) (context.Context, trace.Span) {
5555
span := trace.SpanFromContext(ctx)
5656

57+
if ctx == nil {
58+
// Prevent trace.ContextWithSpan from panicking.
59+
ctx = context.Background()
60+
}
61+
5762
// If the parent context contains a non-zero span context, that span
5863
// context needs to be returned as a non-recording span
5964
// (https://github.com/open-telemetry/opentelemetry-specification/blob/3a1dde966a4ce87cce5adf464359fe369741bbea/specification/trace/api.md#behavior-of-the-api-in-the-absence-of-an-installed-sdk).

0 commit comments

Comments
 (0)