From 38c2875b10c42edca109ab26421ccf9223c364ed Mon Sep 17 00:00:00 2001 From: DanBrima <40828002+DanBrima@users.noreply.github.com> Date: Tue, 18 Aug 2026 05:43:15 +0000 Subject: [PATCH] test(prometheus): pin that team gauges are never label-exclusion wrapped Alias cleanup reads the gauge's existing children through collect(), which the label-exclusion wrapper does not expose. That wrapper is unreachable for these metrics: get_labels_for_metric strips excluded labels before the labelnames reach the metric factory, and the factory only wraps when the labelnames it receives still intersect exclude_labels. The reasoning spans two distant pieces of code, so pin the invariant with a test rather than leaving a future refactor free to break the link. --- ...test_prometheus_team_rate_limit_metrics.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_litellm/integrations/test_prometheus_team_rate_limit_metrics.py b/tests/test_litellm/integrations/test_prometheus_team_rate_limit_metrics.py index 041ad02c1c1..0a59b720133 100644 --- a/tests/test_litellm/integrations/test_prometheus_team_rate_limit_metrics.py +++ b/tests/test_litellm/integrations/test_prometheus_team_rate_limit_metrics.py @@ -422,3 +422,23 @@ def test_emits_nothing_when_the_team_label_is_excluded(): for metric_name in TEAM_RATE_LIMIT_METRICS: getattr(logger, metric_name).labels.assert_not_called() getattr(logger, metric_name).remove.assert_not_called() + + +def test_excluded_labels_never_reach_team_gauge_labelnames(): + """ + `exclude_labels` is applied inside `get_labels_for_metric`, so the + labelnames a team gauge is constructed with never contain an excluded + label. The factory only wraps a metric when its labelnames still intersect + `exclude_labels`, so these gauges are always real prometheus_client + Gauges and always expose `collect` for alias cleanup. + """ + with patch("litellm.integrations.prometheus.PrometheusLogger.__init__", return_value=None): + logger = PrometheusLogger() + logger.exclude_labels = frozenset({"model", "team_alias"}) + logger.label_filters = {} + logger._cached_metric_labels = {} + + for metric_name in TEAM_RATE_LIMIT_METRICS: + labelnames = logger.get_labels_for_metric(metric_name) + assert not frozenset(labelnames) & logger.exclude_labels + assert "team" in labelnames