Repository navigation
fix(gateway): keep prometheus /metrics mount when trimming gateway routes - #30297
manan-tech wants to merge 1 commit into
Conversation
Greptile SummaryThis PR fixes a bug where the gateway's lifespan route-trim was dropping the Prometheus
Confidence Score: 5/5The 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.
|
| 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 SummaryThis PR fixes a bug where the gateway's
Confidence Score: 4/5Safe 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 The
|
| 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)
-
tests/test_litellm/proxy/test_component_allowlists.py, line 62-63 (link)The
_component_pathsdocstring still claims to reproduce_is_gateway_route/_is_backend_route, but it unconditionally skips everyMountwhile_is_gateway_routenow keeps/metrics. The helper is still correct fortest_gateway_plus_backend_covers_full_app(which also excludes Mounts fromall_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 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
c064c6d to
ae10154
Compare
|
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
PR overviewThis pull request updates the gateway route allowlist so the Prometheus There is one open security issue: the preserved Open issues (1)
Fixed/addressed: 0 · PR risk: 7/10 |
|
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. |
Relevant issues
Fixes #30291
Pre-Submission checklist
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewScreenshots / Proof of Fix
Gateway started with the componentized entrypoint and prometheus enabled in the config:
Before this change the gateway is healthy but /metrics is gone:
After this change the same commands serve the Prometheus exposition:
The trim still drops everything it is supposed to drop on the same run:
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_routerejected everyMountbefore the allowlist check, which kept UI static mounts off the gateway. Prometheus registers /metrics asapp.mount("/metrics", make_asgi_app()), aMountrather than anAPIRoute, so the lifespan trim removed it and scrapes got 404 ongateway.maineven with /metrics listed ingateway/routes/allowlist.py.This mirrors the
BACKEND_MOUNT_PATHSallowlist the backend component already uses for its kept Mounts: a newGATEWAY_MOUNT_PATHSfrozenset ingateway/routes/allowlist.pyholds/metrics, and_is_gateway_routekeeps a Mount only when its path is in that set. The UI static mounts and the MCP catch-allMount("/")stay dropped.tests/test_litellm/proxy/test_component_allowlists.pygains gateway Mount coverage in the same shape as the existing backend Mount tests: theGATEWAY_MOUNT_PATHSconstant is validated, and_is_gateway_routeis asserted to keep the /metrics Mount while dropping the UI and MCP mounts. The import ofgateway.mainsaves and restores the shared app'slifespan_contextso 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