Skip to content

Commit baad07e

Browse files
chore: log the message and not the func
1 parent 126bf90 commit baad07e

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

sdk/trace/evictedqueue.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ type evictedQueue[T any] struct {
1515
queue []T
1616
capacity int
1717
droppedCount int
18-
logDroppedFunc func()
18+
logDroppedMsg string
1919
logDroppedOnce sync.Once
2020
}
2121

2222
func newEvictedQueueEvent(capacity int) evictedQueue[Event] {
2323
// Do not pre-allocate queue, do this lazily.
2424
return evictedQueue[Event]{
25-
capacity: capacity,
26-
logDroppedFunc: func() { global.Warn("limit reached: dropping trace trace.Event") },
25+
capacity: capacity,
26+
logDroppedMsg: "limit reached: dropping trace trace.Event",
2727
}
2828
}
2929

3030
func newEvictedQueueLink(capacity int) evictedQueue[Link] {
3131
// Do not pre-allocate queue, do this lazily.
3232
return evictedQueue[Link]{
33-
capacity: capacity,
34-
logDroppedFunc: func() { global.Warn("limit reached: dropping trace trace.Link") },
33+
capacity: capacity,
34+
logDroppedMsg: "limit reached: dropping trace trace.Link",
3535
}
3636
}
3737

@@ -55,7 +55,7 @@ func (eq *evictedQueue[T]) add(value T) {
5555
}
5656

5757
func (eq *evictedQueue[T]) logDropped() {
58-
eq.logDroppedOnce.Do(eq.logDroppedFunc)
58+
eq.logDroppedOnce.Do(func() { global.Warn(eq.logDroppedMsg) })
5959
}
6060

6161
// copy returns a copy of the evictedQueue.

sdk/trace/evictedqueue_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import (
77
"reflect"
88
"testing"
99

10+
"github.com/go-logr/logr"
11+
"github.com/go-logr/logr/funcr"
1012
"github.com/stretchr/testify/assert"
13+
14+
"go.opentelemetry.io/otel/internal/global"
1115
)
1216

1317
func init() {
@@ -36,18 +40,25 @@ func TestCopy(t *testing.T) {
3640

3741
func TestDropCount(t *testing.T) {
3842
q := newEvictedQueueEvent(3)
39-
var called bool
40-
q.logDroppedFunc = func() { called = true }
43+
44+
var called int
45+
t.Cleanup(func(l logr.Logger) func() {
46+
return func() { global.SetLogger(l) }
47+
}(global.GetLogger()))
48+
global.SetLogger(funcr.New(func(prefix, args string) {
49+
called++
50+
}, funcr.Options{Verbosity: 1}))
4151

4252
q.add(Event{Name: "value1"})
43-
assert.False(t, called, `"value1" logged as dropped`)
53+
assert.Equal(t, 0, called, `"value1" logged as dropped`)
4454
q.add(Event{Name: "value2"})
45-
assert.False(t, called, `"value2" logged as dropped`)
55+
assert.Equal(t, 0, called, `"value2" logged as dropped`)
4656
q.add(Event{Name: "value3"})
47-
assert.False(t, called, `"value3" logged as dropped`)
57+
assert.Equal(t, 0, called, `"value3" logged as dropped`)
4858
q.add(Event{Name: "value1"})
49-
assert.True(t, called, `"value2" not logged as dropped`)
59+
assert.Equal(t, 1, called, `"value2" not logged as dropped`)
5060
q.add(Event{Name: "value4"})
61+
assert.Equal(t, 1, called, `"value4" logged as dropped`)
5162
if wantLen, gotLen := 3, len(q.queue); wantLen != gotLen {
5263
t.Errorf("got queue length %d want %d", gotLen, wantLen)
5364
}

0 commit comments

Comments
 (0)