Skip to content

Export Langfuse traces with OTLP (Langfuse v4) - #66

Merged
alexey-milovidov merged 5 commits into
mainfrom
langfuse-v4-otlp
Nov 27, 2026
Merged

alexey-milovidov merged 5 commits into
mainfrom
langfuse-v4-otlp

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Langfuse Cloud is v4-only since 2026-11-16, and v4 answers POST /api/public/ingestion with 400 for every event type except score-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 the x-langfuse-ingestion-version: 4 header.

The tracer now exports OpenTelemetry spans to POST /api/public/otel/v1/traces:

  • OTLP/HTTP with JSON encoding (ExportTraceServiceRequest), so there is no new protobuf dependency. It uses Basic auth with the public and secret keys and sends x-langfuse-ingestion-version: 4. Sub-path hosts still work.
  • A trace is a root span that carries the trace input/output. Generations are its children, and tool calls are children of the generation, the same hierarchy as before. IDs are random 32-hex (trace) and 16-hex (span) values, and times are unix nanoseconds encoded as strings.
  • 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, because Langfuse v4 needs them there for filtering.
  • Generations get langfuse.observation.type = generation, the model (langfuse.observation.model.name and gen_ai.request.model), the model parameters, the input messages, the output, usage (langfuse.observation.usage_details and gen_ai.usage.input_tokens / output_tokens), and metadata (finish_reason, steps, warnings). Tool spans get their arguments as input and their result as output.
  • Errors set langfuse.observation.level = ERROR, langfuse.observation.status_message, and the OTLP span status. A response whose partialSuccess reports rejected spans counts as a failed export, so Config::ErrorPolicy::kStrict reports it.
  • Self-hosted Langfuse has supported this endpoint since v3.22.0, so the legacy path is removed and there is no fallback.

API changes in include/ai/langfuse.h:

  • Tracer::send_batch took legacy ingestion events. It is replaced by Tracer::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::id is now the OpenTelemetry trace id (32 hex characters), which is also the trace id that Langfuse shows.
  • New helpers: Trace::new_trace_id, Trace::new_span_id, Trace::to_unix_nanos.
  • A generation that was instrumented but never finished with finish_generation, and tool spans that never got a finish callback, are now closed at Trace::end. Before, they were dropped or left open.

Tests: the new tests/unit/langfuse_tracer_test.cpp runs a local httplib server 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/ingestion batch API to OTLP/HTTP JSON at /api/public/otel/v1/traces, with x-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::id is the OTel trace id; new helpers new_trace_id, new_span_id, to_unix_nanos. Unfinished generations and open tool spans are closed at Trace::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.

alexey-milovidov and others added 5 commits November 27, 2026 00:32
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>
@alexey-milovidov alexey-milovidov self-assigned this Nov 27, 2026
@alexey-milovidov
alexey-milovidov merged commit 33182db into main Nov 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Langfuse v4 cutover (Nov 16): tracer.cpp uses removed endpoints

1 participant