Skip to content

sdk/metrics: copy attributes dict to prevent post-recording mutation#5106

Open
tejasae-afk wants to merge 2 commits intoopen-telemetry:mainfrom
tejasae-afk:fix/metrics-attributes-copy
Open

sdk/metrics: copy attributes dict to prevent post-recording mutation#5106
tejasae-afk wants to merge 2 commits intoopen-telemetry:mainfrom
tejasae-afk:fix/metrics-attributes-copy

Conversation

@tejasae-afk
Copy link
Copy Markdown

@tejasae-afk tejasae-afk commented Apr 15, 2026

Description

When a caller retains a reference to the attributes dict passed to counter.add() (or any instrument's add/record call) 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:

# before
attributes = measurement.attributes

That dict is then passed to _create_aggregation, which stores it as self._attributes in the _Aggregation base class. Any subsequent mutation of the caller's dict changes what gets exported.

The fix is a one-line copy:

# after
attributes = dict(measurement.attributes)

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_keys is not None) already builds a fresh dict via comprehension, so it is unaffected.

Fixes #4610

Type of change

  • Bug fix (non-breaking change which fixes an issue)

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.

  • Unit test added in opentelemetry-sdk/tests/metrics/

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

…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
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Apr 15, 2026

CLA Missing ID CLA Not Signed

One or more co-authors of this pull request were not found. You must specify co-authors in commit message trailer via:

Co-authored-by: name <email>

Supported Co-authored-by: formats include:

  1. Anything <id+login@users.noreply.github.com> - it will locate your GitHub user by id part.
  2. Anything <login@users.noreply.github.com> - it will locate your GitHub user by login part.
  3. Anything <public-email> - it will locate your GitHub user by public-email part. Note that this email must be made public on Github.
  4. Anything <other-email> - it will locate your GitHub user by other-email part but only if that email was used before for any other CLA as a main commit author.
  5. login <any-valid-email> - it will locate your GitHub user by login part, note that login part must be at least 3 characters long.

Alternatively, if the co-author should not be included, remove the Co-authored-by: line from the commit message.

Please update your commit message(s) by doing git commit --amend and then git push [--force] and then request re-running CLA check via commenting on this pull request:

/easycla

@tammy-baylis-swi
Copy link
Copy Markdown
Contributor

THanks for this! Please sign the CLA.

Copy link
Copy Markdown
Member

@MikeGoldsmith MikeGoldsmith left a comment

Choose a reason for hiding this comment

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

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.

Comment thread CHANGELOG.md Outdated
Comment on lines +15 to +16
- `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))
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.

Changelog entries should link to the PR, not issue.

Suggested change
- `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))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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>
@tammy-baylis-swi tammy-baylis-swi moved this to Reviewed PRs that need fixes in Python PR digest Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

Counter measurement attributes aren't immutable

3 participants