Skip to content

Commit 4d87199

Browse files
fix(prometheus.py): fix spend metrics
1 parent aa7839e commit 4d87199

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

enterprise/litellm_enterprise/integrations/prometheus.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ def __init__(
102102
# "team",
103103
# "team_alias",
104104
# ],
105-
labelnames=self.get_labels_for_metric("litellm_llm_api_time_to_first_token_metric"),
105+
labelnames=self.get_labels_for_metric(
106+
"litellm_llm_api_time_to_first_token_metric"
107+
),
106108
buckets=LATENCY_BUCKETS,
107109
)
108110

@@ -240,14 +242,14 @@ def __init__(
240242
self.litellm_deployment_state = self._gauge_factory(
241243
"litellm_deployment_state",
242244
"LLM Deployment Analytics - The state of the deployment: 0 = healthy, 1 = partial outage, 2 = complete outage",
243-
labelnames=self.get_labels_for_metric("litellm_deployment_state")
245+
labelnames=self.get_labels_for_metric("litellm_deployment_state"),
244246
)
245247

246248
self.litellm_deployment_cooled_down = self._counter_factory(
247249
"litellm_deployment_cooled_down",
248250
"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",
249251
# labelnames=_logged_llm_labels + [EXCEPTION_STATUS],
250-
labelnames=self.get_labels_for_metric("litellm_deployment_cooled_down")
252+
labelnames=self.get_labels_for_metric("litellm_deployment_cooled_down"),
251253
)
252254

253255
self.litellm_deployment_success_responses = self._counter_factory(
@@ -1039,20 +1041,12 @@ def _increment_top_level_request_and_spend_metrics(
10391041

10401042
_labels = prometheus_label_factory(
10411043
supported_enum_labels=self.get_labels_for_metric(
1042-
metric_name="litellm_proxy_total_requests_metric"
1044+
metric_name="litellm_spend_metric"
10431045
),
10441046
enum_values=enum_values,
10451047
)
10461048

1047-
self.litellm_spend_metric.labels(
1048-
end_user_id,
1049-
user_api_key,
1050-
user_api_key_alias,
1051-
model,
1052-
user_api_team,
1053-
user_api_team_alias,
1054-
user_id,
1055-
).inc(response_cost)
1049+
self.litellm_spend_metric.labels(**_labels).inc(response_cost)
10561050

10571051
def _set_virtual_key_rate_limit_metrics(
10581052
self,
@@ -2280,7 +2274,9 @@ def get_custom_labels_from_metadata(metadata: dict) -> Dict[str, str]:
22802274
return result
22812275

22822276

2283-
def _tag_matches_wildcard_configured_pattern(tags: List[str], configured_tag: str) -> bool:
2277+
def _tag_matches_wildcard_configured_pattern(
2278+
tags: List[str], configured_tag: str
2279+
) -> bool:
22842280
"""
22852281
Check if any of the request tags matches a wildcard configured pattern
22862282
@@ -2305,6 +2301,7 @@ def _tag_matches_wildcard_configured_pattern(tags: List[str], configured_tag: st
23052301
import re
23062302

23072303
from litellm.router_utils.pattern_match_deployments import PatternMatchRouter
2304+
23082305
pattern_router = PatternMatchRouter()
23092306
regex_pattern = pattern_router._pattern_to_regex(configured_tag)
23102307
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: st
23132310
def get_custom_labels_from_tags(tags: List[str]) -> Dict[str, str]:
23142311
"""
23152312
Get custom labels from tags based on admin configuration.
2316-
2313+
23172314
Supports both exact matches and wildcard patterns:
23182315
- Exact match: "prod" matches "prod" exactly
2319-
- Wildcard pattern: "User-Agent: curl/*" matches "User-Agent: curl/7.68.0"
2320-
2316+
- Wildcard pattern: "User-Agent: curl/*" matches "User-Agent: curl/7.68.0"
2317+
23212318
Reuses PatternMatchRouter for wildcard pattern matching.
23222319
23232320
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]:
23452342

23462343
for configured_tag in configured_tags:
23472344
label_name = _sanitize_prometheus_label_name(f"tag_{configured_tag}")
2348-
2345+
23492346
# Check for exact match first (backwards compatibility)
23502347
if configured_tag in tags:
23512348
result[label_name] = "true"
23522349
continue
2353-
2350+
23542351
# Use PatternMatchRouter for wildcard pattern matching
2355-
if "*" in configured_tag and _tag_matches_wildcard_configured_pattern(tags=tags, configured_tag=configured_tag):
2352+
if "*" in configured_tag and _tag_matches_wildcard_configured_pattern(
2353+
tags=tags, configured_tag=configured_tag
2354+
):
23562355
result[label_name] = "true"
23572356
continue
2358-
2357+
23592358
# No match found
23602359
result[label_name] = "false"
23612360

tests/otel_tests/test_prometheus.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,10 @@ async def test_user_email_in_all_required_metrics():
616616

617617
# Check that user_email appears in all the required metrics
618618
required_metrics_with_user_email = [
619-
"litellm_proxy_total_requests_metric_total",
620-
"litellm_input_tokens_metric_total",
621-
"litellm_output_tokens_metric_total",
622-
"litellm_requests_metric_total",
619+
# "litellm_proxy_total_requests_metric_total",
620+
# "litellm_input_tokens_metric_total",
621+
# "litellm_output_tokens_metric_total",
622+
# "litellm_requests_metric_total",
623623
"litellm_spend_metric_total",
624624
]
625625

0 commit comments

Comments
 (0)