Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

prometheus - safe update start / end time

Ishaan Jaff443e1b3bba4a
diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.py
index b6f94bb464d..642a776b640 100644
--- a/litellm/integrations/prometheus.py
+++ b/litellm/integrations/prometheus.py
@@ -349,19 +349,23 @@ class PrometheusLogger(CustomLogger):
# latency metrics
total_time: timedelta = kwargs.get("end_time") - kwargs.get("start_time")
total_time_seconds = total_time.total_seconds()
- api_call_total_time: timedelta = kwargs.get("end_time") - kwargs.get(
- "api_call_start_time"
- )
+ api_call_start_time = kwargs.get("api_call_start_time", None)
- api_call_total_time_seconds = api_call_total_time.total_seconds()
+ if api_call_start_time is not None and isinstance(
+ api_call_start_time, datetime
+ ):
+ api_call_total_time: timedelta = (
+ kwargs.get("end_time") - api_call_start_time
+ )
+ api_call_total_time_seconds = api_call_total_time.total_seconds()
+ self.litellm_llm_api_latency_metric.labels(model).observe(
+ api_call_total_time_seconds
+ )
# log metrics
self.litellm_request_total_latency_metric.labels(model).observe(
total_time_seconds
)
- self.litellm_llm_api_latency_metric.labels(model).observe(
- api_call_total_time_seconds
- )
# set x-ratelimit headers
if premium_user is True: