Skip to content

Commit 1d6171b

Browse files
giorgiovilardoxrmx
andauthored
fix(metrics): guard _advisory access in ExplicitBucketHistogramAggregation for non-Histogram instruments (#5034)
Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent d9a8c70 commit 1d6171b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

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
- Fix `BatchLogRecordProcessor` default `schedule_delay_millis` from 5000ms to 1000ms to comply with the OTel specification. Note: logs may be exported 5x more frequently by default (e.g. for users who don't explicitly set the `OTEL_BLRP_SCHEDULE_DELAY` env var).
1618
([#4998](https://github.com/open-telemetry/opentelemetry-python/pull/4998))
1719
- `opentelemetry-sdk`: Add `process` resource detector support to declarative file configuration via `detection_development.detectors[].process`

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)