# Now You Can Drop Your Own Spans Into Workers Traces!

Hey there, it's Shiichan! Today I've got a handy little upgrade for Workers observability. Now you can put the exact stretch of your own code you care about right onto your traces.

## What was announced?

Cloudflare's Changelog announced that [Workers](https://developers.cloudflare.com/workers/) tracing now supports **custom spans**. Just call `tracing.enterSpan()` and you can record a section of your own code as its own span.

The best part: these custom spans show up right alongside the spans the platform instruments automatically, like fetch calls, KV reads, and D1 queries. And when you export via OpenTelemetry, they come out with the correct parent-child nesting preserved.

## The story so far

Until now, the spans in your Workers traces were mostly the ones the platform added for you. You could see operations like fetch, KV, and D1, but you couldn't carve out application-side sections yourself, like "how many milliseconds does this piece of my function take?"

## What changes

From now on you can wrap the slow-looking work or the logic you're curious about in your own span. Since they line up chronologically with the automatic spans, you can tell at a glance whether the time is going into platform work or into your own code. Hunting down bottlenecks gets a lot easier.

## Dive Deep

You load the API with `import { tracing } from "cloudflare:workers"`, or use it through the handler context as `ctx.tracing`.

```ts
import { tracing } from "cloudflare:workers";

export default {
  async fetch(request, env, ctx) {
    return tracing.enterSpan("handleRequest", async (span) => {
      span.setAttribute("url.path", new URL(request.url).pathname);
      const data = await env.MY_KV.get("key");
      return new Response(data);
    });
  },
};
```

Spans nest automatically based on the JavaScript async context, and they're auto-ended when the callback returns or its returned promise settles, so you don't have to write any manual cleanup.

The `Span` object gives you `setAttribute(key, value)` for attaching metadata, and an `isTraced` property to check whether the current request is being sampled.

One thing to note: for spans to be recorded, you need to [enable tracing](https://developers.cloudflare.com/workers/observability/traces/#how-to-enable-tracing) in your Wrangler configuration first. For the full details, check the [custom spans docs](https://developers.cloudflare.com/workers/observability/traces/custom-spans/).

## Wrap-up

- Cloudflare's Changelog announced that Workers tracing now supports custom spans
- With `tracing.enterSpan()` you measure sections of your own code, and they line up next to the automatic spans
- Spans auto-nest via the async context and auto-close, plus you get `setAttribute` and `isTraced`
- To use it, you need to enable tracing in your Wrangler configuration

This one is perfect for anyone who wants to seriously tune Workers performance, or who's already collecting instrumentation with OpenTelemetry!
