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:
@@ -13,17 +13,13 @@ import (
|
||||
// CloneContext call would drop the trace and downstream LLM wrappers would
|
||||
// each auto-create their own shallow trace, fragmenting a single HTTP request
|
||||
// into many unrelated traces in the Langfuse UI.
|
||||
//
|
||||
// Span parenting is NOT carried by this key: it flows through the standard
|
||||
// OpenTelemetry context (trace.SpanFromContext), which tracer.Start wires up
|
||||
// automatically. The *Trace on this key is only for handlers/middleware that
|
||||
// need to set trace-level input/output.
|
||||
var traceCtxKey = types.LangfuseTraceContextKey
|
||||
|
||||
// parentObsCtxKey tracks the current "parent observation" id — i.e. the span
|
||||
// that encloses any generation/span started under it. Unlike traceCtxKey, this
|
||||
// key is private: spans are a pure tracer concern and logger.CloneContext
|
||||
// should reset the parent across request boundaries (we don't want a leftover
|
||||
// span id from one request leaking into another's LLM calls).
|
||||
type parentObsCtxKeyType struct{}
|
||||
|
||||
var parentObsCtxKey = parentObsCtxKeyType{}
|
||||
|
||||
// withTrace stores a *Trace on the context so downstream LLM wrappers can
|
||||
// attach their generations to it.
|
||||
func withTrace(ctx context.Context, t *Trace) context.Context {
|
||||
@@ -47,21 +43,3 @@ func traceFromCtx(ctx context.Context) (*Trace, bool) {
|
||||
func TraceFromContext(ctx context.Context) (*Trace, bool) {
|
||||
return traceFromCtx(ctx)
|
||||
}
|
||||
|
||||
// withParentObservation stores the id of the enclosing span so children
|
||||
// (sub-spans or generations) can attach via parentObservationId.
|
||||
func withParentObservation(ctx context.Context, id string) context.Context {
|
||||
if ctx == nil || id == "" {
|
||||
return ctx
|
||||
}
|
||||
return context.WithValue(ctx, parentObsCtxKey, id)
|
||||
}
|
||||
|
||||
// parentObservationFromCtx returns the enclosing span id if any.
|
||||
func parentObservationFromCtx(ctx context.Context) (string, bool) {
|
||||
if ctx == nil {
|
||||
return "", false
|
||||
}
|
||||
v, ok := ctx.Value(parentObsCtxKey).(string)
|
||||
return v, ok && v != ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user