Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased
- 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))

## Version 1.18.0/0.39b0 (2023-05-04)

- Select histogram aggregation with an environment variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class View:
card characters should not be used with this attribute if the view has also a
``name`` defined.

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

meter_name: This is an instrument matching attribute: the name the
instrument meter must have to match the view.

Expand Down Expand Up @@ -85,6 +88,7 @@ def __init__(
self,
instrument_type: Optional[Type[Instrument]] = None,
instrument_name: Optional[str] = None,
instrument_unit: Optional[str] = None,
meter_name: Optional[str] = None,
meter_version: Optional[str] = None,
meter_schema_url: Optional[str] = None,
Expand All @@ -96,6 +100,7 @@ def __init__(
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"})

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

def test_meter_name(self):

self.assertTrue(
Expand Down