Skip to content
AnswerWell geo Documentation

Research · 10 min read · Answerwell

Tracing, Custom Spans, OpenTelemetry Export, and Agent Diagnostics

How to enable Cloudflare Workers tracing, write custom spans, follow requests across RPC, and observe agent turns in the dashboard or via export.

This page summarizes Cloudflare documentation on Workers tracing, the runtime custom spans API, Workers Observability (dashboard and OpenTelemetry export), and Cloudflare Agents tracing plus diagnostics channels. If you run edge Workers, Durable Objects, or Agents and need to see where time goes or why a turn failed, that is the core topic here.

Workers tracing: what gets recorded without code changes

Section titled “Workers tracing: what gets recorded without code changes”

Workers tracing gives end-to-end visibility into a request as it moves through your Worker and platform bindings. Cloudflare states:

Tracing gives you end-to-end visibility into the life of a request as it travels through your Workers application and connected services. With automatic instrumentation, Cloudflare captures telemetry data for fetch calls, binding operations (KV, R2, Durable Objects), and handler invocations - no code changes required.

— Cloudflare, Observability — Traces

Automatic instrumentation (no SDK) covers outbound fetch(), binding calls (KV, R2, Durable Objects, D1, and similar), JavaScript RPC between Workers and Durable Objects, and handler invocations. See Workers tracing and Spans and attributes for the full span reference.

Why enable tracing in Wrangler (observability.traces.enabled)

Section titled “Why enable tracing in Wrangler (observability.traces.enabled)”

Tracing must be turned on per Worker in Wrangler; otherwise spans are not recorded. The usual setting is observability.traces.enabled = true in wrangler.json or wrangler.toml:

{
"observability": {
"traces": {
"enabled": true
}
}
}

In the future, Cloudflare plans to enable automatic tracing in addition to logs when you set observability.enabled = true in your Wrangler configuration.

While automatic tracing is in early beta, this setting will not enable tracing by default, and will only enable logs.

— Cloudflare, Workers automatic tracing, now in open beta

For high traffic, the Workers tracing docs show an optional head_sampling_rate under observability.traces. Account-level sampling and export are configured separately in the dashboard; see Traces — Configuration.

Unified trace across Worker subrequests and Durable Objects

Section titled “Unified trace across Worker subrequests and Durable Objects”

Previously, a Worker calling another Worker via a service binding or a Durable Object could produce disconnected traces. Cloudflare now propagates trace context automatically:

You can now get a single unified trace across Worker-to-Worker subrequests, with trace context propagating automatically. Previously, automatic tracing produced disconnected traces when a Worker called another Worker through a service binding or Durable Object.

— Cloudflare, Automatic tracing across Durable Object and Worker subrequests

JavaScript RPC spans go further: traces can follow RPC across Worker boundaries into Durable Objects, with session spans grouping reused sessions and per-method call spans. See Workers traces now automatically include JavaScript RPC session spans. External linking to non-Cloudflare services via W3C Trace Context is documented under Configuration — Propagate trace context; Known limitations notes full end-to-end export propagation is still evolving.

Custom spans API: instrument your own code paths

Section titled “Custom spans API: instrument your own code paths”

Platform spans cover bindings and fetch(); custom spans extend traces into your application logic. Both entry points behave the same:

  • import { tracing } from "cloudflare:workers" — usable in libraries and helpers.
  • ctx.tracing on the handler ExecutionContext — convenient inside the handler.

See Custom spans.

tracing.enterSpan(): default choice for sync and async work

Section titled “tracing.enterSpan(): default choice for sync and async work”

enterSpan() creates a child of the active span and ends automatically when the callback returns or its promise settles. Nested enterSpan calls and platform operations inside the callback nest correctly.

import { tracing } from "cloudflare:workers";
export default {
async fetch(request, env, ctx) {
return tracing.enterSpan("handleRequest", async (span) => {
span.setAttribute("url.path", new URL(request.url).pathname);
const data = await env.MY_KV.get("key");
return new Response(data);
});
},
};

— Cloudflare, Workers tracing now supports custom spans

Use span.isTraced before expensive attribute work when sampling might discard spans (Custom spans — span.isTraced).

tracing.startActiveSpan() and span.end(): streams and long-lived work

Section titled “tracing.startActiveSpan() and span.end(): streams and long-lived work”

Use startActiveSpan() when the span must stay open after the callback returns—for example a ReadableStream that finishes later. You must call span.end(); do not rely on the runtime backstop.

The Workers runtime now provides built-in tracing.startActiveSpan() and span.end() APIs, allowing you to write custom spans for operations that last beyond a single callback — for example, instrumenting a stream pipeline where the span should stay open until the stream is fully consumed.

— Cloudflare, Workers tracing — write custom spans with new startActiveSpan() and span.end() runtime APIs

After the callback returns, the span is no longer the active parent, so child spans started later are not nested under it (Custom spans — startActiveSpan).

tracing.startSpan(): measure work without nesting under the span

Section titled “tracing.startSpan(): measure work without nesting under the span”

startSpan() returns a span that is never active; sibling spans do not nest under it. End it explicitly with span.end(). Useful when start and end are in different functions and you do not need children under that span (Custom spans — startSpan).

tracing.getActiveSpan(): annotate without passing the span object

Section titled “tracing.getActiveSpan(): annotate without passing the span object”

Outside any custom span, getActiveSpan() returns the invocation root span—ideal for user.id, route names, or account context early in the request (Investigate issues — Add application context):

tracing.getActiveSpan() returns the currently active span. Use it to annotate the current span from helper functions and libraries without passing the span object through your code. Outside any custom span, it returns the invocation’s root span.

— Cloudflare, Workers tracing — new getActiveSpan(), recordException(), startSpan(), and setAttributes() APIs

Do not put secrets, tokens, or full sensitive bodies into span attributes.

Span methods: errors, status, names, and trace IDs

Section titled “Span methods: errors, status, names, and trace IDs”
Method Role Dashboard vs export
setAttribute / setAttributes Metadata on the span Visible in Observability and OTLP export
recordException Exception event on the span Export; does not end the span
setStatus({ code, message? }) OpenTelemetry status (unset, ok, error) Included in exported traces; dashboard does not show status yet
updateName(name) Rename span (e.g. after route match); also root via getActiveSpan() Names truncated to 64 bytes
spanContext() traceId, spanId, traceFlags IDs match dashboard and export; useful in error JSON
end() Submit span Required for startActiveSpan / startSpan

span.spanContext() returns the span’s traceId, spanId, and traceFlags. The IDs match the trace and span IDs in Workers Observability and in exported traces, so you can log them or return them to clients to correlate a request with its trace.

— Cloudflare, Workers tracing — new spanContext(), setStatus(), and updateName() APIs

console.log() inside an active custom span is attributed to that span in traces and exports (Custom spans — Logging within spans).

Choosing enterSpan vs startActiveSpan vs startSpan

Section titled “Choosing enterSpan vs startActiveSpan vs startSpan”
enterSpan startActiveSpan startSpan
Ends Automatically Manual span.end() Manual span.end()
Active during callback Yes Yes Never
Typical use Most handlers and async work Streams, pipelines Fire-and-forget timing, parallel siblings

Parent-child relationships follow JavaScript async context, not manual wiring (Custom spans — Nested spans).

View traces: dashboard, export, sampling, and local dev

Section titled “View traces: dashboard, export, sampling, and local dev”

Workers Observability and OpenTelemetry (OTLP) export

Section titled “Workers Observability and OpenTelemetry (OTLP) export”

With tracing enabled you can view traces in the Workers Observability dashboard, query span attributes, and export to OTLP destinations (Honeycomb, Sentry, Grafana, and others) via dashboard configuration:

You can now:

View traces alongside your logs in the Workers Observability dashboard Export traces (and correlated logs) to any OTLP-compatible destination ↗︎, such as Honeycomb, Sentry, or Grafana, by configuring a tracing destination in the Cloudflare dashboard ↗︎ Analyze and query across span attributes (operation type, status, duration, errors)

— Cloudflare, Workers automatic tracing, now in open beta

No. Tracing has no measurable overhead on request processing. Requests that are not sampled skip tracing entirely.

— Cloudflare, Traces — Does tracing add latency to my requests

Configuration covers head sampling, persisting traces to Cloudflare, incoming W3C trace context (default Reject—treat joined traces as untrusted), and forwarding context to origin.

Local tracing and Local Explorer (wrangler dev)

Section titled “Local tracing and Local Explorer (wrangler dev)”

During local development, wrangler dev and vite dev capture OpenTelemetry traces and correlated logs:

wrangler dev and vite dev automatically capture structured OpenTelemetry traces and correlated console logs during local Worker invocations.

— Cloudflare, AI agents can debug Workers with local tracing

Local Explorer shows the same traces in the browser—spans, timing, attributes, errors—including binding calls and custom spans (Local Explorer — Traces).

Cloudflare Agents: trace model calls, tools, and diagnostics channels

Section titled “Cloudflare Agents: trace model calls, tools, and diagnostics channels”

Agent observability sits on Workers traces plus a separate diagnostics channel event stream for runtime operations.

Agent tracing in the dashboard (session replay vs trace waterfall)

Section titled “Agent tracing in the dashboard (session replay vs trace waterfall)”

With observability.traces.enabled (and typically observability.enabled), traced agents appear in the dashboard. For each agent you can:

Replay a session assembles the full conversation for a turn: system instructions, user messages, model reasoning, tool calls with arguments and results, and the final response.

View a trace shows the execution timeline, connecting agent operations to Workers infrastructure so you can see where time was spent.

— Cloudflare, Cloudflare Agents: trace every model call, tool run, and decision

Turn structure is documented as:

invoke_agent {agent class}
├── chat {model}
└── execute_tool {tool}
└── tool_approval {tool}

See Agent tracing for identity fields (gen_ai.agent.name, gen_ai.agent.id, gen_ai.conversation.id), payload privacy, and framework setup.

Framework instrumentation: Think, Flue, wrapAISDK()

Section titled “Framework instrumentation: Think, Flue, wrapAISDK()”

Think and Flue agents are instrumented automatically. For direct AI SDK calls, wrap the namespace with wrapAISDK(). Custom harnesses can use the Workers custom spans API with OpenTelemetry Generative AI semantic conventions ↗︎.

— Cloudflare, Cloudflare Agents: trace every model call, tool run, and decision

Think — Tracing notes Think uses wrapAISDK() internally; enable Workers traces in Wrangler only. Direct AI SDK callers must supply agent identity via telemetry / experimental_telemetry as described in Agent tracing — AI SDK.

Agents SDK v0.7.0 diagnostics channels (events without console spam)

Section titled “Agents SDK v0.7.0 diagnostics channels (events without console spam)”

Agents publish structured events to named diagnostics channels—silent by default, zero overhead when nobody is listening:

The previous observability system used console.log() with a custom Observability.emit() interface. v0.7.0 replaces it with structured events published to diagnostics channels — silent by default, zero overhead when nobody is listening.

— Cloudflare, Agents SDK v0.7.0: Observability rewrite

Subscribe with the typed helper:

import { subscribe } from "agents/observability";
const unsub = subscribe("rpc", (event) => {
if (event.type === "rpc") {
console.log(`RPC call: ${event.payload.method}`);
}
});
unsub();

Channel names include agents:rpc, agents:message, agents:workflow, agents:mcp, and others; the full matrix is in Diagnostics channels. In production, events can flow to Tail Workers via event.diagnosticsChannelEvents (Agents SDK v0.7.0 changelog). A custom Observability override on the Agent class remains supported for filtering or forwarding.

Tracing and diagnostics channels solve different problems: traces for timing and platform nesting; diagnostics channels for fine-grained agent lifecycle and RPC/message events.

Span data limits and cloudflare.warning.* attributes

Section titled “Span data limits and cloudflare.warning.* attributes”

Each span holds roughly 64 KB of attribute and exception data; names are truncated to 64 bytes. When data is dropped:

The Worker still returns 202 Accepted, but tracing drops the oversized attribute. The span includes cloudflare.warning.type set to span_data_limit_exceeded and a cloudflare.warning.message identifying the rejected attribute and its size.

— Cloudflare, Workers Observability now surfaces warning attributes on spans for non-fatal errors

Workers may also set span_not_ended when a span was still open at invocation end (Spans and attributes — Warning types).

Workers tracing remains open beta; see Known limitations for roadmap items such as richer external trace linking.

  1. Set observability.traces.enabled = true in Wrangler, deploy, and send a test request that exercises fetch() or a binding. Confirm a trace appears in Workers Observability (or your configured OTLP destination per Traces — Configuration).

  2. Add a short tracing.enterSpan() around one code path, deploy again, and confirm the named span nests under the handler span in the waterfall (Custom spans).

  3. If you use Worker-to-Worker or Durable Object RPC, trigger one flow and confirm a single trace with nested subrequest or RPC spans (automatic subrequest tracing, RPC session spans).

  4. Run wrangler dev, reproduce the request, and open Local Explorer to verify spans, timing, and errors locally (Local Explorer, local tracing).

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page disagrees with your traces or Wrangler config, email yimasynthetic@gmail.com with the page URL, the statement you are questioning, and (if possible) a traceId from spanContext() or the dashboard.