fix(observability): publish the ceiling when the logger is built lazily

`success_callback: ["prometheus"]` is the registration the docs show, and it
builds the logger on the first request. Startup had already published the
ceiling by then, with no logger to publish to, so the gauge registered at its
zero default and stayed there. A proxy serving every request reported a ceiling
of zero, which is the reading `+Inf` was introduced to prevent.

Reproduced on a live proxy with that config and a limit of 7 enforced: the gauge
read 0.0 after a successful request. With the ceiling restored on construction
it reads 7.0, and an unconfigured proxy still reads +Inf.

Set on the instance rather than through the module helper, since during
`__init__` the logger is not yet reachable by the lookup that helper uses.
This commit is contained in:
Yucheng Zhu 2026-08-19 16:45:01 -07:00
parent f9d4ef8e6f
commit b673d834a0
2 changed files with 53 additions and 0 deletions

View file

@ -885,6 +885,30 @@ class PrometheusLogger(CustomLogger):
print_verbose(f"Got exception on init prometheus client {e}")
raise e
self._restore_effective_concurrency_ceiling()
def _restore_effective_concurrency_ceiling(self) -> None:
"""Set the ceiling gauge to the value already in force.
Startup publishes the ceiling, but ``success_callback: ["prometheus"]``
builds this logger lazily on the first request, long after that ran with
no logger to publish to. The gauge would then register at its zero
default and stay there, reading as "no requests allowed" on a proxy
serving everything, which is the reading ``+Inf`` exists to prevent.
Set directly rather than through the module helper, because during
``__init__`` this instance is not yet reachable by the logger lookup.
"""
try:
from litellm.proxy.common_utils.request_pressure_metrics import effective_global_limit
from litellm.proxy.proxy_server import general_settings
self.set_global_max_parallel_requests_limit(
effective_global_limit(general_settings.get("global_max_parallel_requests"))
)
except Exception as e: # noqa: BLE001 # a logger that cannot read proxy config must still start
print_verbose(f"could not publish the concurrency ceiling on init: {e}")
def _parse_prometheus_config(self) -> dict[str, list[str]]:
"""Parse prometheus metrics configuration for label filtering and enabled metrics"""
import litellm

View file

@ -104,3 +104,32 @@ def test_publishing_never_blocks_startup():
),
):
publish_global_max_parallel_requests(3)
def test_a_lazily_built_logger_publishes_the_ceiling_already_in_force(monkeypatch):
"""success_callback: ["prometheus"] builds the logger on the first request,
long after startup published the ceiling with no logger to publish to. The
gauge would register at its zero default and stay there, reading as "no
requests allowed" on a proxy serving everything."""
from prometheus_client import REGISTRY
import litellm
import litellm.proxy.proxy_server as proxy_server
from litellm.integrations.prometheus import PrometheusLogger
monkeypatch.setattr(litellm, "callbacks", [])
monkeypatch.setattr(litellm, "success_callback", [])
monkeypatch.setitem(proxy_server.general_settings, "global_max_parallel_requests", 7)
monkeypatch.setattr(
"litellm.proxy.common_utils.request_pressure_metrics.is_global_limit_enforced",
lambda: True,
)
for collector in list(REGISTRY._collector_to_names.keys()):
try:
REGISTRY.unregister(collector)
except Exception:
pass
PrometheusLogger()
assert REGISTRY.get_sample_value("litellm_global_max_parallel_requests_limit") == 7.0