diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py index e0697fc9de3..a9b1d501851 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py @@ -59,8 +59,10 @@ def consume_measurement(self, measurement: Measurement) -> None: for key, value in measurement.attributes.items(): if key in self._attribute_keys: attributes[key] = value - else: + elif measurement.attributes is not None: attributes = measurement.attributes + else: + attributes = {} attributes = frozenset(attributes.items()) diff --git a/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py b/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py index 769a0c3dd79..38f9249c434 100644 --- a/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py +++ b/opentelemetry-sdk/tests/metrics/test_view_instrument_match.py @@ -86,6 +86,22 @@ def test_consume_measurement(self): }, ) + view_instrument_match = _ViewInstrumentMatch( + "name", + "unit", + "description", + self.mock_aggregation_class, + self.mock_instrumentation_info, + self.mock_resource, + ) + view_instrument_match.consume_measurement( + Measurement(value=0, attributes=None) + ) + self.assertEqual( + view_instrument_match._attributes_aggregation, + {frozenset({}): self.mock_aggregation_instance}, + ) + def test_collect(self): view_instrument_match = _ViewInstrumentMatch(