Skip to content

Commit

Permalink
Add unit to view instrument selection criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
shalevr committed Jun 11, 2023
1 parent 92bfd08 commit 1d3c268
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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 +97,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 +124,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 +146,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

0 comments on commit 1d3c268

Please sign in to comment.