From c29e93ec421b3c7ef4427791902439b88104eec4 Mon Sep 17 00:00:00 2001 From: Giorgio Vilardo Date: Thu, 2 Apr 2026 12:28:52 +0200 Subject: [PATCH] fix(metrics): guard _advisory access in ExplicitBucketHistogramAggregation for non-Histogram instruments --- CHANGELOG.md | 2 ++ .../sdk/metrics/_internal/aggregation.py | 8 +++++++- opentelemetry-sdk/tests/metrics/test_aggregation.py | 13 +++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a0aa36d4f3..2475a81c1f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- `opentelemetry-sdk`: Fix `AttributeError` in `ExplicitBucketHistogramAggregation` when applied to non-Histogram instruments without explicit boundaries + ([#5034](https://github.com/open-telemetry/opentelemetry-python/pull/5034)) - `opentelemetry-sdk`: Add shared `_parse_headers` helper for declarative config OTLP exporters ([#5021](https://github.com/open-telemetry/opentelemetry-python/pull/5021)) - `opentelemetry-api`: Replace a broad exception in attribute cleaning tests to satisfy pylint in the `lint-opentelemetry-api` CI job diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py index 1779dac0bba..46c30f9049c 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py @@ -1390,7 +1390,13 @@ def _create_aggregation( if self._boundaries is not None: boundaries = self._boundaries else: - boundaries = instrument._advisory.explicit_bucket_boundaries + # guard for usage with instruments without advisory + advisory = getattr(instrument, "_advisory", None) + boundaries = ( + advisory.explicit_bucket_boundaries + if advisory is not None + else None + ) return _ExplicitBucketHistogramAggregation( attributes, diff --git a/opentelemetry-sdk/tests/metrics/test_aggregation.py b/opentelemetry-sdk/tests/metrics/test_aggregation.py index 0bee8b3c180..bacaf350793 100644 --- a/opentelemetry-sdk/tests/metrics/test_aggregation.py +++ b/opentelemetry-sdk/tests/metrics/test_aggregation.py @@ -476,6 +476,19 @@ def test_boundaries(self): ), ) + def test_create_aggregation_on_instrument_without_boundaries(self): + """ExplicitBucketHistogramAggregation should not crash when applied + to a non-Histogram instrument without explicit boundaries. + """ + aggregation = ExplicitBucketHistogramAggregation() + result = aggregation._create_aggregation( + _Counter("test.counter", Mock(), Mock()), + Mock(), + _default_reservoir_factory, + 0, + ) + self.assertIsInstance(result, _ExplicitBucketHistogramAggregation) + class TestAggregationFactory(TestCase): def test_sum_factory(self):