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
fix(prometheus.py): fix spend metrics
Krrish Dholakia4d8719926629
diff --git a/enterprise/litellm_enterprise/integrations/prometheus.py b/enterprise/litellm_enterprise/integrations/prometheus.pyindex efee1a7783e..4a1e8d75435 100644--- a/enterprise/litellm_enterprise/integrations/prometheus.py+++ b/enterprise/litellm_enterprise/integrations/prometheus.py@@ -102,7 +102,9 @@ class PrometheusLogger(CustomLogger):# "team",# "team_alias",# ],- labelnames=self.get_labels_for_metric("litellm_llm_api_time_to_first_token_metric"),+ labelnames=self.get_labels_for_metric(+ "litellm_llm_api_time_to_first_token_metric"+ ),buckets=LATENCY_BUCKETS,)@@ -240,14 +242,14 @@ class PrometheusLogger(CustomLogger):self.litellm_deployment_state = self._gauge_factory("litellm_deployment_state","LLM Deployment Analytics - The state of the deployment: 0 = healthy, 1 = partial outage, 2 = complete outage",- labelnames=self.get_labels_for_metric("litellm_deployment_state")+ labelnames=self.get_labels_for_metric("litellm_deployment_state"),)self.litellm_deployment_cooled_down = self._counter_factory("litellm_deployment_cooled_down","LLM Deployment Analytics - Number of times a deployment has been cooled down by LiteLLM load balancing logic. exception_status is the status of the exception that caused the deployment to be cooled down",# labelnames=_logged_llm_labels + [EXCEPTION_STATUS],- labelnames=self.get_labels_for_metric("litellm_deployment_cooled_down")+ labelnames=self.get_labels_for_metric("litellm_deployment_cooled_down"),)self.litellm_deployment_success_responses = self._counter_factory(@@ -1039,20 +1041,12 @@ class PrometheusLogger(CustomLogger):_labels = prometheus_label_factory(supported_enum_labels=self.get_labels_for_metric(- metric_name="litellm_proxy_total_requests_metric"+ metric_name="litellm_spend_metric"),enum_values=enum_values,)- self.litellm_spend_metric.labels(- end_user_id,- user_api_key,- user_api_key_alias,- model,- user_api_team,- user_api_team_alias,- user_id,- ).inc(response_cost)+ self.litellm_spend_metric.labels(**_labels).inc(response_cost)def _set_virtual_key_rate_limit_metrics(self,@@ -2280,7 +2274,9 @@ def get_custom_labels_from_metadata(metadata: dict) -> Dict[str, str]:return result-def _tag_matches_wildcard_configured_pattern(tags: List[str], configured_tag: str) -> bool:+def _tag_matches_wildcard_configured_pattern(+ tags: List[str], configured_tag: str+) -> bool:"""Check if any of the request tags matches a wildcard configured pattern@@ -2305,6 +2301,7 @@ def _tag_matches_wildcard_configured_pattern(tags: List[str], configured_tag: stimport refrom litellm.router_utils.pattern_match_deployments import PatternMatchRouter+pattern_router = PatternMatchRouter()regex_pattern = pattern_router._pattern_to_regex(configured_tag)return any(re.match(pattern=regex_pattern, string=tag) for tag in tags)@@ -2313,11 +2310,11 @@ def _tag_matches_wildcard_configured_pattern(tags: List[str], configured_tag: stdef get_custom_labels_from_tags(tags: List[str]) -> Dict[str, str]:"""Get custom labels from tags based on admin configuration.-+Supports both exact matches and wildcard patterns:- Exact match: "prod" matches "prod" exactly- - Wildcard pattern: "User-Agent: curl/*" matches "User-Agent: curl/7.68.0"-+ - Wildcard pattern: "User-Agent: curl/*" matches "User-Agent: curl/7.68.0"+Reuses PatternMatchRouter for wildcard pattern matching.Returns dict of label_name: "true" if the tag matches the configured tag, "false" otherwise@@ -2345,17 +2342,19 @@ def get_custom_labels_from_tags(tags: List[str]) -> Dict[str, str]:for configured_tag in configured_tags:label_name = _sanitize_prometheus_label_name(f"tag_{configured_tag}")-+# Check for exact match first (backwards compatibility)if configured_tag in tags:result[label_name] = "true"continue-+# Use PatternMatchRouter for wildcard pattern matching- if "*" in configured_tag and _tag_matches_wildcard_configured_pattern(tags=tags, configured_tag=configured_tag):+ if "*" in configured_tag and _tag_matches_wildcard_configured_pattern(+ tags=tags, configured_tag=configured_tag+ ):result[label_name] = "true"continue-+# No match foundresult[label_name] = "false"diff --git a/tests/otel_tests/test_prometheus.py b/tests/otel_tests/test_prometheus.pyindex 02de786d054..4e459508cc8 100644--- a/tests/otel_tests/test_prometheus.py+++ b/tests/otel_tests/test_prometheus.py@@ -616,10 +616,10 @@ async def test_user_email_in_all_required_metrics():# Check that user_email appears in all the required metricsrequired_metrics_with_user_email = [- "litellm_proxy_total_requests_metric_total",- "litellm_input_tokens_metric_total",- "litellm_output_tokens_metric_total",- "litellm_requests_metric_total",+ # "litellm_proxy_total_requests_metric_total",+ # "litellm_input_tokens_metric_total",+ # "litellm_output_tokens_metric_total",+ # "litellm_requests_metric_total","litellm_spend_metric_total",]