[OpenTelemetry] Fix metric storage leak#7466
Conversation
Fix metric storage leak that occurs when meters and instruments are repeatedly created and disposed. Resolves open-telemetry#5922.
Add PR number.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7466 +/- ##
==========================================
+ Coverage 90.22% 90.32% +0.10%
==========================================
Files 287 287
Lines 15559 15798 +239
==========================================
+ Hits 14038 14270 +232
- Misses 1521 1528 +7
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Pull request overview
This PR addresses a metrics storage memory leak in the OpenTelemetry SDK that occurs when Meter/instruments are repeatedly created and disposed (as in dotnet/runtime meter unpublish notifications). It does so by reclaiming freed metric storage slots and by removing deactivated instrument entries from the identity-to-metric dictionary so it cannot grow without bound.
Changes:
- Reclaim and reuse metric storage slots after deactivated metrics are removed during collection (
availableMetricIndices+TryReserveMetricIndex). - Prevent unbounded growth of
instrumentIdentityToMetricby removing deactivated entries using value-matching removal to remain race-safe. - Update/add unit tests validating storage reclamation and reuse across repeated collections; add a changelog entry.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/OpenTelemetry.Tests/Metrics/MeterProviderSdkTests.cs | Updates existing test expectations and adds a new regression test ensuring metric storage is reused across many transient meters/collects. |
| src/OpenTelemetry/Metrics/Reader/MetricReaderExt.cs | Implements index reuse for metric storage and removes deactivated metrics from the identity dictionary to eliminate leak vectors. |
| src/OpenTelemetry/CHANGELOG.md | Adds an Unreleased entry documenting the metric storage leak fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Reference-count the metrics to avoid unbounded cache growth.
Duplicate is no longer detected.
Kielek
left a comment
There was a problem hiding this comment.
Changes LGTM, but it will be great if @alanwest or @cijothomas look into this also.
Fixes #5922
Changes
Fix metric storage leak that occurs when meters and instruments are repeatedly created and disposed.
Merge requirement checklist
CHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)