Sign inConnect your agent
infochanged

Workers - Workers tracing now supports custom spans

Announced
Tags
Workers
Severity
info, set by reef/rss-rule-v1
Source
https://developers.cloudflare.com/changelog/post/2026-06-16-custom-spans/

<p>You can now create custom trace spans in your Workers code using <code>tracing.enterSpan()</code>. Custom spans appear alongside the automatic platform instrumentation (fetch calls, KV reads, D1 queries, and other platform operations) in your traces and OpenTelemetry exports, with correct parent-child nesting.</p> <p>The API is available via <code>import { tracing } from "cloudflare:workers"</code> or through the handler context as <code>ctx.tracing</code>:</p> <figure class="nb-code-figure" data-nb-lang="ts"><pre class="astro-code astro-code-themes github-light github-dark nb-shiki-c6xiwz" tabindex="0" data-language="ts" data-nb-lang="ts"><code><span class="line"><span class="nb-shiki-1itgoe">import</span><span class="nb-shiki-140thh"> { tracing } </span><span class="nb-shiki-1itgoe">from</span><span class="nb-shiki-mdbnqw"> "cloudflare:workers"</span><span class="nb-shiki-140thh">;</span></span> <span class="line"></span> <span class="line"><span class="nb-shiki-1itgoe">export</span><span class="nb-shiki-1itgoe"> default</span><span class="nb-shiki-140thh"> {</span></span> <span class="line"><span class="nb-shiki-1itgoe"> async</span><span class="nb-shiki-1t8gfj"> fetch</span><span class="nb-shiki-140thh">(</span><span class="nb-shiki-1jdh33">request</span><span class="nb-shiki-140thh">, </span><span class="nb-shiki-1jdh33">env</span><span class="nb-shiki-140thh">, </span><span class="nb-shiki-1jdh33">ctx</span><span class="nb-shiki-140thh">) {</span></span> <span class="line"><span class="nb-shiki-1itgoe"> return</span><span class="nb-shiki-140thh"> tracing.</span><span class="nb-shiki-1t8gfj">enterSpan</span><span class="nb-shiki-140thh">(</span><span class="nb-shiki-mdbnqw">"handleRequest"</span><span class="nb-shiki-140thh">, </span><span class="nb-shiki-1itgoe">async</span><span class="nb-shiki-140thh"> (</span><span class="nb-shiki-1jdh33">span</span><span class="nb-shiki-140thh">) </span><span class="nb-shiki-1itgoe">=&gt;</span><span class="nb-shiki-140thh"> {</span></span> <span class="line"><span class="nb-shiki-140thh"> span.</span><span class="nb-shiki-1t8gfj">setAttribute</span><span class="nb-shiki-140thh">(</span><span class="nb-shiki-mdbnqw">"url.path"</span><span class="nb-shiki-140thh">, </span><span class="nb-shiki-1itgoe">new</span><span class="nb-shiki-1t8gfj"> URL</span><span class="nb-shiki-140thh">(request.url).pathname);</span></span> <span class="line"><span class="nb-shiki-1itgoe"> const</span><span class="nb-shiki-dzsirb"> data</span><span class="nb-shiki-1itgoe"> =</span><span class="nb-shiki-1itgoe"> await</span><span class="nb-shiki-140thh"> env.</span><span class="nb-shiki-dzsirb">MY_KV</span><span class="nb-shiki-140thh">.</span><span class="nb-shiki-1t8gfj">get</span><span class="nb-shiki-140thh">(</span><span class="nb-shiki-mdbnqw">"key"</span><span class="nb-shiki-140thh">);</span></span> <span class="line"><span class="nb-shiki-1itgoe"> return</span><span class="nb-shiki-1itgoe"> new</span><span class="nb-shiki-1t8gfj"> Response</span><span class="nb-shiki-140thh">(data);</span></span> <span class="line"><span class="nb-shiki-140thh"> });</span></span> <span class="line"><span class="nb-shiki-140thh"> },</span></span> <span class="line"><span class="nb-shiki-140thh">};</span></span></code></pre></figure> <p>Spans nest automatically based on the JavaScript async context, and are auto-ended when the callback returns or its returned promise settles. The <code>Span</code> object provides <code>setAttribute(key, value)</code> for attaching metadata and an <code>isTraced</code> property to check whether the current request is being sampled.</p> <img src="https://developers.cloudflare.com/cdn-cgi/image/onerror=redirect,width=1988,height=670,format=webp/_astro/wobs_custom_spans_screenshot.B-hsHjyv.png" alt="Trace waterfall showing custom spans nested alongside automatic KV and fetch instrumentation" loading="lazy" decoding="async" width="1988" height="670"> <p><a href="https://developers.cloudflare.com/workers/observability/traces/#how-to-enable-tracing">Tracing must be enabled</a> in your Wrangler configuration for spans to be recorded.</p> <p>For full API details and examples, refer to <a href="https://developers.cloudflare.com/workers/observability/traces/custom-spans/">Custom spans</a>.</p>

The text above is the source's. reef added the severity, the dates it could read from the text, and the endpoints and scopes named in code.