From 5452c2fb3073c89e2f418f8b83b39a72acef9a6d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 14 May 2026 22:49:11 +0000 Subject: [PATCH] test(prometheus): clear cached metric labels after toggling rate-limit flag The PrometheusLogger caches each metric's label set at construction time so that labels used at counter.labels(...) time stay consistent with the labels the metric was registered with. The enterprise async_post_call_failure_hook test toggles litellm.prometheus_emit_rate_limit_labels = True AFTER the fixture has already built the logger, so without invalidating the cache the rate_limit_category / rate_limit_type labels never reach the mocked counter and the assert_called_once_with check fails. Co-authored-by: Yassin Kortam --- .../test_prometheus_logging_callbacks.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/enterprise/litellm_enterprise/enterprise_callbacks/test_prometheus_logging_callbacks.py b/tests/enterprise/litellm_enterprise/enterprise_callbacks/test_prometheus_logging_callbacks.py index 7878e7bddb4..1dd9f69351d 100644 --- a/tests/enterprise/litellm_enterprise/enterprise_callbacks/test_prometheus_logging_callbacks.py +++ b/tests/enterprise/litellm_enterprise/enterprise_callbacks/test_prometheus_logging_callbacks.py @@ -785,8 +785,13 @@ async def test_async_post_call_failure_hook(prometheus_logger): """ # Opt into the unified rate-limit labels so this test exercises the # full label set surfaced when `prometheus_emit_rate_limit_labels` is on. + # The logger caches each metric's label set at construction time (so the + # labels passed to ``counter.labels(...)`` stay in lock step with the + # labels used to register the metric), so we must invalidate the cache + # after flipping the toggle for the cache to pick up the new label set. original_emit = litellm.prometheus_emit_rate_limit_labels litellm.prometheus_emit_rate_limit_labels = True + prometheus_logger._cached_metric_labels.clear() # Mock the prometheus metrics prometheus_logger.litellm_proxy_failed_requests_metric = MagicMock() @@ -840,6 +845,7 @@ async def test_async_post_call_failure_hook(prometheus_logger): ) finally: litellm.prometheus_emit_rate_limit_labels = original_emit + prometheus_logger._cached_metric_labels.clear() prometheus_logger.litellm_proxy_failed_requests_metric.labels().inc.assert_called_once() # Assert total requests metric was incremented with correct labels