-
Notifications
You must be signed in to change notification settings - Fork 715
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
base: main
Are you sure you want to change the base?
Conversation
582958e
to
b48902f
Compare
b48902f
to
f945980
Compare
* 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.
6baf134
to
857165f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again for the PR! The biggest possible impact I imagine is a performance regression, but it may be minimal for small attribute sets. Could you run the SDK benchmarks tox -e benchmark-opentelemetry-sdk
on your machine before and after and see if there is any significant change? Admittedly we don't have great benchmark coverage here so feel free to add some new benchmark cases if you see fit
# one will come from napoleon extension and the other from autodoc extension. This | ||
# will raise an sphinx error of duplicated object description | ||
# See https://github.com/sphinx-doc/sphinx/issues/8664 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was this intentional?
super().__setattr__( | ||
"attributes", | ||
deepcopy(self.attributes), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this to work around frozen=True
?
|
||
@pytest.fixture | ||
def unix_time() -> int: | ||
return time_ns() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you hardcode a timestamp here just for repeatability?
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.
Fixes #4610
Type of change
How Has This Been Tested?
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
orunittest
tests in the project. Let me know if these tests would fit better in a different file, or asunittest
tests.Does This PR Require a Contrib Repo Change?
Checklist: