Fix/copy measurement attributes
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 https://github.com/open-telemetry/opentelemetry-python/issues/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 https://github.com/open-telemetry/opentelemetry-python/issues/4610
Type of change
- [x] 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_collectionopentelemetry-sdk/tests/metrics/test_measurement.py::test_measurement_attribute_is_a_different_objectopentelemetry-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:
- [x] No.
Checklist:
- [x] Followed the style guidelines of this project
- [x] Changelogs have been updated
- [x] Unit tests have been added
- [x] Documentation has been updated (n/a)
The committers listed above are authorized under a signed CLA.
- :white_check_mark: login: P4rk / name: Luke Park (36bc4644e84a0673155ecae7f7558faa828f007e)
@aabmass Sorry I haven't got back to you in a while. Life happened.
I have run tox -e benchmark-opentelemetry-sdk both on this branch and main a few times. The total timings for both fluctuate between 38s +/- 1s.
I have uploaded 2 random runs (that happen to be similar times). I haven't pored over the outputs in detail.
@xrmx I see that this was moved to Reviewed PRs that need fixes. I have addressed everything listed. LMK if theres anything else that needs doing.