Skip to content

fix(test): update Prometheus metric test patterns with new labels#20213

Open
shin-bot-litellm wants to merge 2 commits intomainfrom
litellm_fix_prometheus_test_labels
Open

fix(test): update Prometheus metric test patterns with new labels#20213
shin-bot-litellm wants to merge 2 commits intomainfrom
litellm_fix_prometheus_test_labels

Conversation

@shin-bot-litellm
Copy link
Contributor

Summary

Fixes test_proxy_failure_metrics in otel_tests CI job.

Root Cause

PRs #19717 and #19678 added new labels (client_ip, user_agent, model_id) to Prometheus metrics, but test patterns weren't updated. Prometheus outputs labels alphabetically, so new labels broke the substring match.

Changes

  • Updated expected metric patterns in test_proxy_failure_metrics to include all current labels in the correct alphabetical order

Testing

  • CI passes

cc @ishaan-jaff

…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
@vercel
Copy link

vercel bot commented Feb 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Error Error Feb 1, 2026 1:25am

Request Review

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 1, 2026

Greptile Overview

Greptile Summary

Updated test patterns to include new Prometheus metric labels (client_ip, user_agent, model_id) added in PRs #19717 and #19678. The primary fix addresses test_proxy_failure_metrics failures by ensuring expected patterns match actual metric output with labels in alphabetical order.

  • test_prometheus.py: Added missing labels to metric patterns in correct alphabetical order
  • test_utils.py: Improved test isolation with unique model names and cache invalidation
  • test_resend_email.py: Fixed mock injection to bypass HTTP client caching
  • test_google_genai_handler.py: Simplified mock patching approach
  • test_vector_store_registry.py: Corrected patch.object usage for proper mock injection

All changes are test-only fixes with no impact on production code.

Confidence Score: 5/5

  • Safe to merge - all changes are test-only improvements with no production code impact
  • All changes are test fixes addressing CI failures and improving test reliability. No production code modified, no logical errors detected.
  • No files require special attention

Important Files Changed

Filename Overview
tests/otel_tests/test_prometheus.py Updated metric patterns with new labels (client_ip, user_agent, model_id) in alphabetical order to fix test failures
tests/test_litellm/test_utils.py Improved test isolation by using unique model name and clearing caches before/after test execution
tests/test_litellm/enterprise/enterprise_callbacks/send_emails/test_resend_email.py Fixed mock injection by directly setting the async HTTP client on logger instance to bypass caching

Sequence Diagram

sequenceDiagram
    participant Test as Test Suite
    participant Proxy as LiteLLM Proxy
    participant Prom as Prometheus Metrics
    
    Note over Test,Prom: test_proxy_failure_metrics flow
    
    Test->>Proxy: POST /chat/completions (bad request)
    Proxy-->>Test: 429 Rate Limit Error
    Proxy->>Prom: Record failure metric with labels:<br/>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, user_agent, user_email
    
    Test->>Proxy: GET /metrics
    Proxy-->>Test: Return Prometheus metrics
    
    Note over Test: Verify metrics contain expected<br/>pattern with all labels in<br/>alphabetical order
    
    Test->>Test: Assert litellm_proxy_failed_requests_metric_total<br/>contains all required labels
    Test->>Test: Assert litellm_proxy_total_requests_metric_total<br/>contains all required labels
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants