feat(observability): log end-to-end TTFB on both ends of chat stream
To diagnose where latency lives between "user hits send" and "first token appears" we need a single number that can be matched across the browser console and the server log. Add correlated TTFB markers keyed by X-Request-ID: Frontend (streame.ts) * Generate the X-Request-ID once and reuse it for logging. * Log request:start when fetchEventSource is invoked, response:headers when onopen fires, and response:first_answer the first time an SSE payload with response_type === 'answer' arrives. Filtering by event type avoids treating session_title / references / tool_call as the "first token". Backend (session handler) * parseQARequest records the wall-clock entry time and logs TTFB:start with the same X-Request-ID. * qaRequestContext carries receivedAt through to the stream handler. * AgentStreamHandler emits a one-shot TTFB:first_answer_chunk log the first time it observes an AgentFinalAnswerData chunk, so the delta against TTFB:start is the server's request-in → first-token-out budget. The frontend delta minus the backend delta is then attributable to network + gin middleware, which were previously invisible.
This commit is contained in:
@@ -24,6 +24,7 @@ type qaRequestContext struct {
|
||||
c *gin.Context
|
||||
sessionID string
|
||||
requestID string
|
||||
receivedAt time.Time // Wall-clock time the handler started processing the request
|
||||
query string
|
||||
session *types.Session
|
||||
customAgent *types.CustomAgent
|
||||
@@ -63,8 +64,11 @@ func (rc *qaRequestContext) buildQARequest() *types.QARequest {
|
||||
|
||||
// parseQARequest parses and validates a QA request, returns the request context
|
||||
func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestContext, *CreateKnowledgeQARequest, error) {
|
||||
receivedAt := time.Now()
|
||||
ctx := logger.CloneContext(c.Request.Context())
|
||||
logger.Infof(ctx, "[%s] Start processing request", logPrefix)
|
||||
requestID := secutils.SanitizeForLog(c.GetString(types.RequestIDContextKey.String()))
|
||||
logger.Infof(ctx, "[%s] TTFB:start request_id=%s received_at=%d",
|
||||
logPrefix, requestID, receivedAt.UnixMilli())
|
||||
|
||||
// Get session ID from URL parameter
|
||||
sessionID := secutils.SanitizeForLog(c.Param("session_id"))
|
||||
@@ -204,7 +208,8 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
ctx: ctx,
|
||||
c: c,
|
||||
sessionID: sessionID,
|
||||
requestID: secutils.SanitizeForLog(c.GetString(types.RequestIDContextKey.String())),
|
||||
requestID: requestID,
|
||||
receivedAt: receivedAt,
|
||||
query: secutils.SanitizeForLog(request.Query),
|
||||
session: session,
|
||||
customAgent: customAgent,
|
||||
@@ -355,7 +360,7 @@ func (h *Handler) setupSSEStream(reqCtx *qaRequestContext, generateTitle bool) *
|
||||
|
||||
// Setup stream handler
|
||||
h.setupStreamHandler(asyncCtx, reqCtx.sessionID, reqCtx.assistantMessage.ID,
|
||||
reqCtx.requestID, reqCtx.assistantMessage, eventBus)
|
||||
reqCtx.requestID, reqCtx.receivedAt, reqCtx.assistantMessage, eventBus)
|
||||
|
||||
// Generate title if needed
|
||||
if generateTitle && reqCtx.session.Title == "" {
|
||||
|
||||
Reference in New Issue
Block a user