Skip to content

Commit

Permalink
Merge branch 'main' into fossa
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Feb 19, 2025
2 parents 60d6174 + 947aa74 commit fd0fb69
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 fd0fb69

Please sign in to comment.