Skip to content

Fix component telemetry memory leak with partial config reload enabled - #17877

Merged
swiatekm merged 5 commits into
mainfrom
swiatekm/Memory-leak
Nov 9, 2026
Merged

swiatekm merged 5 commits into
mainfrom
swiatekm/Memory-leak

Conversation

@swiatekm

@swiatekm swiatekm commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

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

  • Low: isolated change, no effect on Fleet connectivity, policy handling, or upgrades
  • Medium: changes existing behavior, but is feature flagged or a failure does not affect Fleet connectivity, policy handling, or upgrades
  • High: touches Fleet connectivity, policy application, upgrade/rollback, installation, or changes defaults for existing users

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.

@swiatekm swiatekm added the backport-9.6 Automated backport to the 9.6 branch label Nov 8, 2026
@mergify

mergify Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

This pull request does not have a backport label. Could you fix it @swiatekm? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-./d./d is the label that automatically backports to the 8./d branch. /d is the digit
  • backport-active-all is the label that automatically backports to all active branches.
  • backport-active-8 is the label that automatically backports to all active minor branches for the 8 major.
  • backport-active-9 is the label that automatically backports to all active minor branches for the 9 major.

@swiatekm swiatekm added Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team bug Something isn't working labels Nov 8, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@swiatekm
swiatekm force-pushed the swiatekm/Memory-leak branch from 6094c6f to 13fde74 Compare November 8, 2026 15:08
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

TL;DR

Buildkite check-ci failed because internal/edot/go.sum is not normalized on the PR branch, so CI detects uncommitted dependency-file changes. Immediate action: run tidy in internal/edot, commit the go.sum update, and rerun CI.

Remediation

  • In internal/edot, run mage tidy (or go mod tidy scoped to that module) and commit the resulting go.sum changes.
  • Re-run make check-ci to confirm no dirty-tree diff remains.
Investigation details

Root Cause

Classification: Dependency/configuration drift.

check-ci runs a no-changes guard and fails because generated dependency state is not committed. The failing diff is in internal/edot/go.sum where stale otelconf checksum lines are removed by CI normalization.

Relevant file/lines:

  • internal/edot/go.sum:1940-1941 (go.opentelemetry.io/contrib/otelconf v0.25.0 checksums)
  • internal/edot/go.mod:762 (go.opentelemetry.io/contrib/otelconf v0.25.0 requirement context)

Evidence

  • Build: https://buildkite.com/elastic/elastic-agent/builds/49682
  • Job/step: check-ci
  • Key log excerpt:
    • Error: some files are not up-to-date ... Modified: [internal/edot/go.sum]
    • diff --git a/internal/edot/go.sum ...
    • -go.opentelemetry.io/contrib/otelconf v0.25.0 ...
    • -go.opentelemetry.io/contrib/otelconf v0.25.0/go.mod ...

Verification

  • Not run locally in this workflow context (read-only investigation).
  • Conclusion is based on the provided Buildkite failure log and repository dependency files.

Follow-up

  • If this recurs after committing tidy output, share the new internal/edot/go.mod/go.sum diff from PR head to identify a transitive dependency reintroduction.

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>
@swiatekm
swiatekm force-pushed the swiatekm/Memory-leak branch from 13fde74 to f47217a Compare November 8, 2026 18:21
Comment thread internal/edot/go.mod Resolved
@swiatekm
swiatekm marked this pull request as ready for review November 9, 2026 17:49
@swiatekm
swiatekm requested a review from a team as a code owner November 9, 2026 17:49
@swiatekm
swiatekm requested a review from blakerouse November 9, 2026 17:49
@infra-vault-gh-plugin-prod

Copy link
Copy Markdown

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

@swiatekm
swiatekm requested a review from cmacknz November 9, 2026 17:50

@cmacknz cmacknz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@infra-vault-gh-plugin-prod

Copy link
Copy Markdown

💚 Build Succeeded

History

cc @swiatekm

@swiatekm
swiatekm merged commit 09f2004 into main Nov 9, 2026
37 of 38 checks passed
@swiatekm
swiatekm deleted the swiatekm/Memory-leak branch November 9, 2026 21:11
swiatekm added a commit that referenced this pull request Nov 9, 2026
#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>
swiatekm added a commit that referenced this pull request Nov 17, 2026
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>
swiatekm added a commit that referenced this pull request Nov 17, 2026
…) (#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-9.6 Automated backport to the 9.6 branch bug Something isn't working Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants