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:
@@ -1,9 +1,13 @@
|
||||
// Package langfuse implements a lightweight client for the Langfuse ingestion
|
||||
// API (https://langfuse.com/docs/api). It lets WeKnora record LLM traces,
|
||||
// generations and token usage in Langfuse without pulling in a heavy SDK.
|
||||
// Package langfuse implements WeKnora's Langfuse/LiteFuse LLM tracing client on
|
||||
// top of the OpenTelemetry Go SDK. It records chat/embedding/rerank/VLM/ASR
|
||||
// generations plus the surrounding agent/HTTP/asynq spans as OTLP/HTTP spans
|
||||
// to a Langfuse v3+ or LiteFuse backend (POST /api/public/otel/v1/traces).
|
||||
//
|
||||
// The integration is fully opt-in: when disabled (the default), all public
|
||||
// entry points are cheap no-ops, so callers can wire them unconditionally.
|
||||
// The integration is fully opt-in: when disabled (the default), every public
|
||||
// entry point is a cheap no-op, so callers can wire them unconditionally. A
|
||||
// W3C traceparent propagated from upstream callers (e.g. sop3) is inherited so
|
||||
// WeKnora's trace shares the upstream trace id — correlating runs across
|
||||
// services in the LiteFuse UI.
|
||||
package langfuse
|
||||
|
||||
import (
|
||||
@@ -12,6 +16,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
// Config holds the runtime configuration for the Langfuse client.
|
||||
@@ -45,6 +51,13 @@ type Config struct {
|
||||
SampleRate float64
|
||||
// Debug enables verbose logging of batch send errors.
|
||||
Debug bool
|
||||
|
||||
// testExporter, when non-nil, replaces the real OTLP exporter. Tests use
|
||||
// it to inject an in-memory span exporter (tracetest.InMemoryExporter) so
|
||||
// they can assert on exported spans deterministically without an HTTP
|
||||
// server. Init registers it with a SimpleSpanProcessor (synchronous
|
||||
// export on span End) when set. Unexported — production never sets it.
|
||||
testExporter sdktrace.SpanExporter
|
||||
}
|
||||
|
||||
// LoadConfigFromEnv builds a Config by reading the LANGFUSE_* environment
|
||||
|
||||
Reference in New Issue
Block a user