Skip to content

Commit

Permalink
Update tracer to guard for a nil ctx (#3110)
Browse files Browse the repository at this point in the history
* Update tracer to guard for a nil ctx

Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
Co-authored-by: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
4 people committed Aug 26, 2022
1 parent 3810616 commit 55b49c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The exponential histogram mapping functions have been updated with
exact upper-inclusive boundary support following the [corresponding
specification change](https://github.com/open-telemetry/opentelemetry-specification/pull/2633). (#2982)
- Attempting to start a span with a nil `context` will no longer cause a panic. (#3110)

## [1.9.0/0.0.3] - 2022-08-01

Expand Down
10 changes: 10 additions & 0 deletions sdk/trace/trace_test.go
Expand Up @@ -363,6 +363,16 @@ func TestStartSpanWithParent(t *testing.T) {
}
}

// Test we get a successful span as a new root if a nil context is sent in, as opposed to a panic.
// See https://github.com/open-telemetry/opentelemetry-go/issues/3109
func TestStartSpanWithNilContext(t *testing.T) {
tp := NewTracerProvider()
tr := tp.Tracer("NoPanic")

// nolint:staticcheck // no nil context, but that's the point of the test.
assert.NotPanics(t, func() { tr.Start(nil, "should-not-panic") })
}

func TestStartSpanNewRootNotSampled(t *testing.T) {
alwaysSampleTp := NewTracerProvider()
sampledTr := alwaysSampleTp.Tracer("AlwaysSampled")
Expand Down
5 changes: 5 additions & 0 deletions sdk/trace/tracer.go
Expand Up @@ -37,6 +37,11 @@ var _ trace.Tracer = &tracer{}
func (tr *tracer) Start(ctx context.Context, name string, options ...trace.SpanStartOption) (context.Context, trace.Span) {
config := trace.NewSpanStartConfig(options...)

if ctx == nil {
// Prevent trace.ContextWithSpan from panicking.
ctx = context.Background()
}

// For local spans created by this SDK, track child span count.
if p := trace.SpanFromContext(ctx); p != nil {
if sdkSpan, ok := p.(*recordingSpan); ok {
Expand Down

0 comments on commit 55b49c4

Please sign in to comment.