Workers tracing — new getActiveSpan(), recordException(), startSpan(), and setAttributes() APIs
Workers now support additional OpenTelemetry span APIs including getActiveSpan(), recordException(), startSpan(), and setAttributes() for more granular instrumentation and error tracking on custom spans.
Custom spans in Workers now support more of the OpenTelemetry span API, so you can instrument more of your code and record errors directly on your spans.
tracing.startSpan(name)creates a span without making it the active span, and returns it. Other spans do not nest under it. Callspan.end()when the operation is complete.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.span.recordException(exception)records an exception event on a span. It accepts anError, a string, or an object with acode,name, ormessage.span.setAttributes(attributes)sets multiple attributes at once.setAttribute()andsetAttributes()now return the span, so you can chain calls.
import { tracing } from "cloudflare:workers";
export default {
async fetch(request, env) {
const user = await authenticate(request, env);
// Annotate the invocation's root span
tracing.getActiveSpan()?.setAttributes({
"user.id": user.id,
"user.plan": user.plan,
});
const span = tracing.startSpan("load-profile");
try {
return Response.json(await loadProfile(env, user.id));
} catch (err) {
span.recordException(err);
throw err;
} finally {
span.end();
}
},
};src/index.tstsimport { tracing } from "cloudflare:workers";
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const user = await authenticate(request, env);
// Annotate the invocation's root span
tracing.getActiveSpan()?.setAttributes({
"user.id": user.id,
"user.plan": user.plan,
});
const span = tracing.startSpan("load-profile");
try {
return Response.json(await loadProfile(env, user.id));
} catch (err) {
span.recordException(err as Error);
throw err;
} finally {
span.end();
}
},
};
For more details, refer to the custom spans documentation.
Source: original entry ↗
More from Cloudflare
Follow Cloudflare to get its new changes in your feed and email digest.
Persistent browser tool in Agents SDK with tabs and logins
The Cloudflare Agents SDK now includes a browserTool that allows agents to maintain a persistent browser session across multiple turns, preserving tabs, cookies, and login sessions. The browser remains alive even when the Durable Object hibernates, with a 10-minute idle timeout before creating a fresh session.
Cloudflare API MCP server sends Workers Issues as MCP Events
The Cloudflare API MCP server now exposes Workers Issues as MCP Events through webhook subscriptions, allowing agents to receive real-time notifications when production failures are detected without polling. Clients can subscribe to cloudflare.alert.workers_observability_real_time_issue scoped to an account or individual Worker.
Web Analytics: Deeper Core Web Vitals Diagnostics with LCP & INP Sub-Part Metrics
Real User Monitoring in Web Analytics now breaks down Largest Contentful Paint (LCP) and Interaction to Next Paint (INP) into their constituent phases, plus splits connection time into TCP and TLS components. These sub-part metrics are available in the dashboard and queryable through the GraphQL Analytics API to help identify which specific area is impacting page performance.