Skip to content

Commit c29e93e

Browse files
fix(metrics): guard _advisory access in ExplicitBucketHistogramAggregation for non-Histogram instruments
1 parent d25e09a commit c29e93e

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

CHANGELOG.md

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

1313
## Unreleased
1414

15+
- `opentelemetry-sdk`: Fix `AttributeError` in `ExplicitBucketHistogramAggregation` when applied to non-Histogram instruments without explicit boundaries
16+
([#5034](https://github.com/open-telemetry/opentelemetry-python/pull/5034))
1517
- `opentelemetry-sdk`: Add shared `_parse_headers` helper for declarative config OTLP exporters
1618
([#5021](https://github.com/open-telemetry/opentelemetry-python/pull/5021))
1719
- `opentelemetry-api`: Replace a broad exception in attribute cleaning tests to satisfy pylint in the `lint-opentelemetry-api` CI job

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,13 @@ def _create_aggregation(
13901390
if self._boundaries is not None:
13911391
boundaries = self._boundaries
13921392
else:
1393-
boundaries = instrument._advisory.explicit_bucket_boundaries
1393+
# guard for usage with instruments without advisory
1394+
advisory = getattr(instrument, "_advisory", None)
1395+
boundaries = (
1396+
advisory.explicit_bucket_boundaries
1397+
if advisory is not None
1398+
else None
1399+
)
13941400

13951401
return _ExplicitBucketHistogramAggregation(
13961402
attributes,

opentelemetry-sdk/tests/metrics/test_aggregation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,19 @@ def test_boundaries(self):
476476
),
477477
)
478478

479+
def test_create_aggregation_on_instrument_without_boundaries(self):
480+
"""ExplicitBucketHistogramAggregation should not crash when applied
481+
to a non-Histogram instrument without explicit boundaries.
482+
"""
483+
aggregation = ExplicitBucketHistogramAggregation()
484+
result = aggregation._create_aggregation(
485+
_Counter("test.counter", Mock(), Mock()),
486+
Mock(),
487+
_default_reservoir_factory,
488+
0,
489+
)
490+
self.assertIsInstance(result, _ExplicitBucketHistogramAggregation)
491+
479492

480493
class TestAggregationFactory(TestCase):
481494
def test_sum_factory(self):

0 commit comments

Comments
 (0)