Skip to content

Commit

Permalink
Remove memory leak at exporter shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
madaraszg-tulip committed Nov 25, 2024
1 parent f74890a commit f592107
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
25 changes: 25 additions & 0 deletions .chloggen/exporter-shutdown-memory-leak.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: exporterhelper

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix memory leak at exporter shutdown

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

# (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: []
16 changes: 8 additions & 8 deletions exporter/exporterhelper/internal/metadata/generated_telemetry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 27 additions & 6 deletions exporter/exporterhelper/internal/queue_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type QueueSender struct {
traceAttribute attribute.KeyValue
consumers *queue.Consumers[internal.Request]

shutdownCallbacks []func()

obsrep *ObsReport
exporterID component.ID
}
Expand Down Expand Up @@ -108,18 +110,37 @@ func (qs *QueueSender) Start(ctx context.Context, host component.Host) error {
}

dataTypeAttr := attribute.String(DataTypeKey, qs.obsrep.Signal.String())
return multierr.Append(
qs.obsrep.TelemetryBuilder.InitExporterQueueSize(func() int64 { return int64(qs.queue.Size()) },
metric.WithAttributeSet(attribute.NewSet(qs.traceAttribute, dataTypeAttr))),
qs.obsrep.TelemetryBuilder.InitExporterQueueCapacity(func() int64 { return int64(qs.queue.Capacity()) },
metric.WithAttributeSet(attribute.NewSet(qs.traceAttribute))),
)

reg1, err1 := qs.obsrep.TelemetryBuilder.InitExporterQueueSize(func() int64 { return int64(qs.queue.Size()) },
metric.WithAttributeSet(attribute.NewSet(qs.traceAttribute, dataTypeAttr)))

qs.shutdownCallbacks = append(qs.shutdownCallbacks, func() {
if reg1 != nil {
_ = reg1.Unregister()
}
})

reg2, err2 := qs.obsrep.TelemetryBuilder.InitExporterQueueCapacity(func() int64 { return int64(qs.queue.Capacity()) },
metric.WithAttributeSet(attribute.NewSet(qs.traceAttribute)))

qs.shutdownCallbacks = append(qs.shutdownCallbacks, func() {
if reg2 != nil {
_ = reg2.Unregister()
}
})

return multierr.Append(err1, err2)
}

// Shutdown is invoked during service shutdown.
func (qs *QueueSender) Shutdown(ctx context.Context) error {
// Stop the queue and consumers, this will drain the queue and will call the retry (which is stopped) that will only
// try once every request.

for _, callback := range qs.shutdownCallbacks {
callback()
}

if err := qs.queue.Shutdown(ctx); err != nil {
return err
}
Expand Down

0 comments on commit f592107

Please sign in to comment.