-
Couldn't load subscription status.
- Fork 757
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
P4rk
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
P4rk:fix/copy-measurement-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
61 changes: 61 additions & 0 deletions
61
opentelemetry-sdk/tests/metrics/integration_test/test_data_point_creation.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import pytest | ||
|
|
||
| from opentelemetry.sdk.metrics import Meter, MeterProvider | ||
| from opentelemetry.sdk.metrics.export import ( | ||
| InMemoryMetricReader, | ||
| ) | ||
| from opentelemetry.util.types import Attributes | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def attributes() -> Attributes: | ||
| return { | ||
| "key": "value", | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def meter_name() -> str: | ||
| return "test_meter" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def reader() -> InMemoryMetricReader: | ||
| return InMemoryMetricReader() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def meter_provider(reader: InMemoryMetricReader) -> MeterProvider: | ||
| return MeterProvider(metric_readers=[reader]) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def meter(meter_provider: MeterProvider, meter_name: str) -> Meter: | ||
| return meter_provider.get_meter("test_meter") | ||
|
|
||
|
|
||
| def test_measurement_collection( | ||
| reader: InMemoryMetricReader, | ||
| meter: Meter, | ||
| attributes: Attributes, | ||
| ) -> None: | ||
| """ | ||
| Validate that adjusting attributes after a data point is created does not affect | ||
| the already collected measurement. | ||
| """ | ||
| counter = meter.create_counter("test_counter") | ||
| counter.add(1, attributes) | ||
| attributes["key"] = "new_value" | ||
| counter.add(1, attributes) | ||
|
|
||
| reader.collect() | ||
|
|
||
| metrics_data = reader.get_metrics_data() | ||
| resource_metric, *_ = metrics_data.resource_metrics | ||
| scope_metric, *_ = resource_metric.scope_metrics | ||
| metrics, *_ = scope_metric.metrics | ||
| data = metrics.data | ||
| data_point_1, data_point_2 = data.data_points | ||
|
|
||
| assert data_point_1.attributes == {"key": "value"} | ||
| assert data_point_2.attributes == {"key": "new_value"} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from opentelemetry.context import Context | ||
| from opentelemetry.metrics import Instrument | ||
| from opentelemetry.sdk.metrics._internal.measurement import ( | ||
| Measurement, | ||
| ) | ||
| from opentelemetry.util.types import Attributes | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def attributes() -> Attributes: | ||
| return { | ||
| "key": "value", | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def unix_time() -> int: | ||
| return 1761568368250037000 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def context() -> Context: | ||
| return Context() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def instrument(): | ||
| return Mock(spec=Instrument) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def measurement( | ||
| unix_time: int, | ||
| instrument: Instrument, | ||
| context: Context, | ||
| attributes: Attributes, | ||
| ) -> Measurement: | ||
| return Measurement( | ||
| value=1.0, | ||
| time_unix_nano=unix_time, | ||
| instrument=instrument, | ||
| context=context, | ||
| attributes=attributes, | ||
| ) | ||
|
|
||
|
|
||
| def test_measurement_attribute_is_a_different_object( | ||
| measurement: Measurement, | ||
| attributes: Attributes, | ||
| ): | ||
| assert measurement.attributes is not attributes | ||
|
|
||
|
|
||
| def test_measurement_attribute_uneffected_by_change( | ||
| measurement: Measurement, | ||
| attributes: Attributes, | ||
| ) -> None: | ||
| attributes["new_key"] = "new_value" | ||
|
|
||
| assert measurement.attributes == { | ||
| "key": "value", | ||
| } |
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.
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?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.
Yes, using the
object's__setattr__allows us to do this and avoid the frozenness of the dataclass.