Skip to content

fix(trace): send Langfuse traces over OpenTelemetry - #45606

Merged
GareArc merged 3 commits into
mainfrom
fix/langfuse-otel-ingestion
Nov 5, 2026
Merged

GareArc merged 3 commits into
mainfrom
fix/langfuse-otel-ingestion

Conversation

@GareArc

@GareArc GareArc commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes #41542

Langfuse v4 rejects trace, span, and generation events on POST /api/public/ingestion (HTTP 207 with a 400 for every event), and Langfuse Cloud retires that endpoint on 2026-11-16. After #44673 the Langfuse provider still posts there, so every Dify trace to Langfuse Cloud or to self-hosted v4 is dropped.

This switches LangfuseProvider.send to OTLP protobuf through the shared dify_trace_sdk.otlp encoder and egress, the same path the Phoenix provider uses. Only the Langfuse provider changes. The pipeline, the SDK runtime, the provider config, and the other providers are untouched.

 LangfuseProvider.send(config, trace)
-  build trace-create / span-create / generation-create events
-  POST {host}/api/public/ingestion            (JSON batches ≤ 3 MB, 207 per-event results)
+  encode_trace(trace) with langfuse.* attributes
+  POST {host}/api/public/otel/v1/traces       (protobuf requests ≤ 3 MB, x-langfuse-ingestion-version: 4)
+  any 2xx = accepted (Langfuse replies with its own body, not an OTLP ack)

Mapping:

Dify span Langfuse attribute
kind (LLM / chain / tool / retriever / agent) langfuse.observation.type = generation / chain / tool / retriever / agent
inputs, outputs (chat preview format kept) langfuse.observation.input / .output
attributes langfuse.observation.metadata.<key>
model, usage, USD cost, time to first token .model.name, .usage_details, .cost_details, .completion_start_time
trace user, session, root name langfuse.user.id, langfuse.session.id, langfuse.trace.name on every span (Langfuse v4 filters per observation)
root attributes + dify_trace_id + external_trace_id langfuse.trace.metadata.<key> on the root span only
error status and message OTel span status (Langfuse sets level ERROR and the status message from it)

Retries follow the shared OTLP classification. A transient failure before anything was sent is retried. An unknown outcome, or a failure after part of a split unit was accepted, is not retried, because Langfuse v4 does not deduplicate re-sent spans.

Supersedes #41543 and #43705, which were written against the provider before #44673.

Screenshots

N/A (backend). Evidence:

Before After
New Langfuse Cloud project: old provider → ExportRejectedError: Langfuse rejected 2 ingestion events Same project: every request 200, including a ~4.5 MB trace split into two requests
Read back through GET /api/public/v2/observations: 207 of 209 field checks match what was sent (IDs, parents, types, times, level, input/output, metadata, user/session, model, usage, cost, TTFT, no duplicates). The 2 misses are Langfuse moving 2.25 MB inputs to its media storage.
Dev deployment, real workflow run (loop, iteration, 15 LLM and 14 tool nodes): all 91 node executions in the database found in Langfuse by span ID, plus the root and 14 loop/iteration rounds. Names, types, times, status, model, and tokens (62,409 = the run's total_tokens) match.
Old vs new provider on the same SDK sample traces: 169 field checks, no data lost.

Behavior changes for users

No Dify config, database, or queue changes. Visible in Langfuse:

  • Requires a Langfuse server with the OTel endpoint and langfuse.* attributes: self-hosted v3.63.0 or later (the same floor as Langfuse's Python SDK). Older servers answer 404 and the trace is dropped with a warning.
  • Trace IDs are the 32-hex OTel IDs: a message ID loses its dashes, and a non-UUID X-Trace-Id is hashed (kept as trace metadata external_trace_id). Observation IDs are the 16-hex span IDs. Existing traces and stored links are unchanged.
  • The message or workflow root is now a root observation (OTel has no separate trace record).
  • Non-LLM spans use the matching Langfuse types instead of SPAN.
  • Proxies in front of Langfuse must allow POST /api/public/otel/v1/traces with application/x-protobuf.

Merge Danger

Door: two-way. Reverting restores the legacy request (which Langfuse Cloud stops accepting on 2026-11-16).

Blast Radius: Langfuse tracing only.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

Langfuse v4 rejects trace, span, and generation events on the legacy
ingestion API, and Langfuse Cloud retires it on 2026-11-16. Send each
trace unit as OTLP protobuf to /api/public/otel/v1/traces instead,
using the shared OTLP encoder and egress.

Fixes #41542
@GareArc
GareArc requested a review from QuantumGhost as a code owner November 5, 2026 03:49
@github-actions github-actions Bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 83.38% 83.38% +0.00%
Strict coverage 83.04% 83.04% +0.00%
Typed symbols 86,362 86,375 +13
Untyped symbols 17,284 17,284 0
Modules 4388 4388 0

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.64865% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.89%. Comparing base (2d5c83d) to head (dea90f8).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
...ders/trace/trace-sdk/src/dify_trace_sdk/testing.py 84.21% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #45606    +/-   ##
========================================
  Coverage   89.88%   89.89%            
========================================
  Files        6245     6246     +1     
  Lines      391156   391273   +117     
  Branches    78436    78451    +15     
========================================
+ Hits       351599   351728   +129     
+ Misses      33453    33444     -9     
+ Partials     6104     6101     -3     
Flag Coverage Δ
api 89.68% <98.19%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GareArc
GareArc added this pull request to the merge queue Nov 5, 2026
Merged via the queue into main with commit 0be453d Nov 5, 2026
69 checks passed
@GareArc
GareArc deleted the fix/langfuse-otel-ingestion branch November 5, 2026 07:30
hjlarry pushed a commit that referenced this pull request Nov 9, 2026
fatelei pushed a commit to fatelei/dify that referenced this pull request Nov 9, 2026
@wylswz wylswz mentioned this pull request Nov 17, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Langfuse v4 events_only mode silently drops all Dify traces (legacy ingestion API)

2 participants