Skip to content

Commit bd9e33c

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

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

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)