Skip to content

Commit

Permalink
chore: log the message and not the func
Browse files Browse the repository at this point in the history
  • Loading branch information
boekkooi-impossiblecloud committed Oct 4, 2024
1 parent 126bf90 commit baad07e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
12 changes: 6 additions & 6 deletions sdk/trace/evictedqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ type evictedQueue[T any] struct {
queue []T
capacity int
droppedCount int
logDroppedFunc func()
logDroppedMsg string
logDroppedOnce sync.Once
}

func newEvictedQueueEvent(capacity int) evictedQueue[Event] {
// Do not pre-allocate queue, do this lazily.
return evictedQueue[Event]{
capacity: capacity,
logDroppedFunc: func() { global.Warn("limit reached: dropping trace trace.Event") },
capacity: capacity,
logDroppedMsg: "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,
logDroppedFunc: func() { global.Warn("limit reached: dropping trace trace.Link") },
capacity: capacity,
logDroppedMsg: "limit reached: dropping trace trace.Link",
}
}

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

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

// copy returns a copy of the evictedQueue.
Expand Down
23 changes: 17 additions & 6 deletions sdk/trace/evictedqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
"reflect"
"testing"

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/internal/global"
)

func init() {
Expand Down Expand Up @@ -36,18 +40,25 @@ func TestCopy(t *testing.T) {

func TestDropCount(t *testing.T) {
q := newEvictedQueueEvent(3)
var called bool
q.logDroppedFunc = func() { called = true }

var called int
t.Cleanup(func(l logr.Logger) func() {
return func() { global.SetLogger(l) }
}(global.GetLogger()))
global.SetLogger(funcr.New(func(prefix, args string) {
called++
}, funcr.Options{Verbosity: 1}))

q.add(Event{Name: "value1"})
assert.False(t, called, `"value1" logged as dropped`)
assert.Equal(t, 0, called, `"value1" logged as dropped`)
q.add(Event{Name: "value2"})
assert.False(t, called, `"value2" logged as dropped`)
assert.Equal(t, 0, called, `"value2" logged as dropped`)
q.add(Event{Name: "value3"})
assert.False(t, called, `"value3" logged as dropped`)
assert.Equal(t, 0, called, `"value3" logged as dropped`)
q.add(Event{Name: "value1"})
assert.True(t, called, `"value2" not logged as dropped`)
assert.Equal(t, 1, called, `"value2" not logged as dropped`)
q.add(Event{Name: "value4"})
assert.Equal(t, 1, called, `"value4" logged as dropped`)
if wantLen, gotLen := 3, len(q.queue); wantLen != gotLen {
t.Errorf("got queue length %d want %d", gotLen, wantLen)
}
Expand Down

0 comments on commit baad07e

Please sign in to comment.