The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
perf: convert PrometheusAuthMiddleware from BaseHTTPMiddleware to pure ASGI
Ryan Crabbe6ae2d6e73b41
diff --git a/litellm/proxy/middleware/prometheus_auth_middleware.py b/litellm/proxy/middleware/prometheus_auth_middleware.pyindex fe2b7f6b783..5915e4aa07d 100644--- a/litellm/proxy/middleware/prometheus_auth_middleware.py+++ b/litellm/proxy/middleware/prometheus_auth_middleware.py@@ -1,25 +1,24 @@"""-Prometheus Auth Middleware--Pure ASGI middleware — avoids Starlette's BaseHTTPMiddleware which wraps-streaming responses with receive_or_disconnect per chunk, blocking the-event loop and causing severe throughput degradation under concurrent-streaming load.+Prometheus Auth Middleware - Pure ASGI implementation"""-from starlette.requests import Request-from starlette.responses import JSONResponse+import json++from fastapi import Requestfrom starlette.types import ASGIApp, Receive, Scope, Sendimport litellmfrom litellm.proxy._types import SpecialHeadersfrom litellm.proxy.auth.user_api_key_auth import user_api_key_auth+# Cache the header name at module level to avoid repeated enum attribute access+_AUTHORIZATION_HEADER = SpecialHeaders.openai_authorization.value # "Authorization"+class PrometheusAuthMiddleware:"""- Middleware to authenticate requests to the metrics endpoint+ Middleware to authenticate requests to the metrics endpoint.- By default, auth is not run on the metrics endpoint+ By default, auth is not run on the metrics endpoint.Enabled by setting the following in proxy_config.yaml:@@ -33,51 +32,42 @@ class PrometheusAuthMiddleware:self.app = appasync def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:- if scope["type"] not in ("http", "websocket"):+ # Fast path: only inspect HTTP requests; pass through websocket/lifespan immediately+ if scope["type"] != "http" or "/metrics" not in scope.get("path", ""):await self.app(scope, receive, send)return- request = Request(scope, receive)-- if self._is_prometheus_metrics_endpoint(request):- if self._should_run_auth_on_metrics_endpoint() is True:- try:- await user_api_key_auth(- request=request,- api_key=request.headers.get(- SpecialHeaders.openai_authorization.value- )- or "",- )- except Exception as e:- response = JSONResponse(- status_code=401,- content=f"Unauthorized access to metrics endpoint: {getattr(e, 'message', str(e))}",- )- await response(scope, receive, send)- return-+ # Only run auth if configured to do so+ if litellm.require_auth_for_metrics_endpoint is True:+ # Construct Request only when auth is actually needed+ request = Request(scope, receive)+ api_key = request.headers.get(_AUTHORIZATION_HEADER) or ""++ try:+ await user_api_key_auth(request=request, api_key=api_key)+ except Exception as e:+ # Send 401 response directly via ASGI protocol+ error_message = getattr(e, "message", str(e))+ body = json.dumps(+ f"Unauthorized access to metrics endpoint: {error_message}"+ ).encode("utf-8")+ await send(+ {+ "type": "http.response.start",+ "status": 401,+ "headers": [+ [b"content-type", b"application/json"],+ [b"content-length", str(len(body)).encode("ascii")],+ ],+ }+ )+ await send(+ {+ "type": "http.response.body",+ "body": body,+ }+ )+ return++ # Pass through to the inner applicationawait self.app(scope, receive, send)-- @staticmethod- def _is_prometheus_metrics_endpoint(request: Request):- try:- if "/metrics" in request.url.path:- return True- return False- except Exception:- return False-- @staticmethod- def _should_run_auth_on_metrics_endpoint():- """- Returns True if auth should be run on the metrics endpoint-- False by default, set to True in proxy_config.yaml to enable-- ```yaml- litellm_settings:- require_auth_for_metrics_endpoint: true- ```- """- return litellm.require_auth_for_metrics_endpoint