Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-5161] Fix panics if a nil context is provided to the .Start() method #5162

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/global/trace.go
Expand Up @@ -136,6 +136,11 @@ func (t *tracer) setDelegate(provider trace.TracerProvider) {
// Start implements trace.Tracer by forwarding the call to t.delegate if
// set, otherwise it forwards the call to a NoopTracer.
func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
if ctx == nil {
// Prevent trace.ContextWithSpan from panicking.
ctx = context.Background()
}

delegate := t.delegate.Load()
if delegate != nil {
return delegate.(trace.Tracer).Start(ctx, name, opts...)
Expand Down
6 changes: 6 additions & 0 deletions trace/noop.go
Expand Up @@ -43,6 +43,12 @@ func (t noopTracer) Start(ctx context.Context, name string, _ ...SpanStartOption
// span is likely already a noopSpan, but let's be sure
span = noopSpanInstance
}

if ctx == nil {
xxbtwxx marked this conversation as resolved.
Show resolved Hide resolved
// Prevent trace.ContextWithSpan from panicking.
ctx = context.Background()
}

return ContextWithSpan(ctx, span), span
}

Expand Down
5 changes: 5 additions & 0 deletions trace/noop/noop.go
Expand Up @@ -54,6 +54,11 @@ type Tracer struct{ embedded.Tracer }
func (t Tracer) Start(ctx context.Context, _ string, _ ...trace.SpanStartOption) (context.Context, trace.Span) {
span := trace.SpanFromContext(ctx)

if ctx == nil {
xxbtwxx marked this conversation as resolved.
Show resolved Hide resolved
// Prevent trace.ContextWithSpan from panicking.
ctx = context.Background()
}

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