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:
wizardchen
2026-05-12 21:11:54 +08:00
committed by lyingbug
parent 64b20a2d87
commit f740e7cecf
3 changed files with 100 additions and 5 deletions
@@ -6,6 +6,7 @@ import (
"github.com/Tencent/WeKnora/internal/application/service/retriever"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/embedding"
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
"github.com/Tencent/WeKnora/internal/types"
)
@@ -133,9 +134,30 @@ func (s *knowledgeBaseService) HybridSearch(ctx context.Context,
return nil, nil
}
// Execute retrieval using the configured engines
// Execute retrieval using the configured engines.
// A dedicated span captures the actual vector/keyword DB round-trip
// — this is the time previously visible in Langfuse only as the gap
// between embedding generations and the rerank call.
logger.Infof(ctx, "Starting retrieval, parameter count: %d", len(retrieveParams))
retrieveResults, err := retrieveEngine.Retrieve(ctx, retrieveParams)
retrieverTypes := make([]string, 0, len(retrieveParams))
for _, rp := range retrieveParams {
retrieverTypes = append(retrieverTypes, string(rp.RetrieverType))
}
retrieveCtx, retrieveSpan := langfuse.GetManager().StartSpan(ctx, langfuse.SpanOptions{
Name: "retrieve",
Input: map[string]interface{}{
"kb_ids": searchKBIDs,
"match_count": matchCount,
"retrievers": retrieverTypes,
},
Metadata: map[string]interface{}{
"param_count": len(retrieveParams),
},
})
retrieveResults, err := retrieveEngine.Retrieve(retrieveCtx, retrieveParams)
retrieveSpan.Finish(map[string]interface{}{
"result_count": len(retrieveResults),
}, nil, err)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
"knowledge_base_ids": searchKBIDs,