Repository navigation
Export Langfuse traces with OTLP (Langfuse v4) - #66
Merged
Merged
Conversation
The format check of CI (clang-format 18) has been failing on main since #62. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Langfuse Cloud is v4-only since 2026-11-16, and v4 rejects every event type except `score-create` on the legacy `POST /api/public/ingestion` endpoint that the tracer used. Traces and observations must go to the OpenTelemetry endpoint `POST /api/public/otel/v1/traces`. The tracer now sends an OTLP/HTTP JSON `ExportTraceServiceRequest` (no protobuf dependency) with Basic auth and `x-langfuse-ingestion-version: 4`. A trace is a root span that carries the trace input/output; generations are its children and tool calls are children of the generation, as before. The trace-level attributes (`langfuse.trace.name`, `langfuse.user.id`, `langfuse.session.id`, `langfuse.trace.tags`, `langfuse.trace.metadata`, `langfuse.release`, `langfuse.environment`) are set on every span, as Langfuse v4 requires. Generations carry the model, model parameters, usage (`langfuse.observation.usage_details` plus `gen_ai.usage.*`) and metadata; failures set `langfuse.observation.level` = `ERROR`, the status message and the OTLP span status. Rejected spans reported in `partialSuccess` count as a failed export. Self-hosted Langfuse supports the OTLP endpoint since v3.22.0, so the legacy path is removed without a fallback. API changes: `Tracer::send_batch` (legacy events) is replaced by `Tracer::export_spans` (OTLP spans); `Trace::id` is now a 32-hex OpenTelemetry trace id; new `Trace::new_trace_id`, `Trace::new_span_id`, `Trace::to_unix_nanos`. A generation without `finish_generation` and unfinished tool spans are now closed at `Trace::end` instead of being dropped or left open. Closes: #59 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # tests/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Langfuse Cloud is v4-only since 2026-11-16, and v4 answers
POST /api/public/ingestionwith400for every event type exceptscore-create, so the tracer could no longer write traces to Langfuse Cloud. Thanks to @volodymyrshk for the report and the pointer to the OTLP endpoint and thex-langfuse-ingestion-version: 4header.The tracer now exports OpenTelemetry spans to
POST /api/public/otel/v1/traces:ExportTraceServiceRequest), so there is no new protobuf dependency. It uses Basic auth with the public and secret keys and sendsx-langfuse-ingestion-version: 4. Sub-path hosts still work.langfuse.trace.name,langfuse.user.id,langfuse.session.id,langfuse.trace.tags,langfuse.trace.metadata,langfuse.release,langfuse.environment) are set on every span, because Langfuse v4 needs them there for filtering.langfuse.observation.type=generation, the model (langfuse.observation.model.nameandgen_ai.request.model), the model parameters, the input messages, the output, usage (langfuse.observation.usage_detailsandgen_ai.usage.input_tokens/output_tokens), and metadata (finish_reason,steps,warnings). Tool spans get their arguments as input and their result as output.langfuse.observation.level=ERROR,langfuse.observation.status_message, and the OTLP span status. A response whosepartialSuccessreports rejected spans counts as a failed export, soConfig::ErrorPolicy::kStrictreports it.API changes in
include/ai/langfuse.h:Tracer::send_batchtook legacy ingestion events. It is replaced byTracer::export_spans, which takes OTLP spans. The name changed so that existing callers fail to compile instead of silently sending payloads the server rejects.Trace::idis now the OpenTelemetry trace id (32 hex characters), which is also the trace id that Langfuse shows.Trace::new_trace_id,Trace::new_span_id,Trace::to_unix_nanos.finish_generation, and tool spans that never got a finish callback, are now closed atTrace::end. Before, they were dropped or left open.Tests: the new
tests/unit/langfuse_tracer_test.cppruns a localhttplibserver and checks the path (including a sub-path host), the headers, the OTLP JSON structure, the hierarchy, attribute propagation, usage, error marking, and the error policy. I have not run it against a real Langfuse instance.Closes: #59
🤖 Generated with Claude Code
Note
Medium Risk
Observability export path and public API changed; misconfiguration or Langfuse version mismatch could silently break tracing until callers update to OTLP and v4-compatible hosts.
Overview
Migrates Langfuse tracing from the legacy
/api/public/ingestionbatch API to OTLP/HTTP JSON at/api/public/otel/v1/traces, withx-langfuse-ingestion-version: 4, so traces work against Langfuse Cloud v4 and self-hosted v3.22.0+. The legacy ingestion path is removed with no fallback.Each trace is now a root OpenTelemetry span; generations and tool calls are child spans with Langfuse/GenAI attributes. Trace-level fields (name, user, session, tags, metadata, etc.) are duplicated on every span for v4 filtering. IDs use 32/16 hex OTel trace/span ids; timestamps are unix nanos as strings.
API:
Tracer::send_batch→Tracer::export_spans;Trace::idis the OTel trace id; new helpersnew_trace_id,new_span_id,to_unix_nanos. Unfinished generations and open tool spans are closed atTrace::end()instead of being dropped.Docs/env notes and a unit test suite (
langfuse_tracer_test.cpp) validate OTLP payloads, hierarchy, errors, and export failure handling via a fake HTTP server.Reviewed by Cursor Bugbot for commit fd83577. Bugbot is set up for automated code reviews on this repo. Configure here.