Choose the Right OpenTelemetry Setting
Dify can send traces to an OpenTelemetry collector in three ways. Pick by what you want to trace:- What your apps and Agents do, run by run, with their nodes and model calls: the OpenTelemetry tracing provider, set per app or Agent.
- How the Dify servers perform, such as web requests, database calls, and background jobs:
ENABLE_OTELand theOTLP_*variables, with one endpoint for the whole deployment. See OTLP / OpenTelemetry Configuration for details. - Every Agent run across all workspaces, in one collector: Agent trajectory export (
DIFY_AGENT_TRAJECTORY_*). See Agent Trajectory Export for details.
Before You Start
You need:- Dify 1.20.0 or later.
- An endpoint for OpenTelemetry traces over HTTP: an OpenTelemetry Collector, or a backend that accepts OpenTelemetry directly. Dify sends traces in the OpenTelemetry Protocol (OTLP) over HTTP, not gRPC. On a Collector, that’s the HTTP receiver, usually on port 4318.
-
Credentials as HTTP headers, if the endpoint needs them, such as an
Authorizationheader or a vendor’s API-key header.
-
A public
https://endpoint: only port 443 gets through. To reach a Collector on its usual port 4318, put a reverse proxy or load balancer on 443 in front of it, as inhttps://otel.example.com/v1/traces. -
A public
http://endpoint: most ports work, 4318 included, but traces and your credentials cross the internet unencrypted. Preferhttps://on 443 for anything outside your own network. -
A collector on your own network: the proxy refuses private, loopback, and link-local addresses until you allow the collector’s address. An
https://collector there still needs port 443.To allow your collector, set the variable that matches the address you’ll give Dify for it:SSRF_PROXY_ALLOW_PRIVATE_DOMAINS= its host name, such as the Docker Compose service name inhttp://otel-collector:4318SSRF_PROXY_ALLOW_PRIVATE_IPS= its IP address
Connect Dify to Your Endpoint
Configuring an app’s tracing requires the Owner or Admin role, or the Editor role on an app you created. For an Agent, you need the Editor role or above.
- In the Monitoring page of your app or Agent, click Tracing app performance.
-
Click Config and fill in the form:
-
Endpoint: the URL that receives your traces. Dify adds
/v1/tracesto a bare address such ashttps://otel.example.comand uses any other path as is. Leave credentials and?queries out of the URL, and put credentials in HTTP headers (JSON) instead. -
HTTP headers (JSON): the headers your endpoint needs, such as
{"Authorization":"Bearer <token>"}. Leave out transport headers such asContent-Type,Host,Accept, andContent-Encoding, even if your backend’s setup guide lists them. Dify never shows saved headers again and clears them when you change Endpoint, so keep your own copy. -
Service name: the name your backend groups traces under (
service.name). It’sdifyunless you change it. Give each app and Agent its own name, so you can find their traces later. -
Resource attributes (JSON): labels added to every trace from this app, such as
{"deployment.environment.name":"production"}. Setservice.namein Service name.
-
Endpoint: the URL that receives your traces. Dify adds
- Click Save & Enable. Dify tests the endpoint before saving.
If Save Fails
The tracing configuration could not be verified.: Dify couldn’t reach the endpoint, or the endpoint refused the test. Check the URL’s path and port, the proxy rules in Before You Start, and the header names and values your backend expects.The tracing configuration is invalid.: a field breaks one of the rules above, such as a JSON typo, a transport header, or a?query in Endpoint.
Check That Traces Arrive
Run the whole app once, in the editor or as a user, then search your backend for its service name. Testing a single node doesn’t send the app’s trace. This run is also the real test of your credentials. Some collectors accept Save’s test without checking them. If the trace isn’t there yet, wait a few minutes. A trace that Dify is retrying can take five minutes or more to arrive. If a trace is still missing, search the logs of Dify’sworker service for Trace export. Most traces Dify drops are logged there with the reason, such as OpenTelemetry ingestion rejected (HTTP 401).
Once Agent tracing is on, Agent runs also show their delivery status in Dify, in the External tracing row of their log details.
Read the Traces in Your Backend
How the spans in a trace nest depends on the app type:-
Workflow:
workflowat the root, then one span per node, named<node type>_<node title>, such asllm_LLM. Each node’s span sits under the node that ran before it, so a straight workflow forms a staircase:Each span still covers only its own node’s time. -
Chatflow:
messageat the root,workflowunder it, then the node spans as in a Workflow app. -
Chatbot, Text Generator, and legacy Agent apps:
messageat the root. Under it sit anllmspan and one span for each other step, such as knowledge retrieval or a tool call. Thellmspan lasts as long as the whole message, not just the model call. - Agents: like a Chatbot app, until you turn on Agent tracing.
Attributes to Filter On
Filter and group traces by these span attributes, for example to chart token use per model fromgen_ai.request.model and gen_ai.usage.total_tokens:
Token counts and cost appear as attributes only on LLM spans, so summing them leaves out Question Classifier, Parameter Extractor, and other nodes that call a model.
For a run’s full token count, read
total_tokens in the dify.metadata of its workflow span. Each node’s span carries its own total_tokens and total_price in the same place. In Agent runs with Capture content off, dify.metadata leaves these out, though LLM spans still carry their token and cost attributes.
Find a Particular Run
Start by narrowing to the app with the service name you gave it. If several apps share a name, filter ondify.app_id instead. It’s the ID in the app’s URL in Dify (/app/<app ID>/…), and most of the app’s spans carry it.
Agents need a service name of their own, because their spans carry a different app ID from the one in the Agent’s URL.
Within the app, filter on the ID you have, as the API returns it, dashes included:
- A Workflow run started through the API:
dify.workflow_run_id, from the response’sworkflow_run_id. - A message sent through the API to a Chatbot, Text Generator, or legacy Agent app:
dify.message_id, from the response’smessage_id. - A conversation in a Chatbot, Chatflow, legacy Agent app, or Agent:
gen_ai.session.id, from the response’sconversation_idor the Conversation ID in the app’s logs. The logs show that ID in capitals, so copy it with its copy button. - Any other run: the time it started.
X-Trace-Session-Id header when you start the run through the API. Dify then stores it in gen_ai.session.id instead of the conversation or run ID, so a tagged conversation can no longer be found by its conversation ID. Agents ignore this header.
Join API Runs to Your Service’s Trace
Workflow and Chatflow runs started through the API can join your own service’s trace, so you can follow a request from your service into Dify. Once Agent tracing is on, runs of workflows with a new Agent node always get a trace of their own instead. If your service’s HTTP client sends the W3Ctraceparent header, as OpenTelemetry’s HTTP instrumentation does, the run joins with no change on your side. Otherwise, send your trace ID in the X-Trace-Id header, or as trace_id in the query string or request body.
Dify handles the ID by its form:
- 32 hexadecimal characters or a UUID: the run joins your trace. OpenTelemetry trace IDs take the first form.
- Up to 128 letters, digits,
-, and_, such asorder-123: the run doesn’t join your trace. Dify computes a trace ID from the value, so every run sent with that value lands in the same trace. - Any other value, such as
order:123: Dify ignores it, and the run gets a trace of its own.
Trace Agent Runs
Agent tracing adds an Agent’s own model and tool calls to its traces, grouped underAgent attempt <n> spans. In an Agent’s own runs, these spans sit under the root message span. In a workflow, they sit under the agent_<node title> span of the new Agent node.
Until Agent tracing is on, an Agent’s own runs look like a Chatbot app’s, and a new Agent node shows as a single span. Turning it on also changes these runs in other ways:
- Content: inputs and outputs arrive empty until you turn on Capture content. In a workflow with a new Agent node, that covers every node, not only the Agent.
- Your trace ID: each run gets a trace of its own, even when you send your trace ID through the API.
- Agent trajectory export: runs of an app or Agent with tracing on go to its tracing provider instead of the trajectory collector, unless Capture Agent internals is off.
Agent tracing is off by default. Enable it by setting both:
OPS_AGENT_TRACE_ENABLED=trueDIFY_AGENT_APPLICATION_TRACING_ENABLED=true
- Capture Agent internals: turn it off to show each Agent as a single step. The Agent’s model names, token counts, and cost are then left out too, so leave it on if you track usage.
- Capture content: turn it on to see what each step received and returned. Until then, Dify also replaces error text with a generic message, such as
Application execution failed. Model names, token counts, and cost arrive either way.
If Your Endpoint Is Down or Refuses a Trace
When Dify can’t connect to the endpoint, or the endpoint answers that it’s overloaded or failing, Dify keeps trying for about five minutes by default. Once Agent tracing is on, Agent runs get about 10 minutes.To keep traces other than Agent runs through longer outages, raise
OPS_TRACE_EXPORT_MAX_ATTEMPTS. See Environment Variables for details.- The endpoint refuses the trace for another reason, such as a wrong path, rejected credentials, or a size it doesn’t accept. The
workerlog names the reason. For a trace that’s too large, raise the endpoint’s request size limit. - The endpoint takes more than 30 seconds to answer, or the connection breaks after Dify sent the trace. If your backend is slow to answer, send to an OpenTelemetry Collector in front of it, which answers as soon as it has the trace.