8000 trace: Use non-generic to replace newEvictedQueue in trace.start to … · open-telemetry/opentelemetry-go@30e82e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30e82e0

Browse files
tttoaddmathieuMrAlias
authored
trace: Use non-generic to replace newEvictedQueue in trace.start to reduce memory usage. (#5497)
benchstat: ``` goos: darwin goarch: arm64 pkg: go.opentelemetry.io/otel/sdk/trace │ old │ new │ │ sec/op │ sec/op vs base │ TraceStart-10 950.6n ± 1% 641.0n ± 0% -32.57% (p=0.000 n=10) │ old │ new │ │ B/op │ B/op vs base │ TraceStart-10 1040.0 ± 0% 704.0 ± 0% -32.31% (p=0.000 n=10) │ old │ new │ │ allocs/op │ allocs/op vs base │ TraceStart-10 20.00 ± 0% 14.00 ± 0% -30.00% (p=0.000 n=10) ``` --------- Co-authored-by: Damien Mathieu <damien.mathieu@elastic.co> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
1 parent d3f3ae1 commit 30e82e0

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2727
- The exporter no longer exports the deprecated "otel.library.name" or "otel.library.version" attributes.
2828
- Upgrade `go.opentelemetry.io/otel/semconv/v1.25.0` to `go.opentelemetry.io/otel/semconv/v1.26.0` in `go.opentelemetry.io/otel/sdk/resource`. (#5490)
2929
- Upgrade `go.opentelemetry.io/otel/semconv/v1.25.0` to `go.opentelemetry.io/otel/semconv/v1.26.0` in `go.opentelemetry.io/otel/sdk/trace`. (#5490)
30+
- Use non-generic functions in the `Start` method of `"go.opentelemetry.io/otel/sdk/trace".Trace` to reduce memory allocation. (#5497)
3031

3132
### Fixed
3233

sdk/trace/evictedqueue.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package trace // import "go.opentelemetry.io/otel/sdk/trace"
55

66
import (
7-
"fmt"
87
"slices"
98
"sync"
109

@@ -19,13 +18,19 @@ type evictedQueue[T any] struct {
1918
logDropped func()
2019
}
2120

22-
func newEvictedQueue[T any](capacity int) evictedQueue[T] {
23-
var tVal T
24-
msg := fmt.Sprintf("limit reached: dropping trace %T", tVal)
21+
func newEvictedQueueEvent(capacity int) evictedQueue[Event] {
2522
// Do not pre-allocate queue, do this lazily.
26-
return evictedQueue[T]{
23+
return evictedQueue[Event]{
2724
capacity: capacity,
28-
logDropped: sync.OnceFunc(func() { global.Warn(msg) }),
25+
logDropped: sync.OnceFunc(func() { global.Warn("limit reached: dropping trace trace.Event") }),
26+
}
27+
}
28+
29+
func newEvictedQueueLink(capacity int) evictedQueue[Link] {
30+
// Do not pre-allocate queue, do this lazily.
31+
return evictedQueue[Link]{
32+
capacity: capacity,
33+
logDropped: sync.OnceFunc(func() { global.Warn("limit reached: dropping trace trace.Link") }),
2934
}
3035
}
3136

sdk/trace/evictedqueue_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,47 @@ func init() {
1414
}
1515

1616
func TestAdd(t *testing.T) {
17-
q := newEvictedQueue[string](3)
18-
q.add("value1")
19-
q.add("value2")
17+
q := newEvictedQueueLink(3)
18+
q.add(Link{})
19+
q.add(Link{})
2020
if wantLen, gotLen := 2, len(q.queue); wantLen != gotLen {
2121
t.Errorf("got queue length %d want %d", gotLen, wantLen)
2222
}
2323
}
2424

2525
func TestCopy(t *testing.T) {
26-
q := newEvictedQueue[string](3)
27-
q.add("value1")
26+
q := newEvictedQueueEvent(3)
27+
q.add(Event{Name: "value1"})
2828
cp := q.copy()
2929

30-
q.add("value2")
31-
assert.Equal(t, []string{"value1"}, cp, "queue update modified copy")
30+
q.add(Event{Name: "value2"})
31+
assert.Equal(t, []Event{{Name: "value1"}}, cp, "queue update modified copy")
3232

33-
cp[0] = "value0"
34-
assert.Equal(t, "value1", q.queue[0], "copy update modified queue")
33+
cp[0] = Event{Name: "value0"}
34+
assert.Equal(t, Event{Name: "value1"}, q.queue[0], "copy update modified queue")
3535
}
3636

3737
func TestDropCount(t *testing.T) {
38-
q := newEvictedQueue[string](3)
38+
q := newEvictedQueueEvent(3)
3939
var called bool
4040
q.logDropped = func() { called = true }
4141

42-
q.add("value1")
42+
q.add(Event{Name: "value1"})
4343
assert.False(t, called, `"value1" logged as dropped`)
44-
q.add("value2")
44+
q.add(Event{Name: "value2"})
4545
assert.False(t, called, `"value2" logged as dropped`)
46-
q.add("value3")
46+
q.add(Event{Name: "value3"})
4747
assert.False(t, called, `"value3" logged as dropped`)
48-
q.add("value1")
48+
q.add(Event{Name: "value1"})
4949
assert.True(t, called, `"value2" not logged as dropped`)
50-
q.add("value4")
50+
q.add(Event{Name: "value4"})
5151
if wantLen, gotLen := 3, len(q.queue); wantLen != gotLen {
5252
t.Errorf("got queue length %d want %d", gotLen, wantLen)
5353
}
5454
if wantDropCount, gotDropCount := 2, q.droppedCount; wantDropCount != gotDropCount {
5555
t.Errorf("got drop count %d want %d", gotDropCount, wantDropCount)
5656
}
57-
wantArr := []string{"value3", "value1", "value4"}
57+
wantArr := []Event{{Name: "value3"}, {Name: "value1"}, {Name: "value4"}}
5858
gotArr := q.copy()
5959

6060
if wantLen, gotLen := len(wantArr), len(gotArr); gotLen != wantLen {

sdk/trace/trace_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,3 +2107,16 @@ func TestAddLinkToNonRecordingSpan(t *testing.T) {
21072107
t.Errorf("AddLinkToNonRecordingSpan: -got +want %s", diff)
21082108
}
21092109
}
2110+
2111+
func BenchmarkTraceStart(b *testing.B) {
2112+
tracer := NewTracerProvider().Tracer("")
2113+
ctx := trace.ContextWithSpanContext(context.Background(), trace.SpanContext{})
2114+
2115+
b.ReportAllocs()
2116+
b.ResetTimer()
2117+
2118+
for i := 0; i < b.N; i++ {
2119+
_, span := tracer.Start(ctx, "")
2120+
span.End()
2121+
}
2122+
}

sdk/trace/tracer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ func (tr *tracer) newRecordingSpan(psc, sc trace.SpanContext, name string, sr Sa
132132
spanKind: trace.ValidateSpanKind(config.SpanKind()),
133133
name: name,
134134
startTime: startTime,
135-
events: newEvictedQueue[Event](tr.provider.spanLimits.EventCountLimit),
136-
links: newEvictedQueue[Link](tr.provider.spanLimits.LinkCountLimit),
135+
events: newEvictedQueueEvent(tr.provider.spanLimits.EventCountLimit),
136+
links: newEvictedQueueLink(tr.provider.spanLimits.LinkCountLimit),
137137
tracer: tr,
138138
}
139139

0 commit comments

Comments
 (0)
0