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
[Feat] Prometheus - Track `route` on proxy_* metrics (#10992)
Ishaan Jaff14321a2708d5
diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.pyindex aa543ee4891..a66b1e755f6 100644--- a/litellm/integrations/prometheus.py+++ b/litellm/integrations/prometheus.py@@ -833,6 +833,7 @@ class PrometheusLogger(CustomLogger):exception_status=str(getattr(original_exception, "status_code", None)),exception_class=self._get_exception_class_name(original_exception),tags=_tags,+ route=user_api_key_dict.request_route,)_labels = prometheus_label_factory(supported_enum_labels=PrometheusMetricLabels.get_labels(@@ -873,6 +874,7 @@ class PrometheusLogger(CustomLogger):user=user_api_key_dict.user_id,user_email=user_api_key_dict.user_email,status_code="200",+ route=user_api_key_dict.request_route,)_labels = prometheus_label_factory(supported_enum_labels=PrometheusMetricLabels.get_labels(diff --git a/litellm/types/integrations/prometheus.py b/litellm/types/integrations/prometheus.pyindex 8fdcce4cbb8..0155a0630c0 100644--- a/litellm/types/integrations/prometheus.py+++ b/litellm/types/integrations/prometheus.py@@ -70,6 +70,7 @@ class UserAPIKeyLabelNames(Enum):EXCEPTION_CLASS = EXCEPTION_CLASSSTATUS_CODE = "status_code"FALLBACK_MODEL = "fallback_model"+ ROUTE = "route"DEFINED_PROMETHEUS_METRICS = Literal[@@ -125,6 +126,7 @@ class PrometheusMetricLabels:UserAPIKeyLabelNames.USER.value,UserAPIKeyLabelNames.STATUS_CODE.value,UserAPIKeyLabelNames.USER_EMAIL.value,+ UserAPIKeyLabelNames.ROUTE.value,]litellm_proxy_failed_requests_metric = [@@ -137,6 +139,7 @@ class PrometheusMetricLabels:UserAPIKeyLabelNames.USER.value,UserAPIKeyLabelNames.EXCEPTION_STATUS.value,UserAPIKeyLabelNames.EXCEPTION_CLASS.value,+ UserAPIKeyLabelNames.ROUTE.value,]litellm_deployment_latency_per_output_token = [@@ -292,3 +295,6 @@ class UserAPIKeyLabelValues(BaseModel):fallback_model: Annotated[Optional[str], Field(..., alias=UserAPIKeyLabelNames.FALLBACK_MODEL.value)] = None+ route: Annotated[+ Optional[str], Field(..., alias=UserAPIKeyLabelNames.ROUTE.value)+ ] = Nonediff --git a/tests/logging_callback_tests/test_prometheus_unit_tests.py b/tests/logging_callback_tests/test_prometheus_unit_tests.pyindex 0b58bc7aafb..007de7e337e 100644--- a/tests/logging_callback_tests/test_prometheus_unit_tests.py+++ b/tests/logging_callback_tests/test_prometheus_unit_tests.py@@ -694,6 +694,7 @@ async def test_async_post_call_failure_hook(prometheus_logger):team_alias="test_team_alias",user_id="test_user",end_user_id="test_end_user",+ request_route="/chat/completions",)# Call the function@@ -714,6 +715,7 @@ async def test_async_post_call_failure_hook(prometheus_logger):user="test_user",exception_status="429",exception_class="Openai.RateLimitError",+ route=user_api_key_dict.request_route,)prometheus_logger.litellm_proxy_failed_requests_metric.labels().inc.assert_called_once()@@ -728,6 +730,7 @@ async def test_async_post_call_failure_hook(prometheus_logger):user="test_user",status_code="429",user_email=None,+ route=user_api_key_dict.request_route,)prometheus_logger.litellm_proxy_total_requests_metric.labels().inc.assert_called_once()@@ -752,6 +755,7 @@ async def test_async_post_call_success_hook(prometheus_logger):team_alias="test_team_alias",user_id="test_user",end_user_id="test_end_user",+ request_route="/chat/completions",)response = {"choices": [{"message": {"content": "test response"}}]}@@ -772,6 +776,7 @@ async def test_async_post_call_success_hook(prometheus_logger):user="test_user",status_code="200",user_email=None,+ route=user_api_key_dict.request_route,)prometheus_logger.litellm_proxy_total_requests_metric.labels().inc.assert_called_once()diff --git a/tests/otel_tests/test_prometheus.py b/tests/otel_tests/test_prometheus.pyindex 7b80e23fb9b..95ecddc15d3 100644--- a/tests/otel_tests/test_prometheus.py+++ b/tests/otel_tests/test_prometheus.py@@ -106,7 +106,7 @@ async def test_proxy_failure_metrics():print("/metrics", metrics)# Check if the failure metric is present and correct- expected_metric = 'litellm_proxy_failed_requests_metric_total{api_key_alias="None",end_user="None",exception_class="Openai.RateLimitError",exception_status="429",hashed_api_key="88dc28d0f030c55ed4ab77ed8faf098196cb1c05df778539800c9f1243fe6b4b",requested_model="fake-azure-endpoint",team="None",team_alias="None",user="default_user_id"} 1.0'+ expected_metric = 'litellm_proxy_failed_requests_metric_total{api_key_alias="None",end_user="None",exception_class="Openai.RateLimitError",exception_status="429",hashed_api_key="88dc28d0f030c55ed4ab77ed8faf098196cb1c05df778539800c9f1243fe6b4b",requested_model="fake-azure-endpoint",route="/chat/completions",team="None",team_alias="None",user="default_user_id"} 1.0'assert (expected_metric in metrics@@ -115,7 +115,7 @@ async def test_proxy_failure_metrics():assert expected_llm_deployment_failureassert (- 'litellm_proxy_total_requests_metric_total{api_key_alias="None",end_user="None",hashed_api_key="88dc28d0f030c55ed4ab77ed8faf098196cb1c05df778539800c9f1243fe6b4b",requested_model="fake-azure-endpoint",status_code="429",team="None",team_alias="None",user="default_user_id",user_email="None"} 1.0'+ 'litellm_proxy_total_requests_metric_total{api_key_alias="None",end_user="None",hashed_api_key="88dc28d0f030c55ed4ab77ed8faf098196cb1c05df778539800c9f1243fe6b4b",requested_model="fake-azure-endpoint",route="/chat/completions",status_code="429",team="None",team_alias="None",user="default_user_id",user_email="None"} 1.0'in metrics)