feat(tracing): migrate langfuse client to OTLP/OTel wire protocol

Migrate WeKnora's Langfuse tracing from the legacy /api/public/ingestion
batch API to the OpenTelemetry Go SDK emitting OTLP/HTTP to
/api/public/otel/v1/traces — the Langfuse v3+ standard ingestion protocol.

Why: Langfuse v3 deprecated the legacy ingestion API in favor of OTLP, and
OTel-only backends (e.g. the LiteFuse Doris fork) dropped it entirely, so the
legacy client was incompatible. Migrating to the OTel SDK also adds W3C
traceparent cross-service propagation: an upstream caller's trace id is
inherited so its run and the WeKnora call land under one trace in the UI.

What:
- Manager owns an OTel TracerProvider backed by an otlptracehttp exporter
  (Basic auth + the x-langfuse-ingestion-version gate header). Spans are
  buffered by BatchSpanProcessor and exported complete on End, so a long
  trace no longer fragments across flush batches.
- *Trace/*Span/*Generation wrap OTel spans; the public API (StartTrace/
  Span/Generation, handles, InjectTracing, asynq/gin middleware, Config,
  TokenUsage) is unchanged, so callers (agent/chat/handler/scheduler/asynqdl)
  need no edits.
- W3C traceparent: GinMiddleware extracts traceparent from incoming requests;
  AsynqMiddleware + InjectTracing propagate it across the asynq payload.
  types.TracingContext gains a LangfuseTraceparent field.
- logger.CloneContext preserves the active OTel span across context rebuilds
  so child spans (agent engine, summary generation) stitch to the HTTP trace
  instead of orphaning into separate traces.
- StartSpan/StartGeneration re-establish the parent from the *Trace handle
  when the ctx lost the active OTel span, covering background/derived contexts.

Compatibility: works with Langfuse v3+ (cloud / self-hosted / the bundled
docker-compose profile) and LiteFuse. Drops Langfuse v2 (legacy-only) support.
Uses the OTel Go SDK already (indirectly) in go.mod, promoting
otlptracehttp to direct; no third-party langfuse-go library.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
shenk-b
2026-07-17 11:41:45 +08:00
committed by lyingbug
parent 1a2021b3f8
commit 5ee96019e0
16 changed files with 654 additions and 1013 deletions
+12 -7
View File
@@ -19,15 +19,20 @@ package types
// happen to be named similarly.
type TracingContext struct {
// LangfuseTraceID is the id of the root trace that originated this task.
// When set, workers attach their observations to the same trace instead
// of creating a standalone one, which is what makes the Langfuse UI
// show the asynq work as a child of the originating HTTP request.
// Kept for backward compatibility with legacy payloads; the OTLP path now
// propagates correlation via LangfuseTraceparent (W3C) below.
LangfuseTraceID string `json:"lf_trace_id,omitempty"`
// LangfuseParentObservationID, when set, points to the span that
// enclosed the enqueue call. The worker's own span/generation will list
// it as parentObservationId so Langfuse renders the tree as:
// http-trace → http-span → asynq-span → generation.
// LangfuseParentObservationID is retained for backward compatibility only;
// the OTLP path no longer uses it (parent linking flows through the W3C
// traceparent's parent span id).
LangfuseParentObservationID string `json:"lf_parent_obs_id,omitempty"`
// LangfuseTraceparent carries the W3C Trace Context (`traceparent` header
// value: `00-<trace_id>-<span_id>-<flags>`) from the enqueuing request.
// The worker re-extracts it so its spans are children of the same trace —
// stitching the HTTP request and the async job into one LiteFuse tree. This
// is also what propagates a sop3 run's W3C trace_id into any asynq jobs
// WeKnora enqueues while serving sop3's agent-chat call.
LangfuseTraceparent string `json:"lf_traceparent,omitempty"`
// LangfuseUserID preserves the userId / tenant label across the async
// boundary so that orphan async traces (when no upstream trace id is
// available) still show up in the Langfuse "Users" view under the