fix(proxy): point /metrics 401 at the opt-out flag - #27502
Conversation
Operators upgrading past 35bbca6 (which made /metrics auth default-on) see "Malformed API Key passed in. Ensure Key has 'Bearer ' prefix." with no hint that litellm_settings.require_auth_for_metrics_endpoint: false restores the previous unauthenticated behavior. Append that discovery hint to the existing 401 body so a Prometheus scraper that breaks after upgrade has a clear migration path. No behavior change.
Greptile SummaryThis PR improves the
Confidence Score: 5/5Safe to merge — the only change is a string appended to an existing error message with no effect on auth logic or behavior. The change touches exactly one string literal in the error path and adds a targeted mock test that confirms the hint text is present. Auth still runs, the 401 status code is unchanged, and the opt-out bypass path is untouched. No regressions in any of the existing tests, and no new code paths are introduced. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/proxy/middleware/prometheus_auth_middleware.py | Appends opt-out hint to the 401 body on auth failure; no logic or behavior change |
| tests/test_litellm/proxy/middleware/test_prometheus_auth_middleware.py | Adds test_invalid_auth_metrics_includes_optout_hint to assert the new hint text appears in the 401 body; uses mocks only, no network calls |
Reviews (1): Last reviewed commit: "fix(proxy): point /metrics 401 at the op..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…r-2bbbc8 fix(proxy): point /metrics 401 at the opt-out flag
Summary
35bbca60b0(which made/metricsauth default-on) hit401 "Malformed API Key passed in. Ensure Key has 'Bearer ' prefix."from any Prometheus scraper that doesn't send credentials, with no hint that the legacy public behavior is one YAML line away.litellm_settings.require_auth_for_metrics_endpoint: falsediscovery hint to the existing 401 body inprometheus_auth_middleware.py. No behavior change — auth still runs, still rejects bad/missing credentials, the legacy opt-out flag still bypasses cleanly. Just a clear migration path in the response operators actually see.Post-fix body:
Test plan
uv run pytest tests/test_litellm/proxy/middleware/test_prometheus_auth_middleware.py tests/test_litellm/proxy/middleware/test_prometheus_auth_middleware_asgi.py -v— 11 passed (including newtest_invalid_auth_metrics_includes_optout_hint)GET /metricswith no auth → 401 body now contains the opt-out hintGET /metricswith validBearer sk-1234→ auth still passesGET /metricswith invalid Bearer → 401 (auth still rejects), body now contains the opt-out hintGET /metricswithrequire_auth_for_metrics_endpoint: false→ auth bypass still worksGET /health/liveliness(unrelated public endpoint) → still 200