Skip to content

Commit

Permalink
fix: NoopSpan should be instance
Browse files Browse the repository at this point in the history
  • Loading branch information
tttoad committed Jun 1, 2024
1 parent cb5c7d4 commit 9a57d68
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion trace/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ func (t Tracer) Start(ctx context.Context, _ string, _ ...trace.SpanStartOption)
span = Span{sc: sc}
} else {
// No parent, return a No-Op span with an empty span context.
span = Span{}
span = noopSpanInstance
}
return trace.ContextWithSpan(ctx, span), span
}

var noopSpanInstance trace.Span = Span{}

// Span is an OpenTelemetry No-Op Span.
type Span struct {
embedded.Span
Expand Down
16 changes: 16 additions & 0 deletions trace/noop/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ func TestTracerStartPropagatesSpanContext(t *testing.T) {
assert.False(t, span.IsRecording(), "recording span returned")
}

func BenchmarkNoopInstance(b *testing.B) {
tracer := NewTracerProvider().Tracer("")
ctx := trace.ContextWithSpanContext(context.Background(), trace.SpanContext{})

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
for j := 0; j < 10; j++ {
newCtx, span := tracer.Start(ctx, "")
ctx = newCtx
span.End()
}
}
}

type recordingSpan struct{ Span }

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

0 comments on commit 9a57d68

Please sign in to comment.