Uh oh!
There was an error while loading. Please reload this page.
feat(obs): add ttft_ms (time to first token) to UsageEvent - #276
Conversation
Add a ttft_ms field (time to first token, milliseconds) to the UsageEvent struct. On the streaming path, record the elapsed time from request entry to the first upstream SSE chunk. Non-streaming, error, and cache-hit paths emit 0 (omitted from the wire via skip_serializing_if). Also wire ttft_ms into the OTLP trace export as aisix.ttft_ms span attribute when > 0.
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR adds time-to-first-token (TTFT) telemetry for chat completions. The ChangesTTFT Telemetry Integration
🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Comment |
There was a problem hiding this comment.
Pull request overview
Adds TTFT telemetry to usage/observability so streaming chat requests can report first-token latency through UsageEvent and OTLP traces.
Changes:
- Added
ttft_mstoUsageEvent, omitted when zero. - Wired streaming completion telemetry to carry TTFT from SSE processing.
- Added OTLP trace attribute emission and serialization tests.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
crates/aisix-proxy/src/chat.rs | Captures TTFT during streaming and forwards it into usage telemetry. |
crates/aisix-obs/src/usage.rs | Adds the ttft_ms UsageEvent field and serialization coverage. |
crates/aisix-obs/src/otlp_http_sink.rs | Emits aisix.ttft_ms OTLP span attribute when set. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Uh oh!
There was an error while loading. Please reload this page.
OpenAI streams emit a role-only chunk before actual content. Record ttft_ms only when the first chunk carrying delta.content or delta.tool_calls arrives.
Add a
ttft_msfield toUsageEventto track first-token latency on the streaming path.What changed:
UsageEventgainsttft_ms: u32— time from request entry to first upstream SSE chunk carryingdelta.contentordelta.tool_calls(milliseconds). Role-only chunks are skipped. Omitted from the wire when 0 (backwards-compatible with older cp-api).build_sse_streamrecords elapsed time on the first content/tool-call chunk, stored inStreamCompletionand propagated throughUsageExtras→emit_usage_event.ttft_ms = 0.aisix.ttft_msspan attribute when > 0.Files:
crates/aisix-obs/src/usage.rs— new field + serialization testscrates/aisix-proxy/src/chat.rs— TTFT capture + wiringcrates/aisix-obs/src/otlp_http_sink.rs— OTLP attribute + testFollow-up: cp-api side (telemetryEvent struct, DB migration, dashboard) to persist and surface TTFT.
Summary by CodeRabbit
New Features
Tests