Skip to content

Commit 9026027

Browse files
shalevrsrikanthccv
andauthored
Add unit to view instrument selection criteria (#3341)
Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent 9d3d0f8 commit 9026027

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- Add max_scale option to Exponential Bucket Histogram Aggregation [#3323](https://github.com/open-telemetry/opentelemetry-python/pull/3323))
1111
- 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))
12+
- Add unit to view instrument selection criteria
13+
([#3341](https://github.com/open-telemetry/opentelemetry-python/pull/3341))
1214
- Upgrade opentelemetry-proto to 0.20 and regen
1315
[#3355](https://github.com/open-telemetry/opentelemetry-python/pull/3355))
1416

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class View:
7676
corresponding metrics stream. If `None` an instance of
7777
`DefaultAggregation` will be used.
7878
79+
instrument_unit: This is an instrument matching attribute: the unit the
80+
instrument must have to match the view.
81+
7982
This class is not intended to be subclassed by the user.
8083
"""
8184

@@ -92,10 +95,12 @@ def __init__(
9295
description: Optional[str] = None,
9396
attribute_keys: Optional[Set[str]] = None,
9497
aggregation: Optional[Aggregation] = None,
98+
instrument_unit: Optional[str] = None,
9599
):
96100
if (
97101
instrument_type
98102
is instrument_name
103+
is instrument_unit
99104
is meter_name
100105
is meter_version
101106
is meter_schema_url
@@ -122,6 +127,7 @@ def __init__(
122127
self._name = name
123128
self._instrument_type = instrument_type
124129
self._instrument_name = instrument_name
130+
self._instrument_unit = instrument_unit
125131
self._meter_name = meter_name
126132
self._meter_version = meter_version
127133
self._meter_schema_url = meter_schema_url
@@ -143,6 +149,10 @@ def _match(self, instrument: Instrument) -> bool:
143149
if not fnmatch(instrument.name, self._instrument_name):
144150
return False
145151

152+
if self._instrument_unit is not None:
153+
if not fnmatch(instrument.unit, self._instrument_unit):
154+
return False
155+
146156
if self._meter_name is not None:
147157
if instrument.instrumentation_scope.name != self._meter_name:
148158
return False

opentelemetry-sdk/tests/metrics/test_view.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ def test_instrument_name(self):
3737
View(instrument_name="instrument_name")._match(mock_instrument)
3838
)
3939

40+
def test_instrument_unit(self):
41+
42+
mock_instrument = Mock()
43+
mock_instrument.configure_mock(**{"unit": "instrument_unit"})
44+
45+
self.assertTrue(
46+
View(instrument_unit="instrument_unit")._match(mock_instrument)
47+
)
48+
4049
def test_meter_name(self):
4150

4251
self.assertTrue(

0 commit comments

Comments
 (0)