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

[interna/sharedcomponent] Use a ring buffer to restrict remembered status count #11826

Merged
Merged
Changes from 1 commit
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
Next Next commit
Use a ring buffer to restrict remembered status count
TylerHelmuth committed Dec 9, 2024
commit 8d473790efd70dbe0fbc8011ace26671bd91c243
29 changes: 21 additions & 8 deletions internal/sharedcomponent/sharedcomponent.go
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@
package sharedcomponent // import "go.opentelemetry.io/collector/internal/sharedcomponent"

import (
"container/ring"
"context"
"slices"
"sync"

"go.opentelemetry.io/collector/component"
@@ -77,7 +77,7 @@
c.hostWrapper = &hostWrapper{
host: host,
sources: make([]componentstatus.Reporter, 0),
previousEvents: make([]*componentstatus.Event, 0),
previousEvents: ring.New(5),
}
statusReporter, isStatusReporter := host.(componentstatus.Reporter)
if isStatusReporter {
@@ -108,19 +108,30 @@
type hostWrapper struct {
host component.Host
sources []componentstatus.Reporter
previousEvents []*componentstatus.Event
previousEvents *ring.Ring
lock sync.Mutex
}

func (h *hostWrapper) GetExtensions() map[component.ID]component.Component {
return h.host.GetExtensions()
}

func (h *hostWrapper) hasEvent(e *componentstatus.Event) bool {
hasEvent := false
h.previousEvents.Do(func(a any) {
if previousEvent, ok := a.(*componentstatus.Event); ok && previousEvent == e {
hasEvent = true
}

Check warning on line 124 in internal/sharedcomponent/sharedcomponent.go

Codecov / codecov/patch

internal/sharedcomponent/sharedcomponent.go#L123-L124

Added lines #L123 - L124 were not covered by tests
})
return hasEvent
}

func (h *hostWrapper) Report(e *componentstatus.Event) {
// Only remember an event if it will be emitted and it has not been sent already.
h.lock.Lock()
if len(h.sources) > 0 && !slices.Contains(h.previousEvents, e) {
h.previousEvents = append(h.previousEvents, e)
if len(h.sources) > 0 && !h.hasEvent(e) {
h.previousEvents.Value = e
h.previousEvents = h.previousEvents.Next()
}
h.lock.Unlock()

@@ -133,9 +144,11 @@

func (h *hostWrapper) addSource(s componentstatus.Reporter) {
h.lock.Lock()
for _, e := range h.previousEvents {
s.Report(e)
}
h.previousEvents.Do(func(a any) {
if e, ok := a.(*componentstatus.Event); ok {
s.Report(e)
}
})
h.lock.Unlock()

h.lock.Lock()