From 86b473d26708c2d3f3a13030efdf8fd637e97cfb Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Fri, 4 Apr 2025 20:37:17 -0700 Subject: [PATCH] allow adding auth on /metrics endpoint --- litellm/proxy/middleware/prometheus_auth_middleware.py | 6 +++++- litellm/proxy/proxy_config.yaml | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/middleware/prometheus_auth_middleware.py b/litellm/proxy/middleware/prometheus_auth_middleware.py index ca38a9d445..9e5a8ffcc8 100644 --- a/litellm/proxy/middleware/prometheus_auth_middleware.py +++ b/litellm/proxy/middleware/prometheus_auth_middleware.py @@ -2,6 +2,7 @@ Prometheus Auth Middleware """ from fastapi import Request +from fastapi.responses import JSONResponse from starlette.middleware.base import BaseHTTPMiddleware import litellm @@ -24,7 +25,10 @@ class PrometheusAuthMiddleware(BaseHTTPMiddleware): or "", ) except Exception as e: - raise e + return JSONResponse( + status_code=401, + content=f"Unauthorized access to metrics endpoint: {getattr(e, 'message', str(e))}", + ) # Process the request and get the response response = await call_next(request) diff --git a/litellm/proxy/proxy_config.yaml b/litellm/proxy/proxy_config.yaml index 61950f55fe..788bf9642d 100644 --- a/litellm/proxy/proxy_config.yaml +++ b/litellm/proxy/proxy_config.yaml @@ -10,5 +10,6 @@ model_list: api_key: fake-key litellm_settings: + require_auth_for_metrics_endpoint: true callbacks: ["prometheus"] service_callback: ["prometheus_system"] \ No newline at end of file