fix(test): add missing Prometheus metric labels to test_proxy_failure_metrics#20211
fix(test): add missing Prometheus metric labels to test_proxy_failure_metrics#20211shin-bot-litellm wants to merge 2 commits intomainfrom
Conversation
…issues ## Problem Four tests in litellm_mapped_tests_core were failing: 1. test_register_model_with_scientific_notation - KeyError due to test isolation issues 2. test_search_uses_registry_credentials - Mock not being called due to incorrect patch path 3. test_send_email_missing_api_key - Real API calls despite mocking 4. test_stream_transformation_error_sync - Mock not effective, real API called ## Solution ### test_register_model_with_scientific_notation - Use unique model name to avoid conflicts with other tests - Clear LRU caches before test to prevent stale data - Clean up model_cost entry after test ### test_search_uses_registry_credentials - Use patch.object() on the actual base_llm_http_handler instance - String-based patching for instance methods can fail; direct object patching is more reliable ### test_send_email_missing_api_key - Directly inject mock HTTP client into logger instance - This bypasses any caching issues that could cause the fixture mock to be ineffective ### test_stream_transformation_error_sync - Patch litellm.completion directly instead of the handler module's litellm reference - This ensures the mock is effective regardless of import order ## Regression These tests were affected by LRU caching added in #19606 and HTTP client caching.
…_metrics ## Problem test_proxy_failure_metrics was failing in CI because the expected metric patterns didn't include labels that were recently added to Prometheus metrics: - client_ip (added in #19717) - user_agent (added in #19717) - model_id (added in #19678) ## Solution Update the expected metric patterns in test_proxy_failure_metrics to include the new labels in the correct alphabetical order (as Prometheus outputs them): - litellm_proxy_failed_requests_metric_total - litellm_proxy_total_requests_metric_total Labels are now: api_key_alias, client_ip, end_user, exception_class, exception_status, hashed_api_key, model_id, requested_model, route, team, team_alias, user, user_agent, user_email
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
|
AnalysisFailing Test
Root CauseThe test checks for exact Prometheus metric patterns using substring matching. When PRs #19717 and #19678 added new labels ( Prometheus outputs labels in alphabetical order, so the new labels appear in the middle of the existing pattern, breaking the substring match: Before (expected by test): After (actual output): FixUpdated the expected patterns to include all current labels in the correct alphabetical order. CI ValidationWaiting for CI to run to validate the fix works. |
Greptile OverviewGreptile SummaryUpdated Key changes:
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| tests/otel_tests/test_prometheus.py | Updates expected Prometheus metric label patterns to include client_ip, user_agent, and model_id labels in alphabetical order |
| tests/test_litellm/enterprise/enterprise_callbacks/send_emails/test_resend_email.py | Improved mock injection by directly setting logger.async_httpx_client to bypass caching issues |
| tests/test_litellm/test_utils.py | Added test isolation with unique model name, cache clearing, and proper cleanup to prevent test interference |
Sequence Diagram
sequenceDiagram
participant Test as test_proxy_failure_metrics
participant Proxy as LiteLLM Proxy
participant Prom as Prometheus Metrics
Test->>Proxy: POST /chat/completions (bad request)
Proxy->>Prom: Increment litellm_proxy_failed_requests_metric
Note over Prom: Labels: api_key_alias, client_ip, end_user,<br/>exception_class, exception_status,<br/>hashed_api_key, model_id, requested_model,<br/>route, team, team_alias, user,<br/>user_agent, user_email
Proxy->>Prom: Increment litellm_proxy_total_requests_metric
Note over Prom: Labels: api_key_alias, client_ip, end_user,<br/>hashed_api_key, model_id, requested_model,<br/>route, status_code, team, team_alias,<br/>user, user_agent, user_email
Proxy-->>Test: 429 Rate Limit Error
Test->>Proxy: GET /metrics
Proxy->>Prom: Export metrics
Prom-->>Proxy: Formatted metrics (alphabetically sorted labels)
Proxy-->>Test: Metrics response
Test->>Test: Assert metric patterns match (including new labels)
Problem
test_proxy_failure_metricsintests/otel_tests/test_prometheus.pywas failing in CI:https://app.circleci.com/pipelines/github/BerriAI/litellm/56743/workflows/9f492c05-c440-4c80-84d4-34c6cd566930/jobs/1140648/tests
The test expects specific Prometheus metric patterns but the expected patterns did not include labels that were recently added:
client_ip(added in feat: add clientip and user agent in metrics #19717)user_agent(added in feat: add clientip and user agent in metrics #19717)model_id(added in feat: Add model_id label to Prometheus metrics (#18048) #19678)Root Cause
When these PRs added new labels to
litellm_proxy_failed_requests_metricandlitellm_proxy_total_requests_metric, the test was not updated to match. The test does an exact substring match on the Prometheus metric output, so when the new labels appeared (in alphabetical order between existing labels), the expected pattern no longer matched.Solution
Update the expected metric patterns in
test_proxy_failure_metricsto include the new labels in the correct alphabetical order (as Prometheus outputs them).Labels updated for
litellm_proxy_failed_requests_metric:api_key_alias, client_ip, end_user, exception_class, exception_status, hashed_api_key, model_id, requested_model, route, team, team_alias, user, user_agent, user_emailLabels updated for
litellm_proxy_total_requests_metric:api_key_alias, client_ip, end_user, hashed_api_key, model_id, requested_model, route, status_code, team, team_alias, user, user_agent, user_emailTesting
This is a test fix - the CI test suite will validate the fix passes.