litellm/tests/unit/accounting/test_metrics.py
joshua-berri 85bc58085e
fix(accounting): keep spend commits working when prometheus_client is not installed (#50230)
* fix(accounting): keep spend commits working when prometheus_client is not installed

The delivery worker imported the ledger metrics module, which imported
prometheus_client at module level. update_database imports the worker
outside its try block, so installs without the proxy-runtime extra failed
every spend commit. The metrics now register only when prometheus_client
is importable and are skipped otherwise

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style(accounting): note why prometheus_client can be missing

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 10:35:26 -08:00

49 lines
1.7 KiB
Python

import importlib.util
import sys
import pytest
from prometheus_client.metrics import MetricWrapperBase
from litellm.accounting import metrics
from litellm.accounting.metrics import record_deliveries
from litellm.repositories.accounting_delivery_repository import DeliveryMetrics
_DELIVERY_METRICS = DeliveryMetrics(
pending_count=4, parked_count=1, oldest_pending_seconds=-3.0, batch_count=7, table_bytes=8192
)
def _sample(metric: MetricWrapperBase, suffix: str = "") -> float:
family = metric.collect()[0]
return next(sample.value for sample in family.samples if sample.name == family.name + suffix)
def test_delivery_metrics_publish_backlog_and_clamp_clock_skew() -> None:
instruments = metrics._INSTRUMENTS
assert instruments is not None
batches_before = _sample(instruments.batch_time, "_count")
record_deliveries(_DELIVERY_METRICS, 0.5)
assert (
_sample(instruments.pending),
_sample(instruments.parked),
_sample(instruments.oldest),
_sample(instruments.batch_count),
_sample(instruments.table_bytes),
) == (4.0, 1.0, 0.0, 7.0, 8192.0)
assert _sample(instruments.batch_time, "_count") == batches_before + 1
def test_delivery_metrics_are_skipped_when_prometheus_client_is_not_installed(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setitem(sys.modules, "prometheus_client", None)
spec = importlib.util.spec_from_file_location("accounting_metrics_without_prometheus", metrics.__file__)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
monkeypatch.setitem(sys.modules, spec.name, module)
spec.loader.exec_module(module)
assert module.record_deliveries(_DELIVERY_METRICS, 0.5) is None