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]

use asyncio tasks for logging db metrics (#7663)

Ishaan Jaff51b17be3ab20
diff --git a/litellm/proxy/db/log_db_metrics.py b/litellm/proxy/db/log_db_metrics.py
index cead4dc681f..b9b443c6dba 100644
--- a/litellm/proxy/db/log_db_metrics.py
+++ b/litellm/proxy/db/log_db_metrics.py
@@ -4,6 +4,7 @@ Handles logging DB success/failure to ServiceLogger()
ServiceLogger() then sends DB logs to Prometheus, OTEL, Datadog etc
"""
+import asyncio
from datetime import datetime
from functools import wraps
from typing import Callable, Dict, Tuple
@@ -44,18 +45,20 @@ def log_db_metrics(func):
from litellm.proxy.proxy_server import proxy_logging_obj
if "PROXY" not in func.__name__:
- await proxy_logging_obj.service_logging_obj.async_service_success_hook(
- service=ServiceTypes.DB,
- call_type=func.__name__,
- parent_otel_span=kwargs.get("parent_otel_span", None),
- duration=(end_time - start_time).total_seconds(),
- start_time=start_time,
- end_time=end_time,
- event_metadata={
- "function_name": func.__name__,
- "function_kwargs": kwargs,
- "function_args": args,
- },
+ asyncio.create_task(
+ proxy_logging_obj.service_logging_obj.async_service_success_hook(
+ service=ServiceTypes.DB,
+ call_type=func.__name__,
+ parent_otel_span=kwargs.get("parent_otel_span", None),
+ duration=(end_time - start_time).total_seconds(),
+ start_time=start_time,
+ end_time=end_time,
+ event_metadata={
+ "function_name": func.__name__,
+ "function_kwargs": kwargs,
+ "function_args": args,
+ },
+ )
)
elif (
# in litellm custom callbacks kwargs is passed as arg[0]
@@ -70,14 +73,17 @@ def log_db_metrics(func):
)
if parent_otel_span is not None:
metadata = get_litellm_metadata_from_kwargs(kwargs=passed_kwargs)
- await proxy_logging_obj.service_logging_obj.async_service_success_hook(
- service=ServiceTypes.BATCH_WRITE_TO_DB,
- call_type=func.__name__,
- parent_otel_span=parent_otel_span,
- duration=0.0,
- start_time=start_time,
- end_time=end_time,
- event_metadata=metadata,
+
+ asyncio.create_task(
+ proxy_logging_obj.service_logging_obj.async_service_success_hook(
+ service=ServiceTypes.BATCH_WRITE_TO_DB,
+ call_type=func.__name__,
+ parent_otel_span=parent_otel_span,
+ duration=0.0,
+ start_time=start_time,
+ end_time=end_time,
+ event_metadata=metadata,
+ )
)
# end of logging to otel
return result