Skip to content

Commit e694cc1

Browse files
feat: Add Spend metrics in datadog
1 parent 6156590 commit e694cc1

File tree

3 files changed

+264
-125
lines changed

3 files changed

+264
-125
lines changed

litellm/integrations/datadog/datadog_llm_obs.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,27 @@ async def async_send_batch(self):
148148
),
149149
),
150150
}
151-
verbose_logger.debug("payload %s", json.dumps(payload, indent=4))
151+
# serialize datetime objects - for budget reset time in spend metrics
152+
import json
153+
from datetime import datetime, date
154+
155+
def custom_json_encoder(obj):
156+
if isinstance(obj, (datetime, date)):
157+
return obj.isoformat()
158+
raise TypeError(f"Object of type {type(obj)} is not JSON serializable")
159+
160+
# Serialize payload with custom encoder for debugging
161+
try:
162+
verbose_logger.debug("payload %s", json.dumps(payload, indent=4, default=custom_json_encoder))
163+
except Exception as debug_error:
164+
verbose_logger.debug("payload serialization failed: %s", str(debug_error))
165+
166+
# Convert payload to JSON string with custom encoder for HTTP request
167+
json_payload = json.dumps(payload, default=custom_json_encoder)
168+
152169
response = await self.async_client.post(
153170
url=self.intake_url,
154-
json=payload,
171+
content=json_payload,
155172
headers={
156173
"DD-API-KEY": self.DD_API_KEY,
157174
"Content-Type": "application/json",

litellm/types/integrations/datadog_llm_obs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,4 @@ class DDLLMObsLatencyMetrics(TypedDict, total=False):
8585
class DDLLMObsSpendMetrics(TypedDict, total=False):
8686
litellm_spend_metric: float
8787
litellm_api_key_max_budget_metric: float
88-
litellm_remaining_api_key_budget_metric: float
89-
litellm_api_key_budget_remaining_hours_metric: float
88+
litellm_api_key_budget_remaining_hours_metric: float

0 commit comments

Comments
 (0)