Skip to content

fix(gateway): keep prometheus /metrics mount when trimming gateway routes - #30297

Closed
manan-tech wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
manan-tech:litellm_fix_gateway_metrics_mount
Closed

manan-tech wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
manan-tech:litellm_fix_gateway_metrics_mount

Conversation

@manan-tech

@manan-tech manan-tech commented Jun 12, 2026 •

Copy link
Copy Markdown

Relevant issues

Fixes #30291

Pre-Submission checklist

  • I have added meaningful tests
  • My PR passes all unit tests
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Screenshots / Proof of Fix

Gateway started with the componentized entrypoint and prometheus enabled in the config:

litellm_settings:
  success_callback: ["prometheus"]
  require_auth_for_metrics_endpoint: false
WORKER_CONFIG=config.yaml LITELLM_MASTER_KEY=sk-1234 python -m uvicorn gateway.main:app --host 127.0.0.1 --port 4000

Before this change the gateway is healthy but /metrics is gone:

$ curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:4000/health/liveliness
200
$ curl -s -w "\nHTTP %{http_code}\n" http://127.0.0.1:4000/metrics
{"detail":"Not Found"}
HTTP 404

After this change the same commands serve the Prometheus exposition:

$ curl -sL http://127.0.0.1:4000/metrics | head -4
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 1981.0
python_gc_objects_collected_total{generation="1"} 536.0
$ curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:4000/metrics/
200

The trim still drops everything it is supposed to drop on the same run:

$ curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:4000/ui/
404
$ curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer sk-1234" http://127.0.0.1:4000/key/list
404

The 307 on the bare /metrics path is the standard Starlette Mount trailing-slash redirect and matches what the monolithic entrypoint does on the same version; that behavior is tracked separately in #30079

Type

🐛 Bug Fix

Changes

_is_gateway_route rejected every Mount before the allowlist check, which kept UI static mounts off the gateway. Prometheus registers /metrics as app.mount("/metrics", make_asgi_app()), a Mount rather than an APIRoute, so the lifespan trim removed it and scrapes got 404 on gateway.main even with /metrics listed in gateway/routes/allowlist.py.

This mirrors the BACKEND_MOUNT_PATHS allowlist the backend component already uses for its kept Mounts: a new GATEWAY_MOUNT_PATHS frozenset in gateway/routes/allowlist.py holds /metrics, and _is_gateway_route keeps a Mount only when its path is in that set. The UI static mounts and the MCP catch-all Mount("/") stay dropped.

tests/test_litellm/proxy/test_component_allowlists.py gains gateway Mount coverage in the same shape as the existing backend Mount tests: the GATEWAY_MOUNT_PATHS constant is validated, and _is_gateway_route is asserted to keep the /metrics Mount while dropping the UI and MCP mounts. The import of gateway.main saves and restores the shared app's lifespan_context so the module's lifespan wrapper does not leak into sibling tests. The /metrics predicate test fails on the previous drop-all-Mounts behavior and passes with this change

@manan-tech

Copy link
Copy Markdown
Author

@greptileai

@greptile-apps

greptile-apps Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug where the gateway's lifespan route-trim was dropping the Prometheus /metrics ASGI Mount because _is_gateway_route previously returned false for every Mount unconditionally, before the allowlist check was reached. The fix introduces a GATEWAY_MOUNT_PATHS frozenset (mirroring BACKEND_MOUNT_PATHS) and updates _is_gateway_route to keep Mount instances whose path is in that set.

  • gateway/routes/allowlist.py: Adds GATEWAY_MOUNT_PATHS = frozenset({"/metrics"}) alongside the existing exact-path and prefix allowlists.
  • gateway/main.py: Changes the Mount branch in _is_gateway_route from an unconditional return False to return path in GATEWAY_MOUNT_PATHS, so only the Prometheus Mount survives while UI static mounts and the MCP catch-all are still dropped.
  • tests/test_litellm/proxy/test_component_allowlists.py: Adds four new tests covering constant existence, /metrics membership, the kept-metrics regression, and the dropped-non-allowlisted-mounts case; imports _is_gateway_route with an explicit lifespan save/restore to prevent test pollution.

Confidence Score: 5/5

The change is a targeted, well-tested fix to the gateway route-trimming logic with no impact on the monolithic entrypoint or any production auth path.

The fix is minimal and additive: one new frozenset, a one-line predicate change, and four focused regression tests. It mirrors an already-proven pattern (BACKEND_MOUNT_PATHS) and explicitly preserves the previous drop-everything-else behavior for all non-allowlisted Mounts. No auth, data-plane, or database-access paths are touched.

No files require special attention.

Important Files Changed

Filename Overview
gateway/routes/allowlist.py Adds GATEWAY_MOUNT_PATHS frozenset containing "/metrics", mirroring the existing BACKEND_MOUNT_PATHS pattern for preserving the Prometheus scrape endpoint on the gateway.
gateway/main.py Updates _is_gateway_route to keep Mount instances whose path is in GATEWAY_MOUNT_PATHS instead of unconditionally dropping all Mounts; fixes the /metrics 404 regression on the gateway entrypoint.
tests/test_litellm/proxy/test_component_allowlists.py Adds gateway Mount coverage: imports _is_gateway_route with a save/restore of the lifespan context, then asserts GATEWAY_MOUNT_PATHS contents and _is_gateway_route behaviour for Prometheus and non-allowlisted Mounts.

Reviews (3): Last reviewed commit: "fix(gateway): keep prometheus /metrics m..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug where the gateway's _is_gateway_route predicate unconditionally dropped every Mount-type route, causing Prometheus /metrics (registered via make_asgi_app() as a Mount, not an APIRoute) to return 404 on the componentized gateway.main entrypoint.

  • gateway/main.py: _is_gateway_route now returns path == "/metrics" for Mount objects instead of always False, with an explicit comment explaining why an exact match is needed rather than reusing the prefix allowlist (to avoid inadvertently keeping the catch-all MCP Mount("/")).
  • tests/test_litellm/proxy/test_component_allowlists.py: Two regression tests are added — one asserting the /metrics Mount survives the filter, one asserting UI/MCP mounts still don't. The import of gateway.main is guarded by a lifespan save-and-restore to prevent leaking the gateway's lifespan wrapper into sibling tests.

Confidence Score: 4/5

Safe to merge — the change is surgical (one line in the predicate), the fix is well-reasoned, and the PR ships dedicated regression tests that would have caught the original bug.

The predicate change is correct and the MCP catch-all concern is explicitly handled. The only finding is a stale docstring on _component_paths that no longer accurately describes its relationship to _is_gateway_route for Mount routes.

The _component_paths helper docstring in tests/test_litellm/proxy/test_component_allowlists.py warrants a small update to avoid misleading future contributors about Mount coverage.

Important Files Changed

Filename Overview
gateway/main.py Single-line predicate fix: _is_gateway_route now returns path == "/metrics" for Mount objects instead of unconditionally False, restoring Prometheus scrape access on the gateway entrypoint.
tests/test_litellm/proxy/test_component_allowlists.py Adds two focused regression tests for the predicate fix and imports _is_gateway_route directly; _component_paths helper comment overpromises — it still skips all Mounts while the real predicate now keeps /metrics.

Comments Outside Diff (1)

  1. tests/test_litellm/proxy/test_component_allowlists.py, line 62-63 (link)

    P2 The _component_paths docstring still claims to reproduce _is_gateway_route / _is_backend_route, but it unconditionally skips every Mount while _is_gateway_route now keeps /metrics. The helper is still correct for test_gateway_plus_backend_covers_full_app (which also excludes Mounts from all_paths), but the misleading comment could cause future contributors to rely on it for Mount coverage checking and get incorrect results.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (2): Last reviewed commit: "fix(gateway): keep prometheus /metrics m..." | Re-trigger Greptile

@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…utes

The componentized gateway trims the shared proxy route table to the data-plane
allowlist, but _is_gateway_route dropped every Mount unconditionally.
Prometheus registers /metrics as a Mount (make_asgi_app), not an APIRoute, so
the gateway returned 404 on /metrics even though gateway/routes/allowlist.py
lists it.

Add a GATEWAY_MOUNT_PATHS allowlist (mirroring the BACKEND_MOUNT_PATHS pattern)
holding /metrics, and keep a Mount only when its path is in it. The UI static
mounts and the MCP catch-all Mount stay dropped.

Fixes BerriAI#30291
@manan-tech
manan-tech force-pushed the litellm_fix_gateway_metrics_mount branch from c064c6d to ae10154 Compare June 13, 2026 06:30
@manan-tech

Copy link
Copy Markdown
Author

Updated to follow the new GATEWAY_MOUNT_PATHS allowlist pattern (mirroring BACKEND_MOUNT_PATHS) and rebased onto the latest litellm_internal_staging. @greptileai


GATEWAY_MOUNT_PATHS: frozenset[str] = frozenset(
{
"/metrics", # Prometheus scrape endpoint, registered as an ASGI Mount

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: Unauthenticated metrics exposure

/metrics is mounted by PrometheusLogger._mount_metrics_endpoint() with no authentication, and the Helm ingress routes /metrics to the gateway. With this allowlist entry, an unauthenticated requester can scrape Prometheus labels such as user email, client IP, API key hash, team, model, and provider routing metadata from the public gateway path; keep this mount on an internal-only listener/service or add an auth/network gate before exposing it through the gateway ingress.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change restores the /metrics Mount that the monolithic entrypoint already serves; it does not alter the auth posture. Metrics auth is enforced by PrometheusAuthMiddleware (litellm/proxy/middleware/prometheus_auth_middleware.py), added via app.add_middleware(...) at proxy_server.py:1745 and run by default. The gateway reuses that same app, and the lifespan trim only filters app.router.routes, so the middleware stays active on gateway.main; /metrics is authenticated unless an operator explicitly sets litellm_settings.require_auth_for_metrics_endpoint: false. The linked issue's reproduction sets that flag to false to surface the 404, which is why the example reads as unauthenticated. The Helm ingress already routes /metrics to the gateway and the allowlist already lists it, so keeping the Mount is the intended behavior; tightening the default or the ingress would be a separate change outside this fix

@veria-ai

veria-ai Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

PR overview

This pull request updates the gateway route allowlist so the Prometheus /metrics endpoint remains mounted when gateway routes are trimmed. The change affects the gateway routing configuration around metrics endpoint preservation.

There is one open security issue: the preserved /metrics route is publicly reachable through the gateway ingress without authentication. An unauthenticated requester could scrape Prometheus labels containing sensitive operational and user-related metadata, so the endpoint should be restricted to an internal listener/service or protected with an authentication or network gate. No issues have been addressed yet, so the PR still carries a clear information-disclosure risk.

Open issues (1)

Fixed/addressed: 0 · PR risk: 7/10

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@github-actions github-actions Bot added the stale label Sep 12, 2026
@yuneng-berri
yuneng-berri deleted the branch BerriAI:litellm_internal_staging September 13, 2026 04:24
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@github-actions github-actions Bot removed the stale label Sep 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

#30291 was fixed by #32317 on main, so this pull request is closed. Reopen it if something was missed.

@github-actions github-actions Bot closed this Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Componentized gateway drops /metrics Mount — Prometheus 404 on gateway.main entrypoint

2 participants