litellm/tests/unit/telemetry/test_histogram.py
devin-ai-integration[bot] 596c51a5fa
feat(telemetry): add AggregatingSink, fixed-bucket histograms, Report and HttpExporter (#45487)
* feat(telemetry): add AggregatingSink, fixed-bucket histograms, Report and HttpExporter

* fix(telemetry): add records to rows in place, guard concurrent flushes, cap restored rows and count bad histogram values

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(telemetry): give the shared row helper its lint reasons

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(telemetry): retry the same statuses as the proxy's should_retry (408, 409, 429, 5xx)

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(telemetry): cover populated block_types and header_keys in report JSON

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: nate <nate@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-13 18:33:01 -07:00

43 lines
1.3 KiB
Python

import math
from typing import Final
import pytest
from litellm.telemetry.histogram import Histogram
_BOUNDS: Final = (10.0, 100.0)
def _histogram_of(*values: float | None) -> Histogram:
histogram: Final = Histogram.empty(_BOUNDS)
for value in values:
histogram.add(value)
return histogram
@pytest.mark.parametrize(
("value", "expected_counts"),
[
(None, [0, 0, 0]),
(0.0, [1, 0, 0]),
(10.0, [1, 0, 0]),
(10.5, [0, 1, 0]),
(100.0, [0, 1, 0]),
(100.5, [0, 0, 1]),
],
)
def test_a_value_lands_in_the_first_bucket_whose_bound_it_does_not_exceed(
value: float | None, expected_counts: list[int]
) -> None:
assert _histogram_of(value) == Histogram(bounds=_BOUNDS, counts=expected_counts, invalid=0)
@pytest.mark.parametrize("value", [-1.0, math.nan, math.inf, -math.inf])
def test_a_negative_or_non_finite_value_is_counted_as_invalid_instead_of_bucketed(value: float) -> None:
assert _histogram_of(value, 5.0) == Histogram(bounds=_BOUNDS, counts=[1, 0, 0], invalid=1)
def test_add_histogram_adds_bucket_and_invalid_counts() -> None:
total: Final = _histogram_of(5.0, -1.0)
total.add_histogram(_histogram_of(5.0, 500.0, math.nan))
assert total == Histogram(bounds=_BOUNDS, counts=[2, 0, 1], invalid=2)