sdk/metrics: copy attributes dict to prevent post-recording mutation#5106
Open
tejasae-afk wants to merge 1 commit intoopen-telemetry:mainfrom
Open
sdk/metrics: copy attributes dict to prevent post-recording mutation#5106tejasae-afk wants to merge 1 commit intoopen-telemetry:mainfrom
tejasae-afk wants to merge 1 commit 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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.A regression test is included that records a measurement, mutates the original dict, and verifies the exported data point still carries the original attributes.
Closes #4610.
you know the codebase far better than I do, happy to adjust anything here.