Skip to content

Fix/copy measurement attributes #4627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

P4rk
Copy link

@P4rk P4rk commented Jun 9, 2025

Description

This change gets the Measurement dataclass to create its own copy of the attributes that are passed to it. This stops changes to the attributes from outside the scope of the measurement affecting it.

I had considered making the Measurement attributes immutable, but that has a larger impact on the rest of the codebase and does more than just fix #4610.

fix: Format TODO
Indent #TODO text so that it appears as a single TODO

fix: Set the measurements attributes to a copy
Set the attributes on the measurement as a copy of the attributes passed in. This makes sure that they cannot be changed, by using a reference from outside the scope of the measurement instance.

Fixes #4610

Type of change

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

How Has This Been Tested?

uv run pytest opentelemetry-sdk/tests/metrics/integration_test/test_data_point_creation.py opentelemetry-sdk/tests/metrics/test_measurement.py

Two new test files were added: a unit test and an integration test.

  • opentelemetry-sdk/tests/metrics/integration_test/test_data_point_creation.py::test_measurement_collection
  • opentelemetry-sdk/tests/metrics/test_measurement.py::test_measurement_attribute_is_a_different_object
  • opentelemetry-sdk/tests/metrics/test_measurement.py::test_measurement_attribute_uneffected_by_change

These are new files and pytest tests. There were a few pytest or unittest tests in the project. Let me know if these tests would fit better in a different file, or as unittest tests.

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated (n/a)

Copy link

linux-foundation-easycla bot commented Jun 9, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: P4rk / name: Luke Park (e694eb7, f945980)
  • ✅ login: xrmx / name: Riccardo Magliocchetti (6baf134)

@P4rk P4rk force-pushed the fix/copy-measurement-attributes branch from 582958e to b48902f Compare June 11, 2025 09:47
@P4rk P4rk marked this pull request as ready for review June 11, 2025 09:57
@P4rk P4rk requested a review from a team as a code owner June 11, 2025 09:57
P4rk added 2 commits June 13, 2025 11:26
* Indent #TODO text so that it appears as a single TODO
Set the attributes on the measurement as a copy of the attributes passed in. This makes sure that they cannot be changed, by using a reference from outside the scope of the measurement instance.
@P4rk P4rk force-pushed the fix/copy-measurement-attributes branch from b48902f to f945980 Compare June 13, 2025 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Counter measurement attributes aren't immutable
2 participants