1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-13 19:53:11 +02:00

Update tracer to guard for a nil ctx (#3110)

* 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>
This commit is contained in:
Mitch Usher
2022-08-26 07:53:33 -06:00
committed by GitHub
parent 3810616eb3
commit 55b49c407e
3 changed files with 16 additions and 0 deletions

View File

@@ -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

View File

@@ -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")

View File

@@ -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 {