Skip to content

Commit 947aa74

Browse files
xrmxemdneto
andauthored
opentelemetry-sdk: fix OTLP exporting of histogram explicit buckets advisory (#4434)
* opentelemetry-sdk: fix OTLP exporting of histogram explicit buckets advisory OTLP exporters don't rely on the default aggregation for histogram instead they do it explicitly. So instead of setting boundaries at init time of ExplicitBucketHistogramAggregation delay it in _create_aggregation when we have the Histogram at hand. Co-authored-by: Emídio Neto <[email protected]> * Add changelog * Update CHANGELOG.md Co-authored-by: Emídio Neto <[email protected]> --------- Co-authored-by: Emídio Neto <[email protected]>
1 parent a7fe4f8 commit 947aa74

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

CHANGELOG.md

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

1010
- Tolerates exceptions when loading resource detectors via `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS`
1111
([#4373](https://github.com/open-telemetry/opentelemetry-python/pull/4373))
12+
- opentelemetry-sdk: fix OTLP exporting of Histograms with explicit buckets advisory
13+
([#4434](https://github.com/open-telemetry/opentelemetry-python/pull/4434))
1214

1315
## Version 1.30.0/0.51b0 (2025-02-03)
1416

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,10 +1367,6 @@ def __init__(
13671367
boundaries: Optional[Sequence[float]] = None,
13681368
record_min_max: bool = True,
13691369
) -> None:
1370-
if boundaries is None:
1371-
boundaries = (
1372-
_DEFAULT_EXPLICIT_BUCKET_HISTOGRAM_AGGREGATION_BOUNDARIES
1373-
)
13741370
self._boundaries = boundaries
13751371
self._record_min_max = record_min_max
13761372

@@ -1391,6 +1387,12 @@ def _create_aggregation(
13911387
AggregationTemporality.CUMULATIVE
13921388
)
13931389

1390+
if self._boundaries is None:
1391+
self._boundaries = (
1392+
instrument._advisory.explicit_bucket_boundaries
1393+
or _DEFAULT_EXPLICIT_BUCKET_HISTOGRAM_AGGREGATION_BOUNDARIES
1394+
)
1395+
13941396
return _ExplicitBucketHistogramAggregation(
13951397
attributes,
13961398
instrument_aggregation_temporality,

opentelemetry-sdk/tests/metrics/integration_test/test_histogram_advisory_explicit_buckets.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from unittest import TestCase
1616

1717
from opentelemetry.sdk.metrics import MeterProvider
18+
from opentelemetry.sdk.metrics._internal.instrument import Histogram
1819
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
1920
from opentelemetry.sdk.metrics.view import (
2021
ExplicitBucketHistogramAggregation,
@@ -133,3 +134,33 @@ def test_view_overrides_buckets(self):
133134
self.assertEqual(
134135
metric.data.data_points[0].explicit_bounds, (10.0, 100.0, 1000.0)
135136
)
137+
138+
def test_explicit_aggregation(self):
139+
reader = InMemoryMetricReader(
140+
preferred_aggregation={
141+
Histogram: ExplicitBucketHistogramAggregation()
142+
}
143+
)
144+
meter_provider = MeterProvider(
145+
metric_readers=[reader],
146+
)
147+
meter = meter_provider.get_meter("testmeter")
148+
histogram = meter.create_histogram(
149+
"testhistogram",
150+
explicit_bucket_boundaries_advisory=[1.0, 2.0, 3.0],
151+
)
152+
histogram.record(1, {"label": "value"})
153+
histogram.record(2, {"label": "value"})
154+
histogram.record(3, {"label": "value"})
155+
156+
metrics = reader.get_metrics_data()
157+
self.assertEqual(len(metrics.resource_metrics), 1)
158+
self.assertEqual(len(metrics.resource_metrics[0].scope_metrics), 1)
159+
self.assertEqual(
160+
len(metrics.resource_metrics[0].scope_metrics[0].metrics), 1
161+
)
162+
metric = metrics.resource_metrics[0].scope_metrics[0].metrics[0]
163+
self.assertEqual(metric.name, "testhistogram")
164+
self.assertEqual(
165+
metric.data.data_points[0].explicit_bounds, (1.0, 2.0, 3.0)
166+
)

0 commit comments

Comments
 (0)