feat(observability): expand Langfuse spans across chat pipeline
Previously the Langfuse timeline for a knowledge-chat request only showed generations (chat.completion, embedding.embed, rerank), so the work happening between them — query setup, vector/keyword search, result merging, prompt assembly — appeared as unexplained gaps. Add spans around the pieces that fill those gaps: * qa.setup wraps request-time KB / model / search-target resolution before the pipeline event loop starts, accounting for the visible delay before the first chat.completion. * pipeline.<event> wraps each pipeline stage's eventManager.Trigger call so generations inside a stage nest under it. CHAT_COMPLETION_ STREAM is intentionally skipped because its OnEvent returns as soon as the streaming goroutine starts; a stage span would always finish before the chat.completion.stream generation it nominally parents. * retrieve wraps the actual vector + keyword retrieve call inside HybridSearch, exposing the DB round-trip that previously sat invisibly between embedding generations and rerank. * web_search wraps the external web-search HTTP call when enabled.
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
@@ -32,6 +33,17 @@ func (s *sessionService) KnowledgeQA(
|
||||
req.EnableMemory,
|
||||
)
|
||||
|
||||
// Span the request setup (KB / model resolution, search target building,
|
||||
// agent override application). This covers the visible gap between trace
|
||||
// start and the first stage observation in the Langfuse timeline.
|
||||
setupCtx, setupSpan := langfuse.GetManager().StartSpan(ctx, langfuse.SpanOptions{
|
||||
Name: "qa.setup",
|
||||
Metadata: map[string]interface{}{
|
||||
"session_id": req.Session.ID,
|
||||
},
|
||||
})
|
||||
ctx = setupCtx
|
||||
|
||||
// Resolve knowledge bases using shared helper
|
||||
knowledgeBaseIDs, knowledgeIDs := s.resolveKnowledgeBases(ctx, req)
|
||||
|
||||
@@ -191,6 +203,11 @@ func (s *sessionService) KnowledgeQA(
|
||||
// Start knowledge QA event processing (set session tenant so pipeline session/message lookups use session owner)
|
||||
ctx = context.WithValue(ctx, types.SessionTenantIDContextKey, req.Session.TenantID)
|
||||
logger.Info(ctx, "Triggering question answering event")
|
||||
setupSpan.Finish(map[string]interface{}{
|
||||
"stages": len(pipeline),
|
||||
"knowledge_base_ids": knowledgeBaseIDs,
|
||||
"search_targets": len(searchTargets),
|
||||
}, nil, nil)
|
||||
err = s.KnowledgeQAByEvent(ctx, chatManage, pipeline)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
@@ -536,8 +553,40 @@ func (s *sessionService) KnowledgeQAByEvent(ctx context.Context,
|
||||
pipelineStart := time.Now()
|
||||
for _, eventType := range eventList {
|
||||
stageStart := time.Now()
|
||||
err := s.eventManager.Trigger(ctx, eventType, chatManage)
|
||||
// Wrap each pipeline stage in a Langfuse span so the trace timeline
|
||||
// shows the gaps between LLM/embedding/rerank generations (the work
|
||||
// that happens between them — vector DB search, merge, filter, prompt
|
||||
// assembly — was previously invisible). Generations created inside
|
||||
// the stage automatically nest under this span.
|
||||
//
|
||||
// CHAT_COMPLETION_STREAM is intentionally skipped: its OnEvent kicks
|
||||
// off a streaming goroutine and returns immediately, so a span would
|
||||
// finish well before the chat.completion.stream generation does. The
|
||||
// generation already captures the full stream duration; adding a
|
||||
// stage span here would just produce a child observation that
|
||||
// visually exceeds its parent.
|
||||
stageCtx := ctx
|
||||
var stageSpan *langfuse.Span
|
||||
if eventType != types.CHAT_COMPLETION_STREAM {
|
||||
stageCtx, stageSpan = langfuse.GetManager().StartSpan(ctx, langfuse.SpanOptions{
|
||||
Name: "pipeline." + string(eventType),
|
||||
Metadata: map[string]interface{}{
|
||||
"event_type": string(eventType),
|
||||
"session_id": chatManage.SessionID,
|
||||
},
|
||||
})
|
||||
}
|
||||
err := s.eventManager.Trigger(stageCtx, eventType, chatManage)
|
||||
stageDuration := time.Since(stageStart)
|
||||
var spanErr error
|
||||
if err != nil && err != chatpipeline.ErrSearchNothing {
|
||||
spanErr = err.Err
|
||||
}
|
||||
if stageSpan != nil {
|
||||
stageSpan.Finish(map[string]interface{}{
|
||||
"duration_ms": stageDuration.Milliseconds(),
|
||||
}, nil, spanErr)
|
||||
}
|
||||
|
||||
if err == chatpipeline.ErrSearchNothing {
|
||||
common.PipelineWarn(ctx, "Pipeline", "stage_fallback", map[string]interface{}{
|
||||
@@ -664,7 +713,19 @@ func (s *sessionService) SearchKnowledge(ctx context.Context,
|
||||
|
||||
for _, event := range searchEvents {
|
||||
logger.Infof(ctx, "Starting to trigger search event: %v", event)
|
||||
err := s.eventManager.Trigger(ctx, event, chatManage)
|
||||
stageCtx, stageSpan := langfuse.GetManager().StartSpan(ctx, langfuse.SpanOptions{
|
||||
Name: "pipeline." + string(event),
|
||||
Metadata: map[string]interface{}{
|
||||
"event_type": string(event),
|
||||
"flow": "search_knowledge",
|
||||
},
|
||||
})
|
||||
err := s.eventManager.Trigger(stageCtx, event, chatManage)
|
||||
var spanErr error
|
||||
if err != nil && err != chatpipeline.ErrSearchNothing {
|
||||
spanErr = err.Err
|
||||
}
|
||||
stageSpan.Finish(nil, nil, spanErr)
|
||||
|
||||
if err == chatpipeline.ErrSearchNothing {
|
||||
logger.Warnf(ctx, "Event %v triggered, search result is empty", event)
|
||||
|
||||
Reference in New Issue
Block a user