Skip to content

Commit

Permalink
Add unit to view instrument selection criteria (#3341)
Browse files Browse the repository at this point in the history
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
  • Loading branch information
shalevr and srikanthccv committed Jun 26, 2023
1 parent 9d3d0f8 commit 9026027
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
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
10 changes: 10 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py
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"})

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

def test_meter_name(self):

self.assertTrue(
Expand Down

0 comments on commit 9026027

Please sign in to comment.