Merge pull request #4909 from idris/fix-datadog-attributes

Fix Datadog logging attributes
This commit is contained in:
Krish Dholakia 2024-07-26 14:19:10 -07:00 committed by GitHub
commit c0717133a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,6 +9,20 @@ import litellm, uuid
from litellm._logging import print_verbose, verbose_logger from litellm._logging import print_verbose, verbose_logger
def make_json_serializable(payload):
for key, value in payload.items():
try:
if isinstance(value, dict):
# recursively sanitize dicts
payload[key] = make_json_serializable(value.copy())
if not isinstance(value, (str, int, float, bool, type(None))):
# everything else becomes a string
payload[key] = str(value)
except:
# non blocking if it can't cast to a str
pass
class DataDogLogger: class DataDogLogger:
# Class variables or attributes # Class variables or attributes
def __init__( def __init__(
@ -61,7 +75,7 @@ class DataDogLogger:
id = response_obj.get("id", str(uuid.uuid4())) id = response_obj.get("id", str(uuid.uuid4()))
usage = dict(usage) usage = dict(usage)
try: try:
response_time = (end_time - start_time).total_seconds() response_time = (end_time - start_time).total_seconds() * 1000
except: except:
response_time = None response_time = None
@ -91,12 +105,12 @@ class DataDogLogger:
"id": id, "id": id,
"call_type": call_type, "call_type": call_type,
"cache_hit": cache_hit, "cache_hit": cache_hit,
"startTime": start_time, "start_time": start_time,
"endTime": end_time, "end_time": end_time,
"responseTime (seconds)": response_time, "response_time": response_time,
"model": kwargs.get("model", ""), "model": kwargs.get("model", ""),
"user": kwargs.get("user", ""), "user": kwargs.get("user", ""),
"modelParameters": optional_params, "model_parameters": optional_params,
"spend": kwargs.get("response_cost", 0), "spend": kwargs.get("response_cost", 0),
"messages": messages, "messages": messages,
"response": response_obj, "response": response_obj,
@ -104,13 +118,7 @@ class DataDogLogger:
"metadata": clean_metadata, "metadata": clean_metadata,
} }
# Ensure everything in the payload is converted to str make_json_serializable(payload)
for key, value in payload.items():
try:
payload[key] = str(value)
except:
# non blocking if it can't cast to a str
pass
import json import json
payload = json.dumps(payload) payload = json.dumps(payload)