Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce newEvictedQueueLink and newEvictedQueueEvent memory allocations #5858

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `Logger.Enabled` in `go.opentelemetry.io/otel/log` now accepts a newly introduced `EnabledParameters` type instead of `Record`. (#5791)
- `FilterProcessor.Enabled` in `go.opentelemetry.io/otel/sdk/log/internal/x` now accepts `EnabledParameters` instead of `Record`. (#5791)
- The `Record` type in `go.opentelemetry.io/otel/log` is no longer comparable. (#5847)
- Reduce `newEvictedQueueLink` and `newEvictedQueueEvent` memory allocations. (#5858)
boekkooi-impossiblecloud marked this conversation as resolved.
Show resolved Hide resolved

### Deprecated

Expand Down
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 }
boekkooi-impossiblecloud marked this conversation as resolved.
Show resolved Hide resolved

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