sdk/metrics: copy attributes dict to prevent post-recording mutation#5106
sdk/metrics: copy attributes dict to prevent post-recording mutation#5106tejasae-afk wants to merge 2 commits intoopen-telemetry:mainfrom
Conversation
…utation When a caller retains a reference to the attributes dict passed to counter.add() (or any instrument record/add call), mutating that dict after the call would silently corrupt the attributes stored on the aggregation and subsequently on exported data points. The fix copies the dict at the point where it is first stored as the canonical attributes for a new aggregation bucket, so downstream mutations by the caller have no effect. Fixes open-telemetry#4610
One or more co-authors of this pull request were not found. You must specify co-authors in commit message trailer via: Supported
Alternatively, if the co-author should not be included, remove the Please update your commit message(s) by doing |
|
THanks for this! Please sign the CLA. |
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Changes look good. I've left one suggestion to fix the changelog.
Also, please could you update the PR description to follow the PR template in .github/pull_request_template.md. It helps make sure all relevant details are included and makes reviewing PRs easier / most consistent.
| - `opentelemetry-sdk`: Fix mutable attributes reference in metrics: attributes passed to instrument `add`/`record` are now copied so that subsequent mutations to the caller's dict do not affect recorded data points | ||
| ([#4610](https://github.com/open-telemetry/opentelemetry-python/issues/4610)) |
There was a problem hiding this comment.
Changelog entries should link to the PR, not issue.
| - `opentelemetry-sdk`: Fix mutable attributes reference in metrics: attributes passed to instrument `add`/`record` are now copied so that subsequent mutations to the caller's dict do not affect recorded data points | |
| ([#4610](https://github.com/open-telemetry/opentelemetry-python/issues/4610)) | |
| - `opentelemetry-sdk`: Fix mutable attributes reference in metrics: attributes passed to instrument `add`/`record` are now copied so that subsequent mutations to the caller's dict do not affect recorded data points | |
| ([#5106](https://github.com/open-telemetry/opentelemetry-python/issues/5106)) |
There was a problem hiding this comment.
Good catch — updated the changelog link to point to the PR and also updated the PR description to follow the template. Thanks for the review!
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Description
When a caller retains a reference to the
attributesdict passed tocounter.add()(or any instrument'sadd/recordcall) and mutates it after the call, the mutation silently corrupts the attributes stored on the aggregation and therefore on exported data points.The root cause is in
_ViewInstrumentMatch.consume_measurement: when no attribute key filter is active, the code assigned the measurement's attributes dict directly:That dict is then passed to
_create_aggregation, which stores it asself._attributesin the_Aggregationbase class. Any subsequent mutation of the caller's dict changes what gets exported.The fix is a one-line copy:
A shallow copy is enough for all attribute value types (
str,bool,int,float, and their sequence variants) because those values are themselves immutable once stored.The filtered path (when
_view._attribute_keysis notNone) already builds a fresh dict via comprehension, so it is unaffected.Fixes #4610
Type of change
How Has This Been Tested?
A regression test is included that records a measurement, mutates the original dict, and verifies the exported data point still carries the original attributes.
opentelemetry-sdk/tests/metrics/Does This PR Require a Contrib Repo Change?
Checklist: