Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/5340.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`opentelemetry-sdk`: raise `ValueError` when `ExplicitBucketHistogramAggregation` boundaries are not strictly increasing or finite
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from enum import IntEnum
from functools import partial
from logging import getLogger
from math import inf
from math import inf, isnan
from threading import Lock
from typing import (
Generic,
Expand Down Expand Up @@ -464,6 +464,12 @@ def __init__(
boundaries = (
_DEFAULT_EXPLICIT_BUCKET_HISTOGRAM_AGGREGATION_BOUNDARIES
)
if boundaries:
for i, x in enumerate(boundaries):
if not math.isfinite(x):
raise ValueError(f"invalid boundary: {x!r}")
if i and boundaries[i - 1] >= x:
raise ValueError("boundaries must be strictly increasing")
super().__init__(
attributes,
reservoir_builder=partial(
Expand Down
60 changes: 60 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,66 @@ def test_create_aggregation_on_instrument_without_boundaries(self):
)
self.assertIsInstance(result, _ExplicitBucketHistogramAggregation)

def test_unsorted_boundaries_raise(self):
with self.assertRaises(ValueError):
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
boundaries=[100, 10, 50],
)

def test_duplicate_boundaries_raise(self):
with self.assertRaises(ValueError):
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
boundaries=[10, 50, 50, 100],
)

def test_nan_boundary_raises(self):
with self.assertRaises(ValueError):
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
boundaries=[10, float("nan"), 100],
)

def test_inf_boundary_raises(self):
with self.assertRaises(ValueError):
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
boundaries=[10, 50, float("inf")],
)

def test_negative_inf_boundary_raises(self):
with self.assertRaises(ValueError):
_ExplicitBucketHistogramAggregation(
Mock(),
AggregationTemporality.DELTA,
0,
_default_reservoir_factory(
_ExplicitBucketHistogramAggregation
),
boundaries=[float("-inf"), 50, 100],
)


class TestAggregationFactory(TestCase):
def test_sum_factory(self):
Expand Down