fix: drain datadog batches safely - #25663
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR fixes memory growth and event loss in the Datadog logger's batching path by detaching the queue before sending ( Confidence Score: 5/5Safe to merge — both prior P0/P1 issues are resolved and no new defects introduced. All previously flagged blocking issues (missing import time, silent event loss on 413) are fixed. The batch-detach pattern, requeue logic, and lock-protected flush_queue override are logically correct. The only remaining finding is a theoretical P2 edge case (batch_to_send unbound before assignment in the exception handler) that requires an already-broken logger state to trigger. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/integrations/datadog/datadog.py | Adds safe batch detach (copy+clear before send), 413/exception requeue, lock-protected flush_queue override, and missing import time; threshold flushes now correctly serialized via the lock. |
| tests/test_litellm/integrations/datadog/test_datadog_logger_batching.py | New mock-only test file covering concurrent appends during flush, 413/exception requeue, flush_queue lock guard, and last_flush_time update logic — all tests are well-isolated with no real network calls. |
Sequence Diagram
sequenceDiagram
participant H as EventHook / _log_async_event
participant FQ as flush_queue()
participant L as flush_lock
participant ASB as async_send_batch()
participant DD as Datadog API
H->>H: log_queue.append(event)
H->>H: len(queue) >= batch_size?
H->>FQ: await flush_queue()
FQ->>L: async with flush_lock
L-->>FQ: acquired
FQ->>FQ: if log_queue not empty
FQ->>ASB: await async_send_batch()
ASB->>ASB: batch_to_send = log_queue[:]
ASB->>ASB: log_queue = []
ASB->>DD: POST /api/v2/logs (batch_to_send)
alt 202 OK
DD-->>ASB: 202 Accepted
ASB-->>FQ: returns (log_queue empty)
FQ->>FQ: last_flush_time = time.time()
else 413 Too Large
DD-->>ASB: 413
ASB->>ASB: log_queue = batch_to_send + log_queue
ASB-->>FQ: returns (log_queue non-empty)
FQ->>FQ: last_flush_time NOT updated
else Exception
ASB->>ASB: log_queue = batch_to_send + log_queue
ASB-->>FQ: returns (log_queue non-empty)
FQ->>FQ: last_flush_time NOT updated
end
FQ->>L: release flush_lock
Reviews (6): Last reviewed commit: "test: use sync mock for datadog payload ..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
This PR improves the safety and correctness of the Datadog logger’s batching/flush behavior under concurrency by draining the queue before sending and routing threshold flushes through the batch flush lock, with regression tests to cover concurrent appends and threshold flushing from the failure hook.
Changes:
- Route threshold-triggered flushes (
async_post_call_failure_hook,_log_async_event) throughflush_queue()to ensure flush-lock serialization. - Detach (
copy + clear) the current Datadog batch before sending, so events appended during the send are not sent twice or lost from the active queue. - Add focused async tests for “append during send” and “failure-hook triggers flush_queue”.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
litellm/integrations/datadog/datadog.py |
Updates batching/flush flow to drain queue before send, requeue on exceptions, and route threshold flushes through flush_queue(). |
tests/test_litellm/integrations/datadog/test_datadog_logger_batching.py |
Adds regression tests validating concurrent appends during send and ensuring failure-hook threshold flush uses flush_queue(). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Addressed the review feedback in the latest signed commit. Changes made:
Verification:
Latest commit: |
b5e2fa4
into
BerriAI:litellm_oss_staging_04_13_2026_p1
* fix: drain datadog batches safely * fix: preserve datadog batches on 413 * fix: import time in datadog flush queue * test: cover datadog batching edge cases * fix: only stamp successful datadog flushes * test: use sync mock for datadog payload builder
* fix: drain datadog batches safely * fix: preserve datadog batches on 413 * fix: import time in datadog flush queue * test: cover datadog batching edge cases * fix: only stamp successful datadog flushes * test: use sync mock for datadog payload builder
Summary
Fixes #25660