Skip to content

Commit

Permalink
[interna/sharedcomponent] Use a ring buffer to restrict remembered st…
Browse files Browse the repository at this point in the history
…atus count (open-telemetry#11826)

#### Description
Use a ring buffer to only remember the last 5 events. This ensures we
remember a reasonable number of events during startup, so that a status
aggregator gets the events for all instances. Then during normal
operation, when we're done adding sources and no longer need to replay
events, we don't have to remember every single event.

#### Link to tracking issue
Closes
open-telemetry#11818

#### Testing

`go test status_test.go -count 1000 -failfast` still passes with many
tries.
  • Loading branch information
TylerHelmuth authored and HongChenTW committed Dec 19, 2024
1 parent 98cd145 commit cad463c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
25 changes: 25 additions & 0 deletions .chloggen/fix-sharedcomponent-memory-issue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: internal/sharedcomponent

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fixed bug where sharedcomponent would use too much memory remembering all the previously reported statuses

# One or more tracking issues or pull requests related to the change
issues: [11826]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
19 changes: 11 additions & 8 deletions internal/sharedcomponent/sharedcomponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package sharedcomponent // import "go.opentelemetry.io/collector/internal/sharedcomponent"

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

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -77,7 +77,7 @@ func (c *Component[V]) Start(ctx context.Context, host component.Host) error {
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 {
Expand Down Expand Up @@ -110,7 +110,7 @@ var (
type hostWrapper struct {
host component.Host
sources []componentstatus.Reporter
previousEvents []*componentstatus.Event
previousEvents *ring.Ring
lock sync.Mutex
}

Expand All @@ -122,8 +122,9 @@ 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()
defer h.lock.Unlock()
if len(h.sources) > 0 && !slices.Contains(h.previousEvents, e) {
h.previousEvents = append(h.previousEvents, e)
if len(h.sources) > 0 {
h.previousEvents.Value = e
h.previousEvents = h.previousEvents.Next()
}
for _, s := range h.sources {
s.Report(e)
Expand All @@ -133,9 +134,11 @@ func (h *hostWrapper) Report(e *componentstatus.Event) {
func (h *hostWrapper) addSource(s componentstatus.Reporter) {
h.lock.Lock()
defer h.lock.Unlock()
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.sources = append(h.sources, s)
}

Expand Down

0 comments on commit cad463c

Please sign in to comment.