→ Back to Home
OpenTelemetry

Cloudflare Workers Introduces Custom Spans for Enhanced OpenTelemetry Tracing

Cloudflare has rolled out a significant update to its Workers platform, introducing support for custom trace spans, a move that greatly enhances observability for serverless applications. This new capability allows developers to instrument their Workers code with user-defined spans, providing deeper insights into the execution flow of their functions. The core of this feature is the `tracing.enterSpan()` API, which enables the creation of custom trace segments within the Workers environment. These custom spans are designed to integrate smoothly with the platform's existing automatic instrumentation. This means that alongside traces generated for operations like `fetch` calls, KV reads, and D1 queries, developers can now see their own custom spans, all correctly nested to reflect the logical flow of execution. This unified view simplifies the process of understanding how different parts of a Worker interact and perform. The API provides flexibility for developers to attach relevant metadata to their custom spans using `span.setAttribute(key, value)`. This allows for the inclusion of contextual information, such as request paths or specific data identifiers, which can be crucial for debugging and analysis. Furthermore, a `span.isTraced` property is available, enabling developers to check whether the current request is being sampled, which is useful for optimizing tracing overhead. Spans automatically nest based on JavaScript's async context, and they are automatically ended when the associated callback returns or its returned promise settles, reducing the boilerplate code required for manual span management. To utilize this feature, tracing must be enabled in the Wrangler configuration for spans to be recorded. This update is particularly beneficial for complex Workers applications where understanding the internal workings and performance bottlenecks is critical, offering a more comprehensive observability story through OpenTelemetry exports.
#custom spans#tracing#cloudflare workers#opentelemetry#observability
Read original source