Skip to content

Commit

Permalink
opentelemetry-sdk: fix OTLP exporting of histogram explicit buckets a…
Browse files Browse the repository at this point in the history
…dvisory (#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]>
  • Loading branch information
xrmx and emdneto authored Feb 18, 2025
1 parent a7fe4f8 commit 947aa74
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1367,10 +1367,6 @@ def __init__(
boundaries: Optional[Sequence[float]] = None,
record_min_max: bool = True,
) -> None:
if boundaries is None:
boundaries = (
_DEFAULT_EXPLICIT_BUCKET_HISTOGRAM_AGGREGATION_BOUNDARIES
)
self._boundaries = boundaries
self._record_min_max = record_min_max

Expand All @@ -1391,6 +1387,12 @@ def _create_aggregation(
AggregationTemporality.CUMULATIVE
)

if self._boundaries is None:
self._boundaries = (
instrument._advisory.explicit_bucket_boundaries
or _DEFAULT_EXPLICIT_BUCKET_HISTOGRAM_AGGREGATION_BOUNDARIES
)

return _ExplicitBucketHistogramAggregation(
attributes,
instrument_aggregation_temporality,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from unittest import TestCase

from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics._internal.instrument import Histogram
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
from opentelemetry.sdk.metrics.view import (
ExplicitBucketHistogramAggregation,
Expand Down Expand Up @@ -133,3 +134,33 @@ def test_view_overrides_buckets(self):
self.assertEqual(
metric.data.data_points[0].explicit_bounds, (10.0, 100.0, 1000.0)
)

def test_explicit_aggregation(self):
reader = InMemoryMetricReader(
preferred_aggregation={
Histogram: ExplicitBucketHistogramAggregation()
}
)
meter_provider = MeterProvider(
metric_readers=[reader],
)
meter = meter_provider.get_meter("testmeter")
histogram = meter.create_histogram(
"testhistogram",
explicit_bucket_boundaries_advisory=[1.0, 2.0, 3.0],
)
histogram.record(1, {"label": "value"})
histogram.record(2, {"label": "value"})
histogram.record(3, {"label": "value"})

metrics = reader.get_metrics_data()
self.assertEqual(len(metrics.resource_metrics), 1)
self.assertEqual(len(metrics.resource_metrics[0].scope_metrics), 1)
self.assertEqual(
len(metrics.resource_metrics[0].scope_metrics[0].metrics), 1
)
metric = metrics.resource_metrics[0].scope_metrics[0].metrics[0]
self.assertEqual(metric.name, "testhistogram")
self.assertEqual(
metric.data.data_points[0].explicit_bounds, (1.0, 2.0, 3.0)
)

0 comments on commit 947aa74

Please sign in to comment.