Skip to content

Commit aa7839e

Browse files
fix: fix test
1 parent f387803 commit aa7839e

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

tests/otel_tests/test_prometheus.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,16 @@ async def test_proxy_failure_metrics():
109109
expected_metric_pattern = '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",user_email="None"}'
110110

111111
# Check if the pattern is in metrics (this metric doesn't include user_email field)
112-
assert any(expected_metric_pattern in line for line in metrics.split('\n')), f"Expected failure metric pattern not found in /metrics. Pattern: {expected_metric_pattern}"
113-
112+
assert any(
113+
expected_metric_pattern in line for line in metrics.split("\n")
114+
), f"Expected failure metric pattern not found in /metrics. Pattern: {expected_metric_pattern}"
115+
114116
# Check total requests metric which includes user_email
115117
total_requests_pattern = '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"}'
116118

117-
assert any(total_requests_pattern in line for line in metrics.split('\n')), f"Expected total requests metric pattern not found in /metrics. Pattern: {total_requests_pattern}"
119+
assert any(
120+
total_requests_pattern in line for line in metrics.split("\n")
121+
), f"Expected total requests metric pattern not found in /metrics. Pattern: {total_requests_pattern}"
118122

119123

120124
@pytest.mark.asyncio
@@ -378,7 +382,9 @@ async def test_team_budget_metrics():
378382
assert first_budget["total"] == 10.0, "Total budget metric is incorrect"
379383
print("first_budget['remaining_hours']", first_budget["remaining_hours"])
380384
# Budget should have positive remaining hours, up to 7 days
381-
assert 0 < first_budget["remaining_hours"] <= 168, "Budget should have positive remaining hours, up to 7 days"
385+
assert (
386+
0 < first_budget["remaining_hours"] <= 168
387+
), "Budget should have positive remaining hours, up to 7 days"
382388

383389
# Get team info and verify spend matches prometheus metrics
384390
team_info = await get_team_info(session, team_id)
@@ -510,7 +516,9 @@ async def test_key_budget_metrics():
510516
print("first_budget['remaining_hours']", first_budget["remaining_hours"])
511517
# The budget reset time is now standardized - for "7d" it resets on Monday at midnight
512518
# So we'll check if it's within a reasonable range (0-7 days depending on current day of week)
513-
assert 0 <= first_budget["remaining_hours"] <= 168, "Budget remaining hours should be within a reasonable range (0-7 days depending on day of week)"
519+
assert (
520+
0 <= first_budget["remaining_hours"] <= 168
521+
), "Budget remaining hours should be within a reasonable range (0-7 days depending on day of week)"
514522

515523
# Get key info and verify spend matches prometheus metrics
516524
key_info = await get_key_info(session, key)
@@ -570,6 +578,7 @@ async def test_user_email_metrics():
570578
user_email in metrics_after_first
571579
), "user_email should be tracked correctly"
572580

581+
573582
@pytest.mark.asyncio
574583
async def test_user_email_in_all_required_metrics():
575584
"""
@@ -579,7 +588,7 @@ async def test_user_email_in_all_required_metrics():
579588
- litellm_input_tokens_metric_total
580589
- litellm_output_tokens_metric_total
581590
- litellm_requests_metric_total
582-
- litellm_spend_metric
591+
- litellm_spend_metric_total
583592
"""
584593
async with aiohttp.ClientSession() as session:
585594
# Create a user with user_email
@@ -611,16 +620,21 @@ async def test_user_email_in_all_required_metrics():
611620
"litellm_input_tokens_metric_total",
612621
"litellm_output_tokens_metric_total",
613622
"litellm_requests_metric_total",
614-
"litellm_spend_metric"
623+
"litellm_spend_metric_total",
615624
]
616625

617626
import re
627+
618628
for metric_name in required_metrics_with_user_email:
619629
# Check that the metric exists and contains user_email label
620630
# Look for the metric with user_email in its labels
621-
pattern = rf'{metric_name}{{[^}}]*user_email="{re.escape(user_email)}"[^}}]*}}'
631+
pattern = (
632+
rf'{metric_name}{{[^}}]*user_email="{re.escape(user_email)}"[^}}]*}}'
633+
)
622634
matches = re.findall(pattern, metrics_text)
623-
assert len(matches) > 0, f"Metric {metric_name} should contain user_email={user_email} but was not found in metrics"
635+
assert (
636+
len(matches) > 0
637+
), f"Metric {metric_name} should contain user_email={user_email} but was not found in metrics"
624638

625639
# Also test failure metric by making a bad request
626640
try:
@@ -639,4 +653,6 @@ async def test_user_email_in_all_required_metrics():
639653
# Check that failure metric also contains user_email
640654
failure_pattern = rf'litellm_proxy_failed_requests_metric_total{{[^}}]*user_email="{re.escape(user_email)}"[^}}]*}}'
641655
failure_matches = re.findall(failure_pattern, metrics_text)
642-
assert len(failure_matches) > 0, f"litellm_proxy_failed_requests_metric_total should contain user_email={user_email}"
656+
assert (
657+
len(failure_matches) > 0
658+
), f"litellm_proxy_failed_requests_metric_total should contain user_email={user_email}"

0 commit comments

Comments
 (0)