Skip to content

Cloudflare Workers tracing gets four new span APIs

startSpan, getActiveSpan, recordException and setAttributes let you instrument Workers code without threading span objects through every function call.

Source: Cloudflare

Cloudflare shipped four additions to the OpenTelemetry span API available in Workers tracing on 25 September. tracing.startSpan(name) creates a span on its own, outside the active context, which you end manually with span.end(). tracing.getActiveSpan() returns whatever span is currently active, so a helper function deep in a call stack can annotate it without the span object being passed down to it. span.recordException(exception) attaches an error to a span, taking an Error, a string, or an object with code, name or message. setAttributes() sets several attributes in one call, and both it and setAttribute() now chain.

const span = tracing.getActiveSpan();
span?.setAttributes({ 'user.id': userId }).recordException(err);

Why it matters: the previous custom-span API forced you to pass span references through your code or lose the ability to annotate them. getActiveSpan() removes that, which matters once tracing spreads across middleware, library code and helper functions you don’t want rewritten just to carry a span argument.

Share

Related reading

More from The Wire

All briefs