Skip to content
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

Add unit to view instrument selection criteria #3341

Merged
merged 6 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add max_scale option to Exponential Bucket Histogram Aggregation [#3323](https://github.com/open-telemetry/opentelemetry-python/pull/3323))
- Use BoundedAttributes instead of raw dict to extract attributes from LogRecord and Support dropped_attributes_count in LogRecord ([#3310](https://github.com/open-telemetry/opentelemetry-python/pull/3310))
- Add unit to view instrument selection criteria
([#3341](https://github.com/open-telemetry/opentelemetry-python/pull/3341))
- Upgrade opentelemetry-proto to 0.20 and regen
[#3355](https://github.com/open-telemetry/opentelemetry-python/pull/3355))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class View:
corresponding metrics stream. If `None` an instance of
`DefaultAggregation` will be used.

instrument_unit: This is an instrument matching attribute: the unit the
instrument must have to match the view.

This class is not intended to be subclassed by the user.
"""

Expand All @@ -92,10 +95,12 @@ def __init__(
description: Optional[str] = None,
attribute_keys: Optional[Set[str]] = None,
aggregation: Optional[Aggregation] = None,
instrument_unit: Optional[str] = None,
):
if (
instrument_type
is instrument_name
is instrument_unit
is meter_name
is meter_version
is meter_schema_url
Expand All @@ -122,6 +127,7 @@ def __init__(
self._name = name
self._instrument_type = instrument_type
self._instrument_name = instrument_name
self._instrument_unit = instrument_unit
self._meter_name = meter_name
self._meter_version = meter_version
self._meter_schema_url = meter_schema_url
Expand All @@ -143,6 +149,10 @@ def _match(self, instrument: Instrument) -> bool:
if not fnmatch(instrument.name, self._instrument_name):
return False

if self._instrument_unit is not None:
if not fnmatch(instrument.unit, self._instrument_unit):
return False

if self._meter_name is not None:
if instrument.instrumentation_scope.name != self._meter_name:
return False
Expand Down
9 changes: 9 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ def test_instrument_name(self):
View(instrument_name="instrument_name")._match(mock_instrument)
)

def test_instrument_unit(self):

mock_instrument = Mock()
mock_instrument.configure_mock(**{"unit": "instrument_unit"})
shalevr marked this conversation as resolved.
Show resolved Hide resolved

self.assertTrue(
View(instrument_unit="instrument_unit")._match(mock_instrument)
)

def test_meter_name(self):

self.assertTrue(
Expand Down