fix(proxy): point /metrics 401 at the opt-out flag#27505
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 cherry-pick onto
Confidence Score: 5/5Safe to merge — the change only extends the 401 error message string; no auth logic, routing, or middleware control flow is altered. The diff is minimal (one f-string extended, one new test added). Auth still runs and rejects invalid credentials identically; the only observable difference is operators now see the opt-out flag in the 401 body. Existing tests are untouched and the new test verifies the hint is present. The new test assertion in
|
| Filename | Overview |
|---|---|
| litellm/proxy/middleware/prometheus_auth_middleware.py | Appends an opt-out discovery hint to the 401 error body; auth logic is unchanged. |
| tests/test_litellm/proxy/middleware/test_prometheus_auth_middleware.py | Adds test_invalid_auth_metrics_includes_optout_hint; the "false" in response.text assertion is slightly weaker than checking the full require_auth_for_metrics_endpoint: false substring. |
Reviews (1): Last reviewed commit: "fix(proxy): point /metrics 401 at the op..." | Re-trigger Greptile
| assert "require_auth_for_metrics_endpoint" in response.text | ||
| assert "false" in response.text |
There was a problem hiding this comment.
The
assert "false" in response.text assertion is overly broad — it would also pass if the inner error message or any other part of the JSON body happened to contain the word "false". Checking for the full key-value substring is more precise and will catch regressions if the hint wording changes.
| assert "require_auth_for_metrics_endpoint" in response.text | |
| assert "false" in response.text | |
| assert "require_auth_for_metrics_endpoint" in response.text | |
| assert "require_auth_for_metrics_endpoint: false" in response.text |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Summary
Cherry-pick of #27502 onto
litellm_1.84.0rc2. rc2 already shipsrequire_auth_for_metrics_endpoint=True(via the staging→main promote in #27245), so operators upgrading to this rc with existing Prometheus scrapers hit401 "Malformed API Key passed in. Ensure Key has 'Bearer ' prefix."with no migration hint.This change appends the opt-out discovery hint to the existing 401 body in
prometheus_auth_middleware.py— auth still runs, still rejects bad/missing credentials, the legacy opt-out flag still bypasses cleanly. Just makes the response operators actually see point at the YAML they need.Post-fix body:
Test plan
origin/litellm_1.84.0rc2uv run pytest tests/test_litellm/proxy/middleware/test_prometheus_auth_middleware.py tests/test_litellm/proxy/middleware/test_prometheus_auth_middleware_asgi.py -vagainst rc2 worktree — 11 passed (including newtest_invalid_auth_metrics_includes_optout_hint)