refactor(prometheus): port the team gauges to the current label wrapper

Rebasing onto main crossed three upstream renames and one silent merge
hazard:

- `_ExcludedLabelMetric` is now `_LabeledMetric`, and its constructor takes
  the series tracker, limits and overflow flag as well; the two wrapper
  tests construct it the way main's own tests do.
- `_PROXY_MaxParallelRequestsHandler_v3` is now public
  `PROXY_MaxParallelRequestsHandler_v3`, with no alias left behind.
- The merge appended this branch's old `_LabeledMetric.remove` after main's,
  shadowing it. Main's version does the series-cap bookkeeping, so the
  override silently broke eviction -- three tests in
  test_prometheus_series_cardinality.py failed. Main's `remove` already
  drops excluded labels via `_kept_values`, so the override is deleted
  rather than reconciled.

Drops `test_excluded_label_wrapper_cannot_remove_when_every_label_is_excluded`:
it pinned the old override's inert behaviour, main's `remove` raises instead,
and the team gauges cannot reach that path now that they require both the
team and model labels before emitting.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
DanBrima 2026-10-26 11:34:27 +02:00
parent 770e51974f
commit fa885f0f6a
2 changed files with 13 additions and 29 deletions

View file

@ -254,13 +254,6 @@ class _LabeledMetric:
str(value) for name, value in zip(self._original_labelnames, values) if name not in self._excluded_labels
)
def remove(self, *labelvalues: str) -> None:
kept_values: Final = tuple(
value for name, value in zip(self._original_labelnames, labelvalues) if name not in self._excluded_labels
)
if kept_values:
self._metric.remove(*kept_values)
_MetricLike: TypeAlias = "NoOpMetric | _LabeledMetric | MetricWrapperBase"

View file

@ -20,12 +20,13 @@ import pytest
from prometheus_client import CollectorRegistry, Gauge
from litellm.integrations.prometheus import PrometheusLogger, _ExcludedLabelMetric
from litellm.integrations.prometheus import PrometheusLogger, _LabeledMetric
from litellm.integrations.prometheus_helpers.bounded_prometheus_series_tracker import (
BoundedPrometheusSeriesTracker,
PrometheusSeriesLimits,
)
from litellm.proxy.hooks.parallel_request_limiter_v3 import (
_PROXY_MaxParallelRequestsHandler_v3,
PROXY_MaxParallelRequestsHandler_v3,
)
from litellm.types.integrations.prometheus import (
DEFINED_PROMETHEUS_METRICS,
@ -278,7 +279,7 @@ def test_limiter_publishes_team_headers_in_the_shape_the_gauges_read():
limiter builds from ``descriptor_key`` + ``rate_limit_type``, so a change
to that format would otherwise silently stop the team series.
"""
headers = _PROXY_MaxParallelRequestsHandler_v3._merge_ratelimit_statuses_into_additional_headers(
headers = PROXY_MaxParallelRequestsHandler_v3._merge_ratelimit_statuses_into_additional_headers(
additional_headers={},
statuses=[
{
@ -341,7 +342,15 @@ def test_removing_a_never_emitted_real_series_raises_nothing():
def test_excluded_label_wrapper_sets_and_removes_using_the_kept_labels():
registry = CollectorRegistry()
real = Gauge("litellm_team_rpm_limit", "doc", labelnames=["team", "model"], registry=registry)
wrapper = _ExcludedLabelMetric(real, ORIGINAL_LABELNAMES, frozenset({"team_alias"}))
wrapper = _LabeledMetric(
metric=real,
metric_name="litellm_team_rpm_limit",
original_labelnames=ORIGINAL_LABELNAMES,
excluded_labels=frozenset({"team_alias"}),
tracker=BoundedPrometheusSeriesTracker(),
limits=PrometheusSeriesLimits(max_series=None, ttl_seconds=None, cleanup_interval_seconds=None),
shares_overflow_series=False,
)
kept = {"team": "team-abc", "model": "gpt-4o-mini"}
wrapper.labels(**TEAM_LABELS).set(60)
@ -351,24 +360,6 @@ def test_excluded_label_wrapper_sets_and_removes_using_the_kept_labels():
assert registry.get_sample_value("litellm_team_rpm_limit", kept) is None
def test_excluded_label_wrapper_cannot_remove_when_every_label_is_excluded():
"""
With every label excluded the gauge collapses to a single unlabeled sample,
which has no child series for prometheus_client to remove. Such a metric
cannot represent per-team state at all, so there is no correct value to
drop it to; this pins the behaviour rather than papering over it.
"""
registry = CollectorRegistry()
real = Gauge("litellm_team_rpm_limit", "doc", registry=registry)
wrapper = _ExcludedLabelMetric(real, ORIGINAL_LABELNAMES, frozenset(ORIGINAL_LABELNAMES))
wrapper.labels(**TEAM_LABELS).set(60)
assert registry.get_sample_value("litellm_team_rpm_limit", {}) == 60
wrapper.remove(*(TEAM_LABELS[name] for name in ORIGINAL_LABELNAMES))
assert registry.get_sample_value("litellm_team_rpm_limit", {}) == 60
def test_noop_metric_remove_is_inert():
"""A disabled metric answers every call without recording or raising."""
metric = NoOpMetric()