mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-10 22:41:41 +00:00
fix(prometheus): default to status_code=500 for exceptions without status code
_extract_status_code() returned None when an exception lacked status_code/code attributes. str(None) became the literal 'None' in Prometheus labels, causing litellm_proxy_total_requests_metric 4xx/5xx aggregations to not match litellm_proxy_failed_requests_metric. Fixes #24224 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
bc268b0376
commit
e0c711964f
2 changed files with 17 additions and 1 deletions
|
|
@ -1448,7 +1448,8 @@ class PrometheusLogger(CustomLogger):
|
|||
exception: Exception object to extract status code from directly
|
||||
|
||||
Returns:
|
||||
Status code as integer if found, None otherwise
|
||||
Status code as integer if found, 500 when an exception is present
|
||||
but carries no status code attribute, or None when no inputs provided
|
||||
"""
|
||||
status_code = None
|
||||
|
||||
|
|
@ -1486,6 +1487,14 @@ class PrometheusLogger(CustomLogger):
|
|||
except (ValueError, TypeError):
|
||||
status_code = None
|
||||
|
||||
# If an exception was provided (directly or via kwargs) but no status code
|
||||
# could be extracted, default to 500 — an unclassified server error is still a 5xx.
|
||||
if status_code is None and (
|
||||
exception is not None
|
||||
or (kwargs and kwargs.get("exception") is not None)
|
||||
):
|
||||
return 500
|
||||
|
||||
return status_code
|
||||
|
||||
def _is_invalid_api_key_request(
|
||||
|
|
|
|||
|
|
@ -39,3 +39,10 @@ def test_extract_status_code_defaults_to_500_for_bare_exception(prometheus_logge
|
|||
def test_extract_status_code_returns_none_when_no_exception(prometheus_logger):
|
||||
"""When no exception is provided at all, should return None."""
|
||||
assert prometheus_logger._extract_status_code() is None
|
||||
|
||||
|
||||
def test_extract_status_code_defaults_to_500_for_bare_exception_in_kwargs(prometheus_logger):
|
||||
"""Bare exception passed through kwargs should also default to 500."""
|
||||
exc = Exception("something broke")
|
||||
result = prometheus_logger._extract_status_code(kwargs={"exception": exc})
|
||||
assert result == 500, f"Expected 500 for bare exception in kwargs, got {result}"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue