diff --git a/.changelog/5332.added b/.changelog/5332.added new file mode 100644 index 0000000000..d8e9a2dd66 --- /dev/null +++ b/.changelog/5332.added @@ -0,0 +1 @@ +`opentelemetry-sdk`: add support for parameter `exclude_attribute_keys` in `View` diff --git a/opentelemetry-configuration/src/opentelemetry/configuration/_meter_provider.py b/opentelemetry-configuration/src/opentelemetry/configuration/_meter_provider.py index 23db41f207..a25365ce68 100644 --- a/opentelemetry-configuration/src/opentelemetry/configuration/_meter_provider.py +++ b/opentelemetry-configuration/src/opentelemetry/configuration/_meter_provider.py @@ -238,15 +238,13 @@ def _create_view(config: ViewConfig) -> View: f"Unknown instrument type: {selector.instrument_type!r}" ) - attribute_keys: set[str] | None = None + attribute_keys: list[str] | None = None + exclude_attribute_keys: list[str] | None = None if stream.attribute_keys is not None: - if stream.attribute_keys.excluded: - _logger.warning( - "attribute_keys.excluded is not supported by the Python SDK View; " - "the exclusion list will be ignored." - ) + if stream.attribute_keys.excluded is not None: + exclude_attribute_keys = stream.attribute_keys.excluded if stream.attribute_keys.included is not None: - attribute_keys = set(stream.attribute_keys.included) + attribute_keys = stream.attribute_keys.included aggregation = None if stream.aggregation is not None: @@ -263,6 +261,7 @@ def _create_view(config: ViewConfig) -> View: description=stream.description, attribute_keys=attribute_keys, aggregation=aggregation, + exclude_attribute_keys=exclude_attribute_keys, ) diff --git a/opentelemetry-configuration/tests/test_meter_provider.py b/opentelemetry-configuration/tests/test_meter_provider.py index 5e1d7e686a..9ac04cb72f 100644 --- a/opentelemetry-configuration/tests/test_meter_provider.py +++ b/opentelemetry-configuration/tests/test_meter_provider.py @@ -876,15 +876,16 @@ def test_stream_attribute_keys_included(self): ) self.assertEqual(view._attribute_keys, {"key1", "key2"}) - def test_stream_attribute_keys_excluded_logs_warning(self): + def test_stream_attribute_keys_excluded_is_applied(self): config = self._make_view_config( stream_kwargs={"attribute_keys": IncludeExclude(excluded=["key1"])} ) - with self.assertLogs( - "opentelemetry.configuration._meter_provider", level="WARNING" - ) as log: - create_meter_provider(config) - self.assertTrue(any("excluded" in msg for msg in log.output)) + meter_provider = create_meter_provider(config) + views = meter_provider._sdk_config.views + self.assertEqual(len(views), 1) + view = views[0] + self.assertEqual(view._exclude_attribute_keys, frozenset({"key1"})) + self.assertIsNone(view._attribute_keys) def test_stream_aggregation_drop(self): view = self._get_view( diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py index d9ba05363b..d98462b593 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py @@ -98,6 +98,24 @@ def consume_measurement( else: attributes = {} + if self._view._exclude_attribute_keys: + attributes = { + key: value + for key, value in attributes.items() + if key not in self._view._exclude_attribute_keys + } + + if ( + self._view._attribute_keys is not None + or self._view._exclude_attribute_keys + ): + measurement = Measurement( + value=measurement.value, + time_unix_nano=measurement.time_unix_nano, + instrument=measurement.instrument, + context=measurement.context, + attributes=attributes, + ) aggr_key = frozenset(attributes.items()) if aggr_key not in self._attributes_aggregation: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py index 7eb1fcc728..c3ec2fc4c1 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py @@ -2,7 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 -from collections.abc import Callable +from collections.abc import Callable, Iterable from fnmatch import fnmatch from logging import getLogger @@ -88,6 +88,12 @@ class View: instrument_unit: This is an instrument matching attribute: the unit the instrument must have to match the view. + exclude_attribute_keys: This is a metric stream customizing attribute: this is + a set of attribute keys. If not `None` then measurement attributes whose + keys are in ``exclude_attribute_keys`` will be removed before identifying + the metric stream. Applied after ``attribute_keys`` if both are provided. + + This class is not intended to be subclassed by the user. """ @@ -102,13 +108,14 @@ def __init__( meter_schema_url: str | None = None, name: str | None = None, description: str | None = None, - attribute_keys: set[str] | None = None, + attribute_keys: Iterable[str] | None = None, aggregation: Aggregation | None = None, exemplar_reservoir_factory: Callable[ [type[_Aggregation]], ExemplarReservoirBuilder ] | None = None, instrument_unit: str | None = None, + exclude_attribute_keys: Iterable[str] | None = None, ): if ( instrument_type @@ -136,6 +143,15 @@ def __init__( "characters in instrument_name" ) + if attribute_keys is not None and exclude_attribute_keys is not None: + overlap = set(attribute_keys).intersection(exclude_attribute_keys) + + if overlap: + raise Exception( + "attribute_keys and exclude_attribute_keys " + f"must be disjoint. Overlapping keys: " + f"{sorted(overlap)}" + ) # _name, _description, _aggregation, _exemplar_reservoir_factory and # _attribute_keys will be accessed when instantiating a _ViewInstrumentMatch. self._name = name @@ -147,11 +163,18 @@ def __init__( self._meter_schema_url = meter_schema_url self._description = description - self._attribute_keys = attribute_keys + self._attribute_keys = ( + frozenset(attribute_keys) if attribute_keys is not None else None + ) self._aggregation = aggregation or self._default_aggregation self._exemplar_reservoir_factory = ( exemplar_reservoir_factory or _default_reservoir_factory ) + self._exclude_attribute_keys = ( + frozenset(exclude_attribute_keys) + if exclude_attribute_keys is not None + else None + ) # pylint: disable=too-many-return-statements # pylint: disable=too-many-branches diff --git a/opentelemetry-sdk/tests/metrics/test_view.py b/opentelemetry-sdk/tests/metrics/test_view.py index 03914c99c6..671f186cc5 100644 --- a/opentelemetry-sdk/tests/metrics/test_view.py +++ b/opentelemetry-sdk/tests/metrics/test_view.py @@ -105,3 +105,20 @@ def test_additive_criteria(self): def test_view_name(self): with self.assertRaises(Exception): View(name="name", instrument_name="instrument_name*") + + def test_attribute_keys_and_exclude_attribute_keys_overlap(self): + with self.assertRaises(Exception): + View( + instrument_name="instrument_name", + attribute_keys=("method", "status_code"), + exclude_attribute_keys=("method", "user_id"), + ) + + def test_attribute_keys_and_exclude_attribute_keys_disjoint(self): + view = View( + instrument_name="instrument_name", + attribute_keys=("method", "status_code"), + exclude_attribute_keys=("user_id",), + ) + + self.assertIsNotNone(view) diff --git a/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py b/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py index 73b0e6e24a..dc2e033c4f 100644 --- a/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py +++ b/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py @@ -74,6 +74,125 @@ def setUpClass(cls): views=[], ) + def test_view_instrument_match_exclude_attribute_keys_affects_aggregation( + self, + ): + instrument1 = Mock(name="instrument1") + instrument1.instrumentation_scope = self.mock_instrumentation_scope + + mock_aggregation = MagicMock() + mock_aggregation._create_aggregation.return_value = MagicMock() + + instrument_class_aggregation = MagicMock() + instrument_class_aggregation.__getitem__.return_value = ( + mock_aggregation + ) + + view = View( + instrument_name="instrument1", + exclude_attribute_keys={"user_id"}, + ) + match = _ViewInstrumentMatch( + view, + instrument=instrument1, + instrument_class_aggregation=instrument_class_aggregation, + ) + measurement1 = Measurement( + value=1, + time_unix_nano=time_ns(), + instrument=instrument1, + context=Context(), + attributes={"method": "GET", "user_id": "u1"}, + ) + measurement2 = Measurement( + value=2, + time_unix_nano=time_ns(), + instrument=instrument1, + context=Context(), + attributes={"method": "GET", "user_id": "u2"}, + ) + + match.consume_measurement(measurement1) + match.consume_measurement(measurement2) + + self.assertEqual( + len(match._attributes_aggregation), + 1, + ) + + aggr_key = list(match._attributes_aggregation.keys())[0] + self.assertDictEqual( + dict(aggr_key), + {"method": "GET"}, + ) + + def test_view_instrument_match_exclude_removes_attributes(self): + instrument1 = Mock(name="instrument1") + instrument1.instrumentation_scope = self.mock_instrumentation_scope + mock_aggregation = MagicMock() + mock_aggregation._create_aggregation.return_value = MagicMock() + + instrument_class_aggregation = MagicMock() + instrument_class_aggregation.__getitem__.return_value = ( + mock_aggregation + ) + view = View( + instrument_name="instrument1", + exclude_attribute_keys={"user_id"}, + ) + match = _ViewInstrumentMatch( + view, + instrument=instrument1, + instrument_class_aggregation=instrument_class_aggregation, + ) + measurement = Measurement( + value=1, + time_unix_nano=time_ns(), + instrument=instrument1, + context=Context(), + attributes={"method": "GET", "user_id": "u1"}, + ) + match.consume_measurement(measurement) + aggr_key = list(match._attributes_aggregation.keys())[0] + self.assertNotIn( + "user_id", + dict(aggr_key), + ) + + def test_view_instrument_match_include_then_exclude(self): + instrument1 = Mock(name="instrument1") + instrument1.instrumentation_scope = self.mock_instrumentation_scope + mock_aggregation = MagicMock() + mock_aggregation._create_aggregation.return_value = MagicMock() + + instrument_class_aggregation = MagicMock() + instrument_class_aggregation.__getitem__.return_value = ( + mock_aggregation + ) + view = View( + instrument_name="instrument1", + attribute_keys={"method", "user_id"}, + exclude_attribute_keys={"user_id"}, + ) + match = _ViewInstrumentMatch( + view, + instrument=instrument1, + instrument_class_aggregation=instrument_class_aggregation, + ) + measurement = Measurement( + value=1, + time_unix_nano=time_ns(), + instrument=instrument1, + context=Context(), + attributes={"method": "GET", "user_id": "u1", "x": "y"}, + ) + match.consume_measurement(measurement) + aggr_key = list(match._attributes_aggregation.keys())[0] + self.assertDictEqual( + dict(aggr_key), + {"method": "GET"}, + ) + def test_consume_measurement(self): instrument1 = Mock(name="instrument1") instrument1.instrumentation_scope = self.mock_instrumentation_scope