Skip to content

docs(cloudflare): document Worker observability.traces support#1439

Open
sam-goodwin wants to merge 2 commits into
mainfrom
claude/dazzling-golick-e39bc4
Open

docs(cloudflare): document Worker observability.traces support#1439
sam-goodwin wants to merge 2 commits into
mainfrom
claude/dazzling-golick-e39bc4

Conversation

@sam-goodwin

Copy link
Copy Markdown
Collaborator

Closes #1438

Background

Issue #1438 requested first-class support for observability.traces on Cloudflare.Worker. On investigation, the functional support already exists end-to-end:

  • WorkerObservability.traces type (enabled, headSamplingRate, persist, destinations) is defined in worker.ts.
  • It is serialized to the Cloudflare API via camelToSnakeObjectDeep in worker-metadata.ts.
  • It round-trips back onto worker.observability, with a passing live test in worker.test.ts asserting both the API metadata and the output shape.

The real gap the issue describes — "explicit documentation/examples appears to be missing/partial" — was documentation, plus a small type incompleteness.

Changes

  • worker-metadata.ts: added persist and destinations to the observability.logs metadata type. The runtime already forwarded these (and the existing test asserts them), but the type omitted them.
  • worker.mdx: added a dedicated Observability section documenting the full enabled / logs / traces configuration, plus a Traces subsection with a Custom Spans (trace.enterSpan()) example and production-vs-dev sampling guidance. Also surfaced traces in the quick-config snippet.

Usage

export const worker = await Worker("api", {
  entrypoint: "./src/worker.ts",
  observability: {
    enabled: true,
    headSamplingRate: 1,
    logs: { enabled: true, invocationLogs: true, persist: true },
    traces: { enabled: true, headSamplingRate: 0.1, persist: true },
  },
});

And custom spans from within the Worker:

import { trace } from "cloudflare:workers";

export default {
  async fetch(request, env, ctx) {
    return trace.enterSpan("handle-request", async (span) => {
      span.setAttribute("route", new URL(request.url).pathname);
      const user = await env.DB.prepare("SELECT * FROM users LIMIT 1").first();
      return Response.json(user);
    });
  },
};

🤖 Generated with Claude Code

github-actions Bot and others added 2 commits May 19, 2026 20:49
Adds a dedicated Observability section to the Worker docs covering the
logs/traces sub-configuration, including a Custom Spans example, and
completes the WorkerMetadata logs type with the persist/destinations
fields the runtime already sends.

Closes #1438

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for Cloudflare observability.traces configuration in Cloudflare.Worker

1 participant