diff --git a/CHANGELOG.md b/CHANGELOG.md index f1332776efe..1afd81b6ebc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py index b930c7a9660..28f7b4fe087 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py @@ -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. """ @@ -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 @@ -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 @@ -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 diff --git a/opentelemetry-sdk/tests/metrics/test_view.py b/opentelemetry-sdk/tests/metrics/test_view.py index 2d1fee490f7..00376a0068b 100644 --- a/opentelemetry-sdk/tests/metrics/test_view.py +++ b/opentelemetry-sdk/tests/metrics/test_view.py @@ -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(