Repository navigation
Fix component telemetry memory leak with partial config reload enabled - #17877
Conversation
|
This pull request does not have a backport label. Could you fix it @swiatekm? 🙏
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
6094c6f to
13fde74
Compare
TL;DRBuildkite Remediation
Investigation detailsRoot CauseClassification: Dependency/configuration drift.
Relevant file/lines:
Evidence
Verification
Follow-up
What is this? | From workflow: Observability Agentic Workflow — Status Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not. |
The collector injects the component ID as an instrumentation scope attribute, and the metric SDK's meter cache is keyed by that scope and never evicted. A component that shuts down keeps its meters, instruments and pipeline aggregations for the life of the MeterProvider: its callback is unregistered so the metrics go quiet, but nothing is released. A full reload disposes the Service and so the MeterProvider, which hid this. A partial receiver reload deliberately keeps the Service running, so in deployments whose receiver IDs churn the collector grows by every receiver generation it has ever built until it is OOM-killed. A field profile showed ~3,700 receiver generations resident against 237 live receivers, growing ~250 MB/h. Give each component its own MeterProvider so everything it created can be dropped when it stops, and drive that from componentstatus.StatusStopped, which the service already reports for every receiver a partial reload removes or rebuilds. Scope attributes are forwarded untouched, so metric granularity is unchanged. Component-scoped metrics now reach exporters as an external Producer. Readers built from configuration cannot be given a Producer by a caller, since sdkmetric.WithProducer is a ReaderOption consumed by the reader's constructor, so this depends on otelconf.WithMetricProducers. Views are likewise a provider-level option and producer output bypasses the base provider's pipelines entirely, so per-component providers apply the same views via otelconf.NewSDKView. Both come from a fork of otelconf pinned by replace until they can be upstreamed. Assisted-By: Claude Opus 5 <noreply@anthropic.com>
Cover the behaviours that would regress silently: meters released when a component stops, scope attributes forwarded untouched, scopes without a component ID left on the base provider, configured views applied to per-component providers, and component metrics reaching a reader built from configuration through the Producer. Each test was checked against a mutation of the code it guards, so a reintroduced leak, dropped views or a missing Producer fails the suite rather than passing quietly. Assisted-By: Claude Opus 5 <noreply@anthropic.com>
13fde74 to
f47217a
Compare
|
Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane) |
cmacknz
left a comment
There was a problem hiding this comment.
LGTM, agree we should ideally move the fork to the Elastic org but depending on timing that could be done after merge. I'd rather not ship a known memory leak if that's all we have time for before the final 9.6 BC.
💚 Build Succeeded
History
cc @swiatekm |
#17877) (#17905) * Release component meters when a component stops The collector injects the component ID as an instrumentation scope attribute, and the metric SDK's meter cache is keyed by that scope and never evicted. A component that shuts down keeps its meters, instruments and pipeline aggregations for the life of the MeterProvider: its callback is unregistered so the metrics go quiet, but nothing is released. A full reload disposes the Service and so the MeterProvider, which hid this. A partial receiver reload deliberately keeps the Service running, so in deployments whose receiver IDs churn the collector grows by every receiver generation it has ever built until it is OOM-killed. A field profile showed ~3,700 receiver generations resident against 237 live receivers, growing ~250 MB/h. Give each component its own MeterProvider so everything it created can be dropped when it stops, and drive that from componentstatus.StatusStopped, which the service already reports for every receiver a partial reload removes or rebuilds. Scope attributes are forwarded untouched, so metric granularity is unchanged. Component-scoped metrics now reach exporters as an external Producer. Readers built from configuration cannot be given a Producer by a caller, since sdkmetric.WithProducer is a ReaderOption consumed by the reader's constructor, so this depends on otelconf.WithMetricProducers. Views are likewise a provider-level option and producer output bypasses the base provider's pipelines entirely, so per-component providers apply the same views via otelconf.NewSDKView. Both come from a fork of otelconf pinned by replace until they can be upstreamed. Assisted-By: Claude Opus 5 <noreply@anthropic.com> * Add smoke tests for per-component meter lifecycle Cover the behaviours that would regress silently: meters released when a component stops, scope attributes forwarded untouched, scopes without a component ID left on the base provider, configured views applied to per-component providers, and component metrics reaching a reader built from configuration through the Producer. Each test was checked against a mutation of the code it guards, so a reintroduced leak, dropped views or a missing Producer fails the suite rather than passing quietly. Assisted-By: Claude Opus 5 <noreply@anthropic.com> * Make the change continent on the partial reload gate * add changelog entry (cherry picked from commit 09f2004) Co-authored-by: Mikołaj Świątek <mail@mikolajswiatek.com>
The per-component meter providers introduced in #17877 aggregated through a single ManualReader with default settings, so every consumer received cumulative data with default aggregation regardless of its own configuration: an OTLP reader configured with temporality_preference: delta exported component-scoped metrics as cumulative, while service-level metrics stayed delta (#18047). Readers never re-aggregate Producer output, so the producer has to aggregate to match the reader it feeds. Each per-component provider now aggregates one dedicated pipeline per consumer, mirroring that consumer's temporality and aggregation selectors. Producers are registered per consumer and collect only their own pipeline. Config-built readers get their selectors bound at reader construction time through the updated otelconf patch (WithMetricProducerFactories replaces WithMetricProducers): periodic readers bind to their exporter's Temporality/Aggregation, the prometheus pull reader is cumulative with default aggregation. Fixes #18047 Assisted-By: Claude Fable 5 <noreply@anthropic.com>
…) (#18074) The per-component meter providers introduced in #17877 aggregated through a single ManualReader with default settings, so every consumer received cumulative data with default aggregation regardless of its own configuration: an OTLP reader configured with temporality_preference: delta exported component-scoped metrics as cumulative, while service-level metrics stayed delta (#18047). Readers never re-aggregate Producer output, so the producer has to aggregate to match the reader it feeds. Each per-component provider now aggregates one dedicated pipeline per consumer, mirroring that consumer's temporality and aggregation selectors. Producers are registered per consumer and collect only their own pipeline. Config-built readers get their selectors bound at reader construction time through the updated otelconf patch (WithMetricProducerFactories replaces WithMetricProducers): periodic readers bind to their exporter's Temporality/Aggregation, the prometheus pull reader is cumulative with default aggregation. Fixes #18047 Assisted-By: Claude Fable 5 <noreply@anthropic.com> (cherry picked from commit 7075522) Co-authored-by: Mikołaj Świątek <mail@mikolajswiatek.com>
Give each otel collector component its own meter provider whose lifetime is tied to the component's. This is done by using a meter provider that secretly spawns sub-providers and wires them correctly to ensure telemetry reaches the configured readers and views.
We also add an extension we inject unconditionally, whose only job is to watch component state changes and drop the subprovider when a component is stopped.
The wiring requires expanding the API of the otel telemetry configuration package. Here's the diff for the patch to
go.opentelemetry.io/contrib/otelconf.Related issues
Risk
Rationale
Using the same meter provider for each component causes it to acquire resources it never releases - meters, instruments and aggregators. This is not a problem without partial config reload, because the meter provider is recreated on full reload alongside all the components. It's also not a problem without the new pipeline telemetry feature flag enabled - the flag causes component metadata to be added as scope attributes, which the Otel Go SDK uses as a cache key. That cache is never released.
We fix this by giving each component its own meter provider and explicitly tracking their lifetime. This requires some relatively minor changes to the upstream telemetry configuration package.
Rollout and safety
This is enabled by default. It can effectively be disabled by disabling the partial reload feature flag.
Testing
Added unit tests and some functional tests. I also ran benchmarks to prove there isn't a performance regression and that the leak is gone.