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
@@ -9,6 +9,7 @@ import (
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/searchutil"
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
@@ -645,7 +646,18 @@ func (p *PluginSearch) searchWebIfEnabled(ctx context.Context, chatManage *types
"tenant_id": chatManage.TenantID,
"provider_id": providerID,
})
webResults, err := p.webSearchService.Search(ctx, providerID, webConfig, chatManage.RewriteQuery)
webCtx, webSpan := langfuse.GetManager().StartSpan(ctx, langfuse.SpanOptions{
Name: "web_search",
Input: map[string]interface{}{
"provider_id": providerID,
"query": chatManage.RewriteQuery,
"max_results": webConfig.MaxResults,
},
})
webResults, err := p.webSearchService.Search(webCtx, providerID, webConfig, chatManage.RewriteQuery)
webSpan.Finish(map[string]interface{}{
"hit_count": len(webResults),
}, nil, err)
if err != nil {
pipelineWarn(ctx, "Search", "web_search_error", map[string]interface{}{
"tenant_id": chatManage.TenantID,