Cloudflare Workers automatically instruments platform operations like fetch calls, KV reads, and D1 queries. Custom spans let you extend this visibility into your own application logic, so you can trace custom code paths alongside the built-in instrumentation.
The custom spans API is available in two ways — both provide the same methods and behave identically:
import { tracing } from "cloudflare:workers"— works anywhere in your codebase, including utility functions, libraries, and modules that do not have access to the handler context.ctx.tracing— available on theExecutionContextpassed to your handler, convenient when you are already working within a handler.
There are three span creation methods:
enterSpan()— creates a span that automatically ends when the callback returns or its returned promise settles. Use this for most instrumentation.startActiveSpan()— creates a span that is active during a callback, and that you end manually by callingspan.end(). Use this when the span must outlive the callback, such as when instrumenting streams or other long-lived operations.startSpan()— creates a span without making it active, and that you end manually by callingspan.end(). Use this to time an operation when you do not need other spans to nest under it.
You can also call getActiveSpan() to get the currently active span, so you can add attributes or record exceptions without passing the span object through your code.
Custom spans require tracing to be enabled on your Worker. If you have not already done so, set observability.traces.enabled to true in your Wrangler configuration file:
{
"$schema": "./node_modules/wrangler/config-schema.json",
"observability": {
"traces": {
"enabled": true
}
}
}[observability.traces]
enabled = trueUse tracing.enterSpan() to wrap a section of code in a named span. The span automatically becomes a child of whichever span is currently active, and ends when the callback returns or its returned promise settles.
The following example uses both access methods — the cloudflare:workers import and ctx.tracing — to show that they are interchangeable:
import { tracing } from "cloudflare:workers";
export default {
async fetch(request, env, ctx) {
// Using the import
return tracing.enterSpan("handleRequest", async (span) => {
span.setAttribute("url.path", new URL(request.url).pathname);
const user = await ctx.tracing.enterSpan("auth", async () => {
// Using ctx.tracing
return authenticate(request, env);
});
return buildResponse(user);
});
},
};import { tracing } from "cloudflare:workers";
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
// Using the import
return tracing.enterSpan("handleRequest", async (span) => {
span.setAttribute("url.path", new URL(request.url).pathname);
const user = await ctx.tracing.enterSpan("auth", async () => {
// Using ctx.tracing
return authenticate(request, env);
});
return buildResponse(user);
});
},
};Creates a new span and runs callback inside it. The span is automatically ended when the callback returns (synchronous or asynchronous) or throws.
Parameters:
| Parameter | Type | Description |
|---|---|---|
name |
string |
The name of the span. This appears in trace visualizations. |
callback |
(span: Span, ...args: A) => T |
The function to execute within the span. Receives the Span object as its first argument, followed by any additional arguments passed to enterSpan. |
...args |
A |
Optional additional arguments forwarded to the callback after the span parameter. |
Returns: The return value of callback.
Behavior:
- The new span is a child of whichever span is currently active on the async context. If no span is active, it becomes a child of the request's root span.
- Nested
enterSpancalls and runtime-created spans (such asfetchor KV operations) that run inside the callback automatically become children of this span. - The span ends when the callback returns synchronously, throws synchronously, or when its returned promise fulfills or rejects.
// Synchronous callback — span ends when the function returns
const result = tracing.enterSpan("parse", (span) => {
span.setAttribute("format", "json");
return JSON.parse(body);
});
// Async callback — span ends when the promise settles
const data = await tracing.enterSpan("fetchData", async (span) => {
const res = await fetch("https://api.example.com/data");
span.setAttribute("http.response.status_code", res.status);
return res.json();
});
// Forwarding arguments
const doubled = tracing.enterSpan("compute", (span, x) => x * 2, 21);Creates a new span, makes it the active span while callback runs, and returns the callback result without automatically ending the span. You must call span.end() explicitly when the operation is complete.
Parameters:
| Parameter | Type | Description |
|---|---|---|
name |
string |
The name of the span. This appears in trace visualizations. |
callback |
(span: Span, ...args: A) => T |
The function to execute while the span is active. Receives the Span object as its first argument, followed by any additional arguments. |
...args |
A |
Optional additional arguments forwarded to the callback after the span parameter. |
Returns: The return value of callback.
Behavior:
- Unlike
enterSpan, the span is not automatically ended when the callback returns or throws. You are responsible for callingspan.end(). - If you forget to call
span.end(), the span is still submitted when the request-owned span object is destroyed, as a backstop. Do not rely on this behavior — always callspan.end()explicitly.
Use startActiveSpan when you need a span to cover an operation that extends beyond a single callback — for example, instrumenting a stream pipeline where the span should remain open until the stream is fully consumed:
import { tracing } from "cloudflare:workers";
export default {
async fetch(request, env, ctx) {
const body = request.body;
if (!body) return new Response("No body", { status: 400 });
// The span is active during the callback, so the pipeThrough
// operation is correctly nested. The span stays open after
// the callback returns, until flush() calls span.end().
const stream = tracing.startActiveSpan("process-stream", (span) => {
span.setAttribute(
"request.content_type",
request.headers.get("content-type") ?? "unknown",
);
return body.pipeThrough(
new TransformStream({
transform(chunk, controller) {
// Process each chunk
controller.enqueue(chunk);
},
flush() {
span.setAttribute("stream.status", "complete");
span.end();
},
cancel() {
span.setAttribute("stream.status", "cancelled");
span.end();
},
}),
);
});
return new Response(stream);
},
};import { tracing } from "cloudflare:workers";
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const body = request.body;
if (!body) return new Response("No body", { status: 400 });
// The span is active during the callback, so the pipeThrough
// operation is correctly nested. The span stays open after
// the callback returns, until flush() calls span.end().
const stream = tracing.startActiveSpan("process-stream", (span) => {
span.setAttribute(
"request.content_type",
request.headers.get("content-type") ?? "unknown",
);
return body.pipeThrough(
new TransformStream({
transform(chunk, controller) {
// Process each chunk
controller.enqueue(chunk);
},
flush() {
span.setAttribute("stream.status", "complete");
span.end();
},
cancel() {
span.setAttribute("stream.status", "cancelled");
span.end();
},
}),
);
});
return new Response(stream);
},
};If you do not need the span to be active during a callback, use startSpan instead.
Creates a new span and returns it without making it the active span. You must call span.end() explicitly when the operation is complete.
Parameters:
| Parameter | Type | Description |
|---|---|---|
name |
string |
The name of the span. This appears in trace visualizations. |
Returns: A Span.
Behavior:
- The new span is a child of whichever span is currently active on the async context. If no span is active, it becomes a child of the request's root span.
- The span never becomes the active span. Spans created by
enterSpan,startActiveSpan, and platform operations (such asfetchor KV operations) that run while it is open are not children of this span. They are siblings. - If you forget to call
span.end(), the span is still submitted when the request-owned span object is destroyed, as a backstop. Do not rely on this behavior — always callspan.end()explicitly.
Use startSpan when you want to measure an operation whose start and end happen in different places, such as in event callbacks or across the methods of a class, and you do not need other spans nested under it:
import { tracing } from "cloudflare:workers";
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
// ...
const span = tracing.startSpan("cache-warmup");
span.setAttribute("cache.keys", keys.length);
try {
await warmCache(keys);
span.setAttribute("cache.warmup.status", "complete");
} catch (err) {
span.recordException(err as Error);
throw err;
} finally {
span.end();
}
// ...
},
};Returns the span that is currently active on the async context.
Returns: A Span, or undefined.
Behavior:
- Inside a request, returns the
Spanthat is currently considered active. If no spans have been explicitly created withenterSpanorstartActiveSpanor the system itself, it will return the root span of the current invocation. You can use it to add attributes or record exceptions on the root span. - Returns
undefinedoutside a request, such as in the top-level scope of your module, or in code that runs in an async context captured outside a request. - Spans created with
startSpannever become active, sogetActiveSpannever returns them.
Use getActiveSpan to annotate the current span without passing the span object through your code:
import { tracing } from "cloudflare:workers";
export default {
async fetch(request, env, ctx) {
const user = await authenticate(request, env);
// No custom span is active, so this annotates the root span
tracing.getActiveSpan()?.setAttributes({
"user.id": user.id,
"user.plan": user.plan,
});
return tracing.enterSpan("render", async () => {
// ...
return new Response("OK");
});
},
};import { tracing } from "cloudflare:workers";
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const user = await authenticate(request, env);
// No custom span is active, so this annotates the root span
tracing.getActiveSpan()?.setAttributes({
"user.id": user.id,
"user.plan": user.plan,
});
return tracing.enterSpan("render", async () => {
// ...
return new Response("OK");
});
},
};enterSpan and startActiveSpan pass a Span object to their callbacks, and startSpan and getActiveSpan return one. It provides methods to annotate the span with metadata and control its lifecycle.
Sets an attribute on the span.
| Parameter | Type | Description |
|---|---|---|
key |
string |
The attribute name. |
value |
string | number | boolean | undefined |
The attribute value. Passing undefined is a no-op. |
Returns: The same Span, so you can chain calls.
Attributes appear alongside the span in your traces and OpenTelemetry exports.
span
.setAttribute("user.plan", "enterprise")
.setAttribute("item.count", 42)
.setAttribute("cache.hit", true);Sets multiple attributes on the span at once. This works the same as calling setAttribute for each entry.
| Parameter | Type | Description |
|---|---|---|
attributes |
Record<string, string | number | boolean | undefined> |
An object that maps attribute names to values. Entries with undefined values are ignored. |
Returns: The same Span, so you can chain calls.
span.setAttributes({
"user.plan": "enterprise",
"item.count": 42,
"cache.hit": true,
"optional.field": undefined, // ignored
});Records an exception event on the span. The event includes a timestamp and appears in your traces and OpenTelemetry exports.
| Parameter | Type | Description |
|---|---|---|
exception |
Exception |
The exception to record. |
You can pass any of the following:
- An
Errorobject. The runtime records itsname,message, andstack. - A string, which the runtime records as the exception message.
- An object with at least one of
code,name, ormessage, and an optionalstack.codecan be a string or a number.
The runtime ignores the call if the span is not being traced or has already ended, or if you pass an object that has none of code, name, or message.
Recording an exception does not end the span or change how it ends. It also does not catch or rethrow the error. You still handle the error in your own code.
await tracing.startActiveSpan("chargeCard", async (span) => {
try {
return await chargeCard(token, amount);
} catch (err) {
span.recordException(err as Error);
throw err;
} finally {
span.end();
}
});// Strings and plain objects are also accepted
span.recordException("Upstream returned an empty body");
span.recordException({ code: "RATE_LIMITED", message: "Too many requests" });A readonly boolean indicating whether this invocation is being traced. When the request is not sampled (based on your head_sampling_rate), isTraced is false and enterSpan still runs the callback but does not record any telemetry.
You can use this to skip expensive attribute computation when the request is not being traced:
tracing.enterSpan("process", (span) => {
if (span.isTraced) {
span.setAttribute(
"request.body.preview",
JSON.stringify(body).slice(0, 200),
);
}
return processBody(body);
});Ends the span and submits its attributes to the tracing system. This method is idempotent. Calling it multiple times has no effect after the first call. After end() is called, span.isTraced returns false and any further method calls to annotate the span are silently ignored, including calls from in-flight async work that has not yet completed.
- For spans created with
enterSpan, you do not need to callend(). The runtime calls it automatically. Callingend()yourself is safe, but could end the span early. - For spans created with
startActiveSpanorstartSpan, you must callend()to submit the span. - For the root span returned by
getActiveSpan()outside any custom span,end()has no effect. The runtime ends the root span when the invocation completes.
const span = tracing.startSpan("manual-op");
span.setAttribute("step", "processing");
await doWork();
// Later, when the work is truly complete:
span.end(); // Span is submitted
span.end(); // No-op, safe to call againSpans nest automatically based on the JavaScript async context. Any enterSpan call or platform operation (such as fetch and env.MY_KV.get()) that runs inside a callback becomes a child of the enclosing span.
import { tracing } from "cloudflare:workers";
async function handleOrder(env, orderId) {
return tracing.enterSpan("handleOrder", async (span) => {
span.setAttribute("order.id", orderId);
// This KV read is automatically a child of "handleOrder"
const order = await env.ORDERS_KV.get(orderId, "json");
// This nested span is also a child of "handleOrder"
const total = tracing.enterSpan("calculateTotal", (innerSpan) => {
innerSpan.setAttribute("item.count", order.items.length);
return order.items.reduce((sum, item) => sum + item.price, 0);
});
// This fetch is a child of "handleOrder"
await fetch("https://api.example.com/notify", {
method: "POST",
body: JSON.stringify({ orderId, total }),
});
return new Response(JSON.stringify({ orderId, total }));
});
}import { tracing } from "cloudflare:workers";
async function handleOrder(env: Env, orderId: string) {
return tracing.enterSpan("handleOrder", async (span) => {
span.setAttribute("order.id", orderId);
// This KV read is automatically a child of "handleOrder"
const order = await env.ORDERS_KV.get(orderId, "json");
// This nested span is also a child of "handleOrder"
const total = tracing.enterSpan("calculateTotal", (innerSpan) => {
innerSpan.setAttribute("item.count", order.items.length);
return order.items.reduce(
(sum: number, item: any) => sum + item.price,
0,
);
});
// This fetch is a child of "handleOrder"
await fetch("https://api.example.com/notify", {
method: "POST",
body: JSON.stringify({ orderId, total }),
});
return new Response(JSON.stringify({ orderId, total }));
});
}
console.log() and other console methods emit log events that are automatically attributed to the currently active span. This means log output from inside an enterSpan or startActiveSpan callback is associated with that span in your traces and OpenTelemetry exports.
tracing.enterSpan("processPayment", async (span) => {
console.log("Starting payment processing"); // attributed to "processPayment"
const result = await chargeCard(token, amount);
console.log("Payment complete", result.id); // also attributed to "processPayment"
});The full type declarations for the custom spans API:
declare module "cloudflare:workers" {
namespace tracing {
function enterSpan<T, A extends unknown[]>(
name: string,
callback: (span: Span, ...args: A) => T,
...args: A
): T;
function startActiveSpan<T, A extends unknown[]>(
name: string,
callback: (span: Span, ...args: A) => T,
...args: A
): T;
function startSpan(name: string): Span;
function getActiveSpan(): Span | undefined;
}
type Exception =
| string
| { code: string | number; name?: string; message?: string; stack?: string }
| { code?: string | number; name: string; message?: string; stack?: string }
| { code?: string | number; name?: string; message: string; stack?: string };
class Span {
readonly isTraced: boolean;
setAttribute(key: string, value: string | number | boolean): this;
setAttributes(
attributes: Record<string, string | number | boolean | undefined>,
): this;
recordException(exception: Exception): void;
end(): void;
}
}The same API is available on the handler context as ctx.tracing, with the same types.
enterSpan |
startActiveSpan |
startSpan |
|
|---|---|---|---|
| Span ends | Automatically, when the callback returns, throws, or its returned promise settles | Manually, when you call span.end() |
Manually, when you call span.end() |
| Active context scope | During the callback | During the callback | Never active |
| Use case | Most instrumentation — sync and async work that fits within a single callback | Operations that outlive the callback, such as stream pipelines | Timing an operation with no nested spans, when you manage the start and end yourself |
| Error handling | Span auto-ends on throw | Span stays open on throw. Call span.end() or rely on the runtime backstop |
Span stays open on throw. Call span.end() or rely on the runtime backstop |
enterSpan and startActiveSpan set the span as the active context parent only during the callback. After the callback returns, the span is no longer the active parent. With enterSpan, this distinction does not matter because the span is also ended. With startActiveSpan, the span remains open but is no longer the context parent — new spans created after the callback returns are not children of this span. startSpan never sets the span as the active context parent, so no spans are ever created as its children.
The runtime limits how much data you can add to a custom span:
- Span names are truncated to 64 bytes.
- Each span can hold approximately 64 KB of attribute and exception data. Attribute keys and values, and the
code,name,message, andstackof recorded exceptions, all count toward this limit.
After a span reaches its data limit, the runtime ignores further setAttribute, setAttributes, and recordException calls on that span. It adds two attributes to the span so you can tell that data was dropped:
| Attribute | Value |
|---|---|
cloudflare.warning.type |
span_data_limit_exceeded |
cloudflare.warning.message |
A description of the attribute or exception that was dropped, and its size. |
- No manual parent-child wiring. Parent-child relationships are determined by the JavaScript async context automatically.
- No
spanContext()(trace/span IDs) yet. Access to trace and span identifiers for manual propagation across boundaries is planned for a future release. - No
setStatusyet. Setting span status is planned for a future release.
For other tracing limitations, refer to the known limitations page.