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:
@@ -0,0 +1,41 @@
|
||||
package langfuse
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
// newExporter builds an OTLP/HTTP trace exporter pointed at the
|
||||
// Langfuse v3+ / LiteFuse OTel endpoint (POST /api/public/otel/v1/traces).
|
||||
//
|
||||
// Auth is HTTP Basic (public_key:secret_key). The x-langfuse-ingestion-version
|
||||
// header is the gate opt-in that LiteFuse/Langfuse v3 require for the OTel
|
||||
// direct-write path (verified against directWriteHelpers.ts — without it the
|
||||
// server returns 400 "requires Python SDK >= 4.0.0"). The x-langfuse-sdk-*
|
||||
// markers are sent for parity; WeKnora is a Go client, not the Python SDK.
|
||||
func newExporter(ctx context.Context, cfg Config) (sdktrace.SpanExporter, error) {
|
||||
endpoint := strings.TrimRight(cfg.Host, "/") + "/api/public/otel/v1/traces"
|
||||
creds := cfg.PublicKey + ":" + cfg.SecretKey
|
||||
opts := []otlptracehttp.Option{
|
||||
otlptracehttp.WithEndpointURL(endpoint),
|
||||
otlptracehttp.WithHeaders(map[string]string{
|
||||
"Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(creds)),
|
||||
"x-langfuse-ingestion-version": "4",
|
||||
"x-langfuse-sdk-name": "python",
|
||||
"x-langfuse-sdk-version": langfuseScopeVersion,
|
||||
}),
|
||||
}
|
||||
if cfg.RequestTimeout > 0 {
|
||||
opts = append(opts, otlptracehttp.WithTimeout(cfg.RequestTimeout))
|
||||
}
|
||||
exp, err := otlptracehttp.New(ctx, opts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("langfuse: build otlp exporter: %w", err)
|
||||
}
|
||||
return exp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user