OpenAICloudflare Developer PlatformJun 16, 2026, 12:00 AM

Workers - Workers tracing now supports custom spans

A condensed section focused on the key takeaways first.

Original Post

Quick Digest

Summary

A condensed section focused on the key takeaways first.

openaienmodel: gpt-5-mini-2025-08-07

Workers - Workers tracing now supports custom spans

Key Points

  • Create custom spans with tracing.enterSpan()
  • Spans auto-nest via async context
  • Exports included in OpenTelemetry traces

Summary

Cloudflare Workers can now create custom trace spans using tracing.enterSpan() (import from cloudflare:workers or use ctx.tracing). Custom spans appear alongside automatic platform instrumentation (fetch calls, KV reads, D1 queries, etc.) with correct parent-child nesting and are exported to Cloudflare traces and OpenTelemetry.

Key Points

  • Create spans with tracing.enterSpan(name, async span => { ... }); spans auto-end when the callback returns or its promise settles.
  • Spans are nested automatically according to the JavaScript async context and appear alongside platform-instrumented operations.
  • Use span.setAttribute(key, value) to attach metadata and check span.isTraced to avoid unnecessary work when a request is not sampled.
  • Tracing must be enabled in your Wrangler configuration for spans to be recorded.

Practical notes

  • Import: import { tracing } from "cloudflare:workers" or access via ctx.tracing in handler context.
  • Instrumentation is useful for debugging and performance analysis; prefer short-lived spans and guard work with span.isTraced to reduce overhead.
  • See the Custom spans docs for full API details and examples.

Full Translation

Translations

A translation section that keeps the flow of the original article.

openaijamodel: gpt-5-mini-2025-08-07

Workers — トレーシングでカスタムスパンがサポートされるようになりました

Workers トレーシングでカスタムスパンがサポートされるようになりました

公開日: 2026-06-16

Workers のコード内で tracing.enterSpan() を使ってカスタムのトレーススパンを作成できるようになりました。カスタムスパンは、fetch 呼び出し、KV 読み取り、D1 クエリなどの自動プラットフォーム計測と並んで、トレースおよび OpenTelemetry エクスポートに表示され、親子のネスト関係が正しく維持されます。

利用方法

API は次のいずれかで利用できます。

  • import { tracing } from "cloudflare:workers"
  • ハンドラのコンテキスト経由: ctx.tracing

TypeScript の使用例:

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);
    });
  },
};

主なポイント

  • スパンは JavaScript の async コンテキストに基づいて自動的にネストされます。
  • コールバックが返るか、その返された Promise が解決/拒否されると、スパンは自動的に終了します。
  • Span オブジェクトはメタデータ添付用の setAttribute(key, value) を提供します。
  • Span には現在のリクエストがサンプリングされているかを判定する isTraced プロパティがあります。
  • スパンを記録するには、Wrangler の設定でトレーシングを有効にする必要があります。

完全な API 詳細と追加の例については、Custom spans を参照してください。