fix: ensure arrival_time is set before calculating queue time#21918
fix: ensure arrival_time is set before calculating queue time#21918Harshit28j merged 1 commit intoBerriAI:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR fixes a bug where the
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| litellm/proxy/common_request_processing.py | Correct bug fix: moved queue_time_seconds calculation after add_litellm_data_to_request which sets arrival_time. Added import time at module level. Remaining changes are formatting-only (line wrapping, trailing whitespace). |
| tests/test_litellm/proxy/test_common_request_processing.py | Added regression test for queue_time_seconds. Test uses proper mocking (no real network calls). Minor style issue: import time inside a mock function instead of at module level. Remaining changes are formatting-only. |
Sequence Diagram
sequenceDiagram
participant Client
participant Proxy as common_processing_pre_call_logic
participant Utils as add_litellm_data_to_request
participant Metrics as Prometheus Metrics
Client->>Proxy: HTTP Request
Proxy->>Proxy: start_time = datetime.now()
Note over Proxy: BEFORE FIX: tried to read arrival_time here (always None)
Proxy->>Utils: add_litellm_data_to_request(data)
Utils->>Utils: arrival_time = time.time()
Utils->>Utils: data["proxy_server_request"]["arrival_time"] = arrival_time
Utils-->>Proxy: return data (with arrival_time set)
Note over Proxy: AFTER FIX: reads arrival_time here (correctly populated)
Proxy->>Proxy: queue_time = time.time() - arrival_time
Proxy->>Proxy: metadata["queue_time_seconds"] = queue_time
Proxy->>Metrics: Record litellm_request_queue_time_seconds
Last reviewed commit: 9fc3c77
|
|
||
| async def mock_add_litellm_data_to_request(*args, **kwargs): | ||
| data = kwargs.get("data", args[0] if args else {}) | ||
| # Simulate what add_litellm_data_to_request does: set arrival_time |
There was a problem hiding this comment.
Move import time to module level
Per project guidelines (CLAUDE.md: "Avoid imports within methods — place all imports at the top of the file"), import time should be at the top of the file rather than inside this mock function.
| # Simulate what add_litellm_data_to_request does: set arrival_time |
Context Used: Context from dashboard - CLAUDE.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Relevant issues
Fixes the
litellm_request_queue_time_secondsPrometheus metric which was previously reporting as 0/None becausearrival_timewas being accessed before it was initialized in the request metadata.Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory (at least 1 test added)- Added
test_queue_time_seconds_is_set_in_metadataintests/test_litellm/proxy/test_common_request_processing.pymake test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting maintainer reviewCI (LiteLLM team)
Link:
Link:
Links:
Type
🐛 Bug Fix
✅ Test
Changes
litellm/proxy/common_request_processing.pyto move thearrival_timeinitialization (viaadd_litellm_data_to_request) before the calculation ofqueue_time_seconds.import timetocommon_request_processing.pyto support high-precision timestamp calculations.tests/test_litellm/proxy/test_common_request_processing.pythat mocks the request flow and verifiesqueue_time_secondsis correctly populated and non-negative in the request metadata.Verification:
Verified by sending a request via Postman to the proxy and then checking the /metrics endpoint. The metric litellm_request_queue_time_seconds_sum now correctly records the processing delay instead of being empty or zero