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

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Apr 15, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: tejasae-afk / name: Tejas (24b14a6)
  • ✅ login: tejasae-afk / name: tejasae-afk (e720812)

@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!

@tammy-baylis-swi tammy-baylis-swi moved this to Reviewed PRs that need fixes in Python PR digest Apr 18, 2026
@MikeGoldsmith
Copy link
Copy Markdown
Member

@tejasae-afk EasyCLA can't verify ClaudeCode as a co-author on this commit (08c2390). You'll need to update it.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 6, 2026

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label May 6, 2026
@MikeGoldsmith
Copy link
Copy Markdown
Member

@tejasae-afk we cannot accept contributions unless you sign the CLA, please sign it.

@github-actions github-actions Bot removed the Stale label May 8, 2026
@tejasae-afk tejasae-afk force-pushed the fix/metrics-attributes-copy branch from 08c2390 to dea8513 Compare May 9, 2026 03:38
@tejasae-afk
Copy link
Copy Markdown
Author

/easycla

…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
@tejasae-afk tejasae-afk force-pushed the fix/metrics-attributes-copy branch from dea8513 to 24b14a6 Compare May 9, 2026 03:41
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