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 3, 2024
1 parent cb5c7d4 commit 19a5196
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The `IsEmpty` method is added to the `Instrument` type in `go.opentelemetry.io/otel/sdk/metric`.
This method is used to check if an `Instrument` instance is a zero-value. (#5431)

### Changed

- `Tracer.Start` in `go.opentelemetry.io/otel/trace/noop` no longer allocates a span for empty span context. (#5457)

### Fixed

- Log a warning to the OpenTelemetry internal logger when a `Record` in `go.opentelemetry.io/otel/sdk/log` drops an attribute due to a limit being reached. (#5376)
Expand Down
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
13 changes: 13 additions & 0 deletions trace/noop/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ 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++ {
_, span := tracer.Start(ctx, "")
span.End()
}
}

type recordingSpan struct{ Span }

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

0 comments on commit 19a5196

Please sign in to comment.