Set Up Distributed Tracing for Cloudflare

Learn how to connect events across applications/services using the Sentry JavaScript SDK.

Distributed tracing connects and records the path of requests as they travel through the different tiers of your application architecture. If your architecture consists of multiple services that live on different sub-domains (e.g. fe.example.com and api.example.com), distributed tracing will help you follow the path of events as they move from one service to another.

This end-to-end visibility allows developers to identify bottlenecks, pinpoint the root cause of errors, and understand component interactions—turning what would be a complex debugging nightmare into a manageable process that improves system reliability and performance.

Here's an example showing a distributed trace in Sentry:

This distributed trace shows a Vue app's pageload making a request to a Python backend, which then calls the /api endpoint of a Ruby microservice.

What happens in the background is that Sentry uses reads and further propagates two HTTP headers between your applications:

  • sentry-trace
  • baggage

The Sentry Cloudflare SDK automatically propagates traces for incoming and outgoing HTTP requests if you've setup the SDK to send traces.

For RPC calls within Cloudflare (Worker-to-Durable Object, Worker-to-Worker via service bindings), you name the bindings that should carry the trace. See RPC Trace Propagation below.

Available since: v10.52.0

By default, traces are not propagated across RPC calls between Workers and Durable Objects. This is because Cap'n Proto (which powers Cloudflare RPC) has no native support for headers or metadata. The SDK carries the trace context in a trailing argument instead, and the receiving SDK strips it before your method runs.

That trailing argument is why propagation is opt-in per binding: only a Sentry-instrumented receiver strips it again. List the bindings whose receiver you know runs Sentry in rpcTracePropagationBindings (SDK version 10.72.0 or higher). Setting the option also turns on the receiver side, so a Worker that both calls and receives needs nothing else.

If you build with the Sentry Cloudflare Vite plugin, it derives the bindings that point at classes in this Worker, such as your own Durable Objects, and wraps those classes as receivers. Traces then connect across these RPC calls with no configuration. See Derived RPC Trace Propagation.

Bindings to other Workers stay opt-in. List them in your instrument.server.ts file. The plugin adds the derived bindings to your list:

src/instrument.server.ts
Copied
import { defineCloudflareOptions } from "@sentry/cloudflare";

export default defineCloudflareOptions((env) => ({
  dsn: env.SENTRY_DSN,
  tracesSampleRate: 1.0,
  // Propagate to `env.AUTH_SERVICE` and every `env.SVC_*` binding
  rpcTracePropagationBindings: ["AUTH_SERVICE", /^SVC_/],
}));

The Worker behind each listed binding must also run Sentry, so that it strips the trace argument again.

If you deploy with wrangler directly, the plugin can't derive bindings. List every binding yourself, including bindings to your own Durable Objects, and set the option on the wrapped classes too, as shown in the Wrangler setup.

Strings match a binding name exactly, so "DB" does not also match MY_DB. Use a regular expression for pattern matching. For the full definition, see rpcTracePropagationBindings.

The receiver continues the trace it is sent, creates a span per RPC method, and strips the trailing argument, so your method signatures are unaffected. Listing bindings on the receiver does no harm, so you can share one options object across the whole deployment.

If you don't want to use the default tracing setup, you can set up Custom Instrumentation for distributed tracing.

If you want to disable distributed tracing, set the tracePropagationTargets option to be an empty array. This will ensure no trace headers are sent.

src/instrument.server.ts
Copied
import { defineCloudflareOptions } from "@sentry/cloudflare";

export default defineCloudflareOptions((env) => ({
  dsn: "___PUBLIC_DSN___",
  // Capture 100% of spans. This is useful for development and debugging. Consider reducing in production or using traceSampler
  tracesSampleRate: 1.0,
  // Overwrite the defaults to ensure no trace headers are sent
  tracePropagationTargets: [],
}));

src/instrument.server.ts
Copied
import { defineCloudflareOptions } from "@sentry/cloudflare";

export default defineCloudflareOptions((env) => ({
  dsn: "___PUBLIC_DSN___",
  // Capture 100% of spans. This is useful for development and debugging. Consider reducing in production or using traceSampler
  tracesSampleRate: 1.0,
  tracePropagationTargets: [
    "https://api.myecommerce.com",
    "https://auth.myecommerce.com",
  ],
}));

This tells Sentry to pass trace headers across the following paths:

  • Your main API server (where product data comes from)
  • Your authentication server (where logins happen)

This way, if a customer experiences an error during checkout, or you want to check the performance of a specific endpoint, you can see the complete path their request took across these different services.

src/instrument.server.ts
Copied
import { defineCloudflareOptions } from "@sentry/cloudflare";

export default defineCloudflareOptions((env) => ({
  dsn: "___PUBLIC_DSN___",
  tracePropagationTargets: [
    "https://api.myapp.com",
    "https://media.myapp.com",
    /^\/local-api\//,
  ],
}));

This configuration lets your app track user actions across:

  • Your main API server (handles most app functions)
  • Your media server (handles images, videos, etc.)
  • Any local API endpoints in your app

If your app crashes while a user is uploading a photo, you can trace exactly where the problem occurred - in the app itself, the main API, or the media service.

Server-side SDKs handle traces automatically on a per-request basis. This means that SDKs will:

  • Continue an existing trace if the incoming request contains a trace header.
  • Start a new trace if the incoming request does not contain a trace header. This trace stays active until the response is sent.

If necessary, you can override the default trace duration by manually starting a new trace.

Sentry uses a "head-based" sampling approach:

  • A sampling decision is made in the originating service (the "head")
  • This decision is propagated to all downstream services

The two key headers are:

  • sentry-trace: Contains trace ID, span ID, and sampling decision
  • baggage: Contains additional trace metadata including sample rate

Sentry automatically attaches these headers to outgoing HTTP requests when using the browserTracingIntegration. For other communication channels like WebSockets, you can manually propagate trace information:

Copied
// Extract trace data from the current scope
const traceData = Sentry.getTraceData();
const sentryTraceHeader = traceData["sentry-trace"];
const sentryBaggageHeader = traceData["baggage"];

// Add to your custom request (example using WebSocket)
webSocket.send(
  JSON.stringify({
    message: "Your data here",
    metadata: {
      sentryTrace: sentryTraceHeader,
      baggage: sentryBaggageHeader,
    },
  }),
);
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
How to contribute   |  Edit this page   |  Create a docs issue   |  Get support