litellm/tests/unit/integrations/test_prometheus_series_cardinality.py
devin-ai-integration[bot] 6f5ca84f69
feat(prometheus): cap series per metric for every labeled metric (#44420)
* feat(prometheus): cap series per metric for every labeled metric

Add prometheus_metrics_max_series_per_metric: per metric and per worker process, the first N label
sets keep a series of their own. Counters and histograms record every later label set on one series
whose labels are all "other", so totals stay exact, and gauges skip it. The cap holds with multiple
workers because it never needs to remove a series.

Add prometheus_metrics_ttl_seconds: a series idle for that long is removed and its slot is freed.
The prometheus client cannot remove a series in multi-process mode, so the TTL is ignored there with
a startup warning.

Both settings are off by default. The end_user caps are unchanged.

* fix(prometheus): share the series cap across workers of one proxy instance

Workers writing to one PROMETHEUS_MULTIPROC_DIR now agree on which label
sets get a series through an append-only admissions file per metric, so a
merged scrape stays at the cap plus `other` instead of growing with every
worker and every worker restart. The two fallback counters now pass their
label names as a keyword so the cap and prometheus_exclude_labels apply to
them, admission and child creation happen under one lock, the test fixture
restores the shared registry, and the `other` label value lives in
constants.py.

* test(prometheus): check emitted labels instead of wrapper types, close the admission match

The exclude-labels test now emits through the spend and provider budget
metrics and checks the scrape keeps all their labels. The admission match
arms end in assert_never so the match is exhaustive.

* fix(prometheus): return the exhaustive-match fallback so every admission arm returns

* fix(prometheus): pick the series tracker with isinstance so every path of _admits returns

* fix(prometheus): skip an admissions line a worker could only write part of

* fix(prometheus): frame each admissions record with newlines so a cut-off record cannot swallow the next

A record a worker could only write part of used to merge with the next worker's record, and both were skipped for one request. Each record is now written between two newlines, so the fragment is a line of its own. The clock fixture in the series tests starts from a constant instead of reading the real clock

* fix(prometheus): ignore a non-positive series cap or TTL with a warning instead of failing the logger

A cap or TTL of 0 or less raised at logger init. The proxy logs that as a non-blocking error and keeps serving, so the result was a running proxy with no Prometheus metrics at all. The setting is now ignored with a startup warning naming it, the same rule the end_user cap already follows for a non-positive value

* fix(prometheus): start the series cap over on a one-worker restart and audit it live

A proxy with one worker and an operator-set PROMETHEUS_MULTIPROC_DIR now drops litellm's admission files at boot, so a restart frees every slot there the way it already does with several workers. A cap or TTL that is not a number greater than 0 (a bool, a non-numeric string, an empty value) is ignored with the startup warning instead of breaking the logger

The integration cells drive the cap on every endpoint through the OpenAI and Anthropic SDKs and raw httpx, streaming and not, plus gauges, cache hits, failures, both workers of one instance, the TTL on one worker and its warning on two, ignored settings, excluded labels on the fallback counters, a null cap, /config/update, a concurrent burst scraped mid-flight, a provider outage, a killed worker, and restarts with one and two workers

* fix(prometheus): wipe an operator-set multiprocess directory on a one-worker boot too

* fix(prometheus): leave the multiprocess directory alone on a setup-only run

A run with --skip_server_startup starts no worker, so it no longer creates or
wipes PROMETHEUS_MULTIPROC_DIR. Wiping there deleted the samples of a proxy
already running against the same directory

* fix(prometheus): free the capped series slots when a gateway or backend container restarts

The component image entrypoint starts uvicorn without the proxy CLI and wiped only the .db sample files at container start, so the admitted-series files of the previous container survived an in-place restart. Every label set seen after the restart was then counted on `other` once the previous container had filled the cap

* test(prometheus): cover a setup-only run and a gateway image restart under the cap

Two integration cells from the audit: a `--skip_server_startup` run pointed at a live
two-worker proxy's operator directory leaves its samples alone, and the gateway image
(`docker/component_entrypoint.sh` running `python -m gateway.launch`) restarted on a kept
PROMETHEUS_MULTIPROC_DIR starts the cap over. The burst cells now wait for every counter
they assert on, since the request and failure counters of one call increment at different
points of the logging callback

* test(prometheus): prove the cap reaches the fallback counters in the X1 cell

* fix(prometheus): ignore a cleanup interval that is not a number of at least 0

A string or negative prometheus_metrics_cleanup_interval_seconds reached the
series tracker unvalidated, so the first labeled emit with a TTL on raised
TypeError inside the callback and recorded no series. The interval is now
validated the way the cap and the TTL are: an invalid value is ignored with a
warning and the default 60 seconds applies. The I2 integration cell drives a
string interval through a live proxy and reads the warning from its log

* test(prometheus): give the restart cells the boot budget of their siblings

C4 and C5 boot two proxies each and hit the file's 240 s budget on a loaded
box; C3 and D1 already carry 420 s

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-06 01:28:44 +00:00

442 lines
19 KiB
Python

import logging
import re
from pathlib import Path
from threading import Thread
from typing import Final
import pytest
from prometheus_client import REGISTRY, CollectorRegistry, Counter, generate_latest
import litellm
from litellm.constants import PROMETHEUS_ADMITTED_SERIES_FILE_PREFIX
from litellm.integrations.prometheus import PrometheusLogger, _LabeledMetric, prometheus_label_factory
from litellm.integrations.prometheus_helpers import bounded_prometheus_series_tracker
from litellm.integrations.prometheus_helpers.bounded_prometheus_series_tracker import (
BoundedPrometheusSeriesTracker,
PrometheusSeriesLimits,
)
from litellm.integrations.prometheus_helpers.shared_prometheus_series_admissions import (
SharedPrometheusSeriesAdmissions,
)
from litellm.proxy.prometheus_cleanup import wipe_directory
from litellm.types.integrations.prometheus import UserAPIKeyLabelValues
SERIES_SETTINGS: Final = (
"prometheus_metrics_max_series_per_metric",
"prometheus_metrics_ttl_seconds",
"prometheus_metrics_cleanup_interval_seconds",
"prometheus_exclude_labels",
"prometheus_metrics_config",
"enable_end_user_cost_tracking_prometheus_only",
"prometheus_end_user_metrics_max_series_per_metric",
"prometheus_end_user_metrics_ttl_seconds",
)
def _unregister_everything() -> None:
for collector in list(REGISTRY._collector_to_names):
try:
REGISTRY.unregister(collector)
except Exception:
pass
@pytest.fixture(autouse=True)
def isolated_registry_and_settings(monkeypatch):
collectors_before: Final = tuple(REGISTRY._collector_to_names)
_unregister_everything()
monkeypatch.delenv("PROMETHEUS_MULTIPROC_DIR", raising=False)
for setting in SERIES_SETTINGS:
monkeypatch.setattr(litellm, setting, getattr(litellm, setting))
yield
_unregister_everything()
for collector in collectors_before:
REGISTRY.register(collector)
@pytest.fixture
def clock(monkeypatch):
now: Final = [1_000.0]
monkeypatch.setattr(bounded_prometheus_series_tracker.time, "monotonic", lambda: now[0])
return now
def _scraped_series(sample_name: str, registry: CollectorRegistry = REGISTRY) -> frozenset[str]:
exposition: Final = generate_latest(registry).decode()
return frozenset(line for line in exposition.splitlines() if line.startswith(f"{sample_name}{{"))
def _label_values(series: frozenset[str], label: str) -> frozenset[str]:
pattern: Final = re.compile(rf'[{{,]{label}="([^"]*)"')
return frozenset(match.group(1) for match in map(pattern.search, series) if match is not None)
def _sample_value(series: frozenset[str], label: str, value: str) -> float:
(line,) = (line for line in series if f'{label}="{value}"' in line)
return float(line.rsplit(" ", 1)[1])
def _count_request(logger: PrometheusLogger, user_agent: str) -> None:
PrometheusLogger._inc_labeled_counter(
logger,
logger.litellm_proxy_total_requests_metric,
"litellm_proxy_total_requests_metric",
UserAPIKeyLabelValues(user_agent=user_agent),
)
def _observe_latency(logger: PrometheusLogger, user: str) -> None:
labels: Final = prometheus_label_factory(
supported_enum_labels=logger.get_labels_for_metric("litellm_request_total_latency_metric"),
enum_values=UserAPIKeyLabelValues(user=user),
)
logger.litellm_request_total_latency_metric.labels(**labels).observe(0.5)
def test_label_sets_past_the_cap_are_counted_on_one_other_series():
litellm.prometheus_metrics_max_series_per_metric = 2
litellm.prometheus_metrics_ttl_seconds = None
logger: Final = PrometheusLogger()
for identity in ("one", "two", "three", "one", "four"):
_count_request(logger, f"codex/{identity}")
_observe_latency(logger, f"user-{identity}")
counter_series: Final = _scraped_series("litellm_proxy_total_requests_metric_total")
assert _label_values(counter_series, "user_agent") == {"codex/one", "codex/two", "other"}
assert _sample_value(counter_series, "user_agent", "codex/one") == 2
assert _sample_value(counter_series, "user_agent", "codex/two") == 1
assert _sample_value(counter_series, "user_agent", "other") == 2
histogram_series: Final = _scraped_series("litellm_request_total_latency_metric_count")
assert _label_values(histogram_series, "user") == {"user-one", "user-two", "other"}
assert _sample_value(histogram_series, "user", "other") == 2
def test_gauge_label_sets_past_the_cap_are_not_emitted():
litellm.prometheus_metrics_max_series_per_metric = 2
litellm.prometheus_metrics_ttl_seconds = None
logger: Final = PrometheusLogger()
for provider in ("openai", "anthropic", "bedrock", "openai"):
logger.track_provider_remaining_budget(provider=provider, spend=1.0, budget_limit=10.0)
series: Final = _scraped_series("litellm_provider_remaining_budget_metric")
assert _label_values(series, "api_provider") == {"openai", "anthropic"}
def test_series_idle_past_the_ttl_are_removed_and_free_their_slot(clock):
litellm.prometheus_metrics_max_series_per_metric = 2
litellm.prometheus_metrics_ttl_seconds = 10.0
litellm.prometheus_metrics_cleanup_interval_seconds = 0.0
logger: Final = PrometheusLogger()
_count_request(logger, "idle-agent")
clock[0] += 9.0
_count_request(logger, "still-fresh-agent")
clock[0] += 2.0
_count_request(logger, "new-agent")
series: Final = _scraped_series("litellm_proxy_total_requests_metric_total")
assert _label_values(series, "user_agent") == {"still-fresh-agent", "new-agent"}
def test_cap_holds_and_ttl_is_ignored_in_multiprocess_mode(monkeypatch, tmp_path, clock):
monkeypatch.setenv("PROMETHEUS_MULTIPROC_DIR", str(tmp_path))
litellm.prometheus_metrics_max_series_per_metric = 2
litellm.prometheus_metrics_ttl_seconds = 10.0
litellm.prometheus_metrics_cleanup_interval_seconds = 0.0
logger: Final = PrometheusLogger()
_count_request(logger, "first-agent")
_count_request(logger, "second-agent")
clock[0] += 11.0
_count_request(logger, "third-agent")
series: Final = _scraped_series("litellm_proxy_total_requests_metric_total")
assert _label_values(series, "user_agent") == {"first-agent", "second-agent", "other"}
def test_workers_sharing_a_multiprocess_dir_admit_the_same_label_sets(tmp_path: Path):
first_worker: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
second_worker: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
assert first_worker.admit_series("litellm_requests_metric", ("user-a",), max_series=2)
assert second_worker.admit_series("litellm_requests_metric", ("user-b",), max_series=2)
replacement_worker: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
for worker in (first_worker, second_worker, replacement_worker):
assert worker.admit_series("litellm_requests_metric", ("user-a",), max_series=2)
assert worker.admit_series("litellm_requests_metric", ("user-b",), max_series=2)
assert not worker.admit_series("litellm_requests_metric", ("user-c",), max_series=2)
assert first_worker.admit_series("litellm_spend_metric", ("user-c",), max_series=2)
def test_workers_agree_when_racing_appends_overfill_the_admissions_file(tmp_path: Path):
racing_workers: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
for user in ("user-a", "user-b", "user-c"):
assert racing_workers.admit_series("litellm_requests_metric", (user,), max_series=3)
worker: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
assert not worker.admit_series("litellm_requests_metric", ("user-c",), max_series=2)
assert worker.admit_series("litellm_requests_metric", ("user-a",), max_series=2)
assert worker.admit_series("litellm_requests_metric", ("user-b",), max_series=2)
def test_a_line_another_worker_is_still_writing_is_read_once_it_is_complete(tmp_path: Path):
admissions_file: Final = tmp_path / f"{PROMETHEUS_ADMITTED_SERIES_FILE_PREFIX}litellm_requests_metric"
assert SharedPrometheusSeriesAdmissions(directory=str(tmp_path)).admit_series(
"litellm_requests_metric", ("user-a",), max_series=2
)
reader: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
with admissions_file.open("ab") as write_in_progress:
write_in_progress.write(b'["user')
write_in_progress.flush()
assert reader.admit_series("litellm_requests_metric", ("user-a",), max_series=2)
write_in_progress.write(b'-b"]\n')
assert not reader.admit_series("litellm_requests_metric", ("user-c",), max_series=2)
assert reader.admit_series("litellm_requests_metric", ("user-b",), max_series=2)
def test_a_record_cut_short_by_a_full_disk_admits_nothing_and_hides_no_other_record(tmp_path: Path):
admissions_file: Final = tmp_path / f"{PROMETHEUS_ADMITTED_SERIES_FILE_PREFIX}litellm_requests_metric"
admissions_file.write_bytes(b'\n["user-a')
writer: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
reader: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
assert writer.admit_series("litellm_requests_metric", ("user-b",), max_series=2)
assert reader.admit_series("litellm_requests_metric", ("user-b",), max_series=2)
assert reader.admit_series("litellm_requests_metric", ("user-c",), max_series=2)
assert writer.admit_series("litellm_requests_metric", ("user-c",), max_series=2)
assert not writer.admit_series("litellm_requests_metric", ("user-a",), max_series=2)
assert not reader.admit_series("litellm_requests_metric", ("user-a",), max_series=2)
def test_wiping_the_multiprocess_dir_frees_every_admitted_slot(tmp_path: Path):
before_restart: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
assert before_restart.admit_series("litellm_requests_metric", ("user-a",), max_series=1)
wipe_directory(str(tmp_path))
after_restart: Final = SharedPrometheusSeriesAdmissions(directory=str(tmp_path))
assert after_restart.admit_series("litellm_requests_metric", ("user-b",), max_series=1)
assert not after_restart.admit_series("litellm_requests_metric", ("user-a",), max_series=1)
def test_eviction_racing_a_new_series_cannot_leave_it_untracked():
registry: Final = CollectorRegistry()
counter: Final = Counter("requests", "requests", labelnames=("user",), registry=registry)
class _EvictedWhileBeingCreated:
def labels(self, *labelvalues: str):
if labelvalues == ("evicted-user",):
eviction.start()
eviction.join(timeout=0.05)
return counter.labels(*labelvalues)
def remove(self, *labelvalues: str) -> None:
counter.remove(*labelvalues)
labeled: Final = _LabeledMetric(
metric=_EvictedWhileBeingCreated(),
metric_name="requests",
original_labelnames=("user",),
excluded_labels=frozenset(),
tracker=BoundedPrometheusSeriesTracker(),
limits=PrometheusSeriesLimits(max_series=1, ttl_seconds=None, cleanup_interval_seconds=None),
shares_overflow_series=True,
)
eviction: Final = Thread(target=labeled.remove, args=("evicted-user",))
labeled.labels("evicted-user").inc()
eviction.join()
labeled.labels("next-user").inc()
assert _label_values(_scraped_series("requests_total", registry), "user") == {"next-user"}
@pytest.mark.parametrize(
"metric_name", ["litellm_deployment_successful_fallbacks", "litellm_deployment_failed_fallbacks"]
)
def test_cap_applies_to_the_fallback_counters(metric_name: str):
litellm.prometheus_metrics_max_series_per_metric = 2
litellm.prometheus_metrics_ttl_seconds = None
logger: Final = PrometheusLogger()
for index in range(4):
PrometheusLogger._inc_labeled_counter(
logger,
getattr(logger, metric_name),
metric_name,
UserAPIKeyLabelValues(fallback_model=f"model-{index}"),
)
series: Final = _scraped_series(f"{metric_name}_total")
assert _label_values(series, "fallback_model") == {"model-0", "model-1", "other"}
assert _sample_value(series, "fallback_model", "other") == 2
def test_end_user_eviction_keeps_the_series_and_its_slot_in_multiprocess_mode(monkeypatch, tmp_path):
monkeypatch.setenv("PROMETHEUS_MULTIPROC_DIR", str(tmp_path))
litellm.enable_end_user_cost_tracking_prometheus_only = True
litellm.prometheus_metrics_config = [
{"group": "end-user-spend", "metrics": ["litellm_spend_metric"], "include_labels": ["end_user"]}
]
litellm.prometheus_end_user_metrics_max_series_per_metric = 2
litellm.prometheus_end_user_metrics_ttl_seconds = None
litellm.prometheus_metrics_max_series_per_metric = 3
litellm.prometheus_metrics_ttl_seconds = None
logger: Final = PrometheusLogger()
for index in range(5):
PrometheusLogger._inc_labeled_counter(
logger,
logger.litellm_spend_metric,
"litellm_spend_metric",
UserAPIKeyLabelValues(end_user=f"end-user-{index}"),
amount=0.01,
)
series: Final = _scraped_series("litellm_spend_metric_total")
assert _label_values(series, "end_user") == {"end-user-0", "end-user-1", "end-user-2", "other"}
def test_cap_applies_under_a_globally_excluded_label():
litellm.prometheus_exclude_labels = ["hook_type"]
litellm.prometheus_metrics_max_series_per_metric = 2
litellm.prometheus_metrics_ttl_seconds = None
logger: Final = PrometheusLogger()
for index in range(4):
logger._record_guardrail_metrics(
guardrail_name=f"guardrail-{index}",
latency_seconds=0.1,
status="success",
error_type=None,
hook_type="pre_call",
)
series: Final = _scraped_series("litellm_guardrail_requests_total")
assert _label_values(series, "guardrail_name") == {"guardrail-0", "guardrail-1", "other"}
assert _sample_value(series, "guardrail_name", "other") == 2
assert all("hook_type" not in line for line in series)
def test_end_user_eviction_frees_a_slot_under_the_cap():
litellm.enable_end_user_cost_tracking_prometheus_only = True
litellm.prometheus_metrics_config = [
{"group": "end-user-spend", "metrics": ["litellm_spend_metric"], "include_labels": ["end_user"]}
]
litellm.prometheus_end_user_metrics_max_series_per_metric = 2
litellm.prometheus_end_user_metrics_ttl_seconds = None
litellm.prometheus_metrics_max_series_per_metric = 3
litellm.prometheus_metrics_ttl_seconds = None
logger: Final = PrometheusLogger()
for index in range(5):
PrometheusLogger._inc_labeled_counter(
logger,
logger.litellm_spend_metric,
"litellm_spend_metric",
UserAPIKeyLabelValues(end_user=f"end-user-{index}"),
amount=0.01,
)
series: Final = _scraped_series("litellm_spend_metric_total")
assert _label_values(series, "end_user") == {"end-user-3", "end-user-4"}
def test_series_stay_unbounded_unless_a_limit_is_configured():
litellm.prometheus_metrics_max_series_per_metric = None
litellm.prometheus_metrics_ttl_seconds = None
logger: Final = PrometheusLogger()
for index in range(5):
_count_request(logger, f"agent-{index}")
series: Final = _scraped_series("litellm_proxy_total_requests_metric_total")
assert _label_values(series, "user_agent") == {f"agent-{index}" for index in range(5)}
@pytest.mark.parametrize(
("setting", "value"),
[
("prometheus_metrics_max_series_per_metric", 0),
("prometheus_metrics_max_series_per_metric", -5),
("prometheus_metrics_ttl_seconds", 0.0),
("prometheus_metrics_ttl_seconds", -1.0),
("prometheus_metrics_max_series_per_metric", "five"),
("prometheus_metrics_max_series_per_metric", True),
("prometheus_metrics_max_series_per_metric", 2.5),
("prometheus_metrics_ttl_seconds", ""),
],
)
def test_a_series_limit_that_is_not_a_positive_number_is_ignored_with_a_warning_and_metrics_keep_flowing(
setting: str, value: object, clock, caplog
):
litellm.prometheus_metrics_cleanup_interval_seconds = 0.0
setattr(litellm, setting, value)
with caplog.at_level(logging.WARNING, logger="LiteLLM"):
logger: Final = PrometheusLogger()
for index in range(3):
_count_request(logger, f"agent-{index}")
clock[0] += 100.0
series: Final = _scraped_series("litellm_proxy_total_requests_metric_total")
assert _label_values(series, "user_agent") == {"agent-0", "agent-1", "agent-2"}
assert setting in caplog.text
@pytest.mark.parametrize("value", ["sixty", -1, True, ""])
def test_a_cleanup_interval_that_is_not_a_number_of_at_least_zero_falls_back_to_the_default_with_a_warning(
value: object, monkeypatch, clock, caplog
):
monkeypatch.setattr(litellm, "prometheus_metrics_max_series_per_metric", 3)
monkeypatch.setattr(litellm, "prometheus_metrics_ttl_seconds", 10.0)
monkeypatch.setattr(litellm, "prometheus_metrics_cleanup_interval_seconds", value)
with caplog.at_level(logging.WARNING, logger="LiteLLM"):
logger: Final = PrometheusLogger()
_count_request(logger, "agent-0")
clock[0] += 30.0
_count_request(logger, "agent-1")
within_the_interval: Final = _scraped_series("litellm_proxy_total_requests_metric_total")
clock[0] += 31.0
_count_request(logger, "agent-2")
assert "prometheus_metrics_cleanup_interval_seconds" in caplog.text
assert _label_values(within_the_interval, "user_agent") == {"agent-0", "agent-1"}
series: Final = _scraped_series("litellm_proxy_total_requests_metric_total")
assert _label_values(series, "user_agent") == {"agent-2"}
def test_a_cleanup_interval_written_as_a_numeric_string_is_honored(monkeypatch, clock, caplog):
monkeypatch.setattr(litellm, "prometheus_metrics_max_series_per_metric", 3)
monkeypatch.setattr(litellm, "prometheus_metrics_ttl_seconds", 10.0)
monkeypatch.setattr(litellm, "prometheus_metrics_cleanup_interval_seconds", "0")
with caplog.at_level(logging.WARNING, logger="LiteLLM"):
logger: Final = PrometheusLogger()
_count_request(logger, "agent-0")
clock[0] += 30.0
_count_request(logger, "agent-1")
series: Final = _scraped_series("litellm_proxy_total_requests_metric_total")
assert _label_values(series, "user_agent") == {"agent-1"}
assert "prometheus_metrics_cleanup_interval_seconds" not in caplog.text
def test_a_series_cap_written_as_a_numeric_string_is_honored(caplog):
litellm.prometheus_metrics_max_series_per_metric = "2"
with caplog.at_level(logging.WARNING, logger="LiteLLM"):
logger: Final = PrometheusLogger()
for index in range(3):
_count_request(logger, f"agent-{index}")
series: Final = _scraped_series("litellm_proxy_total_requests_metric_total")
assert _label_values(series, "user_agent") == {"agent-0", "agent-1", "other"}
assert "prometheus_metrics_max_series_per_metric" not in caplog.text