Skip to content

Commit 032ff89

Browse files
tttoadMrAlias
authored andcommitted
trace: Span in noop.Start is no longer allocated (#5457)
trace/noop/noop.Span should be instance. This will reduce memory waste. Benchmark results with changes: new.txt ``` goos: darwin goarch: arm64 pkg: go.opentelemetry.io/otel/trace/noop BenchmarkNoopInstance-10 29894822 39.94 ns/op 48 B/op 1 allocs/op BenchmarkNoopInstance-10 29675424 39.99 ns/op 48 B/op 1 allocs/op BenchmarkNoopInstance-10 30064700 39.98 ns/op 48 B/op 1 allocs/op BenchmarkNoopInstance-10 29962016 40.03 ns/op 48 B/op 1 allocs/op BenchmarkNoopInstance-10 30060465 40.02 ns/op 48 B/op 1 allocs/op BenchmarkNoopInstance-10 29916855 40.04 ns/op 48 B/op 1 allocs/op BenchmarkNoopInstance-10 29829998 40.28 ns/op 48 B/op 1 allocs/op BenchmarkNoopInstance-10 30084706 39.99 ns/op 48 B/op 1 allocs/op BenchmarkNoopInstance-10 30087441 40.02 ns/op 48 B/op 1 allocs/op BenchmarkNoopInstance-10 29864365 40.14 ns/op 48 B/op 1 allocs/op ``` without changes on `main`: old.txt ``` goos: darwin goarch: arm64 pkg: go.opentelemetry.io/otel/trace/noop BenchmarkNoopInstance-10 14813442 67.64 ns/op 128 B/op 2 allocs/op BenchmarkNoopInstance-10 17714486 68.17 ns/op 128 B/op 2 allocs/op BenchmarkNoopInstance-10 17701257 67.66 ns/op 128 B/op 2 allocs/op BenchmarkNoopInstance-10 17805859 67.69 ns/op 128 B/op 2 allocs/op BenchmarkNoopInstance-10 17912841 67.43 ns/op 128 B/op 2 allocs/op BenchmarkNoopInstance-10 17864120 67.58 ns/op 128 B/op 2 allocs/op BenchmarkNoopInstance-10 17663130 68.41 ns/op 128 B/op 2 allocs/op BenchmarkNoopInstance-10 17740423 67.57 ns/op 128 B/op 2 allocs/op BenchmarkNoopInstance-10 17751040 67.56 ns/op 128 B/op 2 allocs/op BenchmarkNoopInstance-10 17738064 67.91 ns/op 128 B/op 2 allocs/op ``` benchstat: ``` goos: darwin goarch: arm64 pkg: go.opentelemetry.io/otel/trace/noop │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ NoopInstance-10 67.65n ± 1% 40.02n ± 0% -40.84% (p=0.000 n=10) │ old.txt │ new.txt │ │ B/op │ B/op vs base │ NoopInstance-10 128.00 ± 0% 48.00 ± 0% -62.50% (p=0.000 n=10) │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ NoopInstance-10 2.000 ± 0% 1.000 ± 0% -50.00% (p=0.000 n=10) ``` Co-authored-by: Tyler Yahn <[email protected]>
1 parent 4690345 commit 032ff89

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1313
- The `IsEmpty` method is added to the `Instrument` type in `go.opentelemetry.io/otel/sdk/metric`.
1414
This method is used to check if an `Instrument` instance is a zero-value. (#5431)
1515

16+
### Changed
17+
18+
- `Tracer.Start` in `go.opentelemetry.io/otel/trace/noop` no longer allocates a span for empty span context. (#5457)
19+
1620
### Fixed
1721

1822
- 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)

trace/noop/noop.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ func (t Tracer) Start(ctx context.Context, _ string, _ ...trace.SpanStartOption)
6767
span = Span{sc: sc}
6868
} else {
6969
// No parent, return a No-Op span with an empty span context.
70-
span = Span{}
70+
span = noopSpanInstance
7171
}
7272
return trace.ContextWithSpan(ctx, span), span
7373
}
7474

75+
var noopSpanInstance trace.Span = Span{}
76+
7577
// Span is an OpenTelemetry No-Op Span.
7678
type Span struct {
7779
embedded.Span

trace/noop/noop_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ func TestTracerStartPropagatesSpanContext(t *testing.T) {
101101
assert.False(t, span.IsRecording(), "recording span returned")
102102
}
103103

104+
func BenchmarkNoopInstance(b *testing.B) {
105+
tracer := NewTracerProvider().Tracer("")
106+
ctx := trace.ContextWithSpanContext(context.Background(), trace.SpanContext{})
107+
108+
b.ReportAllocs()
109+
b.ResetTimer()
110+
111+
for i := 0; i < b.N; i++ {
112+
_, span := tracer.Start(ctx, "")
113+
span.End()
114+
}
115+
}
116+
104117
type recordingSpan struct{ Span }
105118

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

0 commit comments

Comments
 (0)