Skip to content

Commit

Permalink
trace: replace newEvictedQueue sync.OnceFunc with sync.Once
Browse files Browse the repository at this point in the history
benchstat:
```
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/sdk/trace
cpu: 11th Gen Intel(R) Core(TM) i5-11400H @ 2.70GHz
                                 │   old.txt    │               new.txt                │
                                 │    sec/op    │    sec/op     vs base                │
TraceStart/with_a_simple_span-12    743.6n ± 5%   451.0n ±  5%  -39.36% (p=0.000 n=10)
TraceStart/with_several_links-12    944.4n ± 7%   595.8n ±  3%  -36.91% (p=0.000 n=10)
TraceStart/with_attributes-12      1034.5n ± 7%   644.5n ± 10%  -37.70% (p=0.000 n=10)
geomean                             898.9n        557.4n        -38.00%

                                 │  old.txt   │              new.txt               │
                                 │    B/op    │    B/op     vs base                │
TraceStart/with_a_simple_span-12   704.0 ± 0%   496.0 ± 0%  -29.55% (p=0.000 n=10)
TraceStart/with_several_links-12   880.0 ± 0%   672.0 ± 0%  -23.64% (p=0.000 n=10)
TraceStart/with_attributes-12      960.0 ± 0%   752.0 ± 0%  -21.67% (p=0.000 n=10)
geomean                            841.0        630.5       -25.03%

                                 │   old.txt   │              new.txt               │
                                 │  allocs/op  │ allocs/op   vs base                │
TraceStart/with_a_simple_span-12   14.000 ± 0%   2.000 ± 0%  -85.71% (p=0.000 n=10)
TraceStart/with_several_links-12   15.000 ± 0%   3.000 ± 0%  -80.00% (p=0.000 n=10)
TraceStart/with_attributes-12      16.000 ± 0%   4.000 ± 0%  -75.00% (p=0.000 n=10)
geomean                             14.98        2.884       -80.74%
```
  • Loading branch information
boekkooi-impossiblecloud committed Oct 1, 2024
1 parent d7e7da6 commit e3c5cd1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions sdk/trace/evictedqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@ import (

// evictedQueue is a FIFO queue with a configurable capacity.
type evictedQueue[T any] struct {
queue []T
capacity int
droppedCount int
logDropped func()
queue []T
capacity int
droppedCount int
logDroppedFunc func()
logDroppedOnce sync.Once
}

func newEvictedQueueEvent(capacity int) evictedQueue[Event] {
// Do not pre-allocate queue, do this lazily.
return evictedQueue[Event]{
capacity: capacity,
logDropped: sync.OnceFunc(func() { global.Warn("limit reached: dropping trace trace.Event") }),
capacity: capacity,
logDroppedFunc: func() { global.Warn("limit reached: dropping trace trace.Event") },
}
}

func newEvictedQueueLink(capacity int) evictedQueue[Link] {
// Do not pre-allocate queue, do this lazily.
return evictedQueue[Link]{
capacity: capacity,
logDropped: sync.OnceFunc(func() { global.Warn("limit reached: dropping trace trace.Link") }),
capacity: capacity,
logDroppedFunc: func() { global.Warn("limit reached: dropping trace trace.Link") },
}
}

Expand All @@ -53,6 +54,10 @@ func (eq *evictedQueue[T]) add(value T) {
eq.queue = append(eq.queue, value)
}

func (eq *evictedQueue[T]) logDropped() {
eq.logDroppedOnce.Do(eq.logDroppedFunc)
}

// copy returns a copy of the evictedQueue.
func (eq *evictedQueue[T]) copy() []T {
return slices.Clone(eq.queue)
Expand Down
2 changes: 1 addition & 1 deletion sdk/trace/evictedqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCopy(t *testing.T) {
func TestDropCount(t *testing.T) {
q := newEvictedQueueEvent(3)
var called bool
q.logDropped = func() { called = true }
q.logDroppedFunc = func() { called = true }

q.add(Event{Name: "value1"})
assert.False(t, called, `"value1" logged as dropped`)
Expand Down

0 comments on commit e3c5cd1

Please sign in to comment.