-
Notifications
You must be signed in to change notification settings - Fork 717
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
2
commits 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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from copy import deepcopy | ||
from dataclasses import dataclass | ||
from typing import Union | ||
|
||
|
@@ -34,12 +34,19 @@ class Measurement: | |
""" | ||
|
||
# TODO Fix doc - if using valid Google `Attributes:` key, the attributes are duplicated | ||
# 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 | ||
# 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 | ||
|
||
value: Union[int, float] | ||
time_unix_nano: int | ||
instrument: Instrument | ||
context: Context | ||
attributes: Attributes = None | ||
|
||
def __post_init__(self) -> None: | ||
if self.attributes is not None: | ||
super().__setattr__( | ||
"attributes", | ||
deepcopy(self.attributes), | ||
) | ||
Comment on lines
+49
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this to work around |
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,67 @@ | ||
from time import time_ns | ||
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 time_ns() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you hardcode a timestamp here just for repeatability? |
||
|
||
|
||
@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", | ||
} |
Oops, something went wrong.
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.
was this intentional?