From e3c5cd17d06d2c6726042483dcffc709f4502b91 Mon Sep 17 00:00:00 2001 From: warnar boekkooi Date: Tue, 1 Oct 2024 17:17:40 +0200 Subject: [PATCH] trace: replace newEvictedQueue sync.OnceFunc with sync.Once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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% ``` --- sdk/trace/evictedqueue.go | 21 +++++++++++++-------- sdk/trace/evictedqueue_test.go | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/sdk/trace/evictedqueue.go b/sdk/trace/evictedqueue.go index 821c83faa1d..605e72a995b 100644 --- a/sdk/trace/evictedqueue.go +++ b/sdk/trace/evictedqueue.go @@ -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") }, } } @@ -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) diff --git a/sdk/trace/evictedqueue_test.go b/sdk/trace/evictedqueue_test.go index 7b88d63d077..699e2168107 100644 --- a/sdk/trace/evictedqueue_test.go +++ b/sdk/trace/evictedqueue_test.go @@ -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`)