Skip to Content
OpenPerf

Guides

Distributed tracing

Continue a browser trace on the server so one trace shows the whole request.

When the browser and the server both run the SDK, a request from the page and the work it causes on the server show up as one trace. The browser passes the trace on in a traceparent request header, and the server continues it.

Requests to the page's own origin get the header without any option. For an API on another origin, list it in tracePropagationTargets:

main.ts
import { init } from "@openperf/sdk/browser";

init({
  dsn: import.meta.env.VITE_OPENPERF_DSN,
  tracePropagationTargets: [location.origin, "https://api.example.com/"],
});

Open a page load in Traces. The server spans are nested under the browser request that caused them.

Choose the targets

tracePropagationTargets applies to fetch and XMLHttpRequest calls. Each entry is checked against the full URL of the request:

  • A string matches a URL that contains it.
  • A RegExp matches a URL it matches.

The list replaces the default. Keep location.origin in it when the page also calls its own origin.

Allow the header on the server

A request to another origin with a traceparent header is preflighted by the browser. The API has to allow the header in its CORS response:

Access-Control-Allow-Headers: traceparent

Check CORS before you add a target

Without this response header the browser blocks the request itself, not only the tracing.

What the server does

The Node.js and Bun SDKs read traceparent on incoming requests and continue the trace. There is nothing to configure. Outgoing fetch and HTTP calls from the server carry the header on, so a chain of services ends up in the same trace.

The header follows the W3C Trace Context format:

traceparent: 00-<trace id>-<span id>-<flags>

A backend in another language joins the trace when it reads this header, for example with OpenTelemetry.

Sampling across the boundary

The browser decides whether a trace is sampled when the trace starts. The decision travels in the last field of the header: 01 for sampled, 00 for not sampled. The server follows it and sends its spans only for sampled traces. The server's own tracesSampleRate applies to traces that start on the server. Uncaught errors and errors reported with captureException are sent either way.

Common mistakes

  • Losing same-origin requests. After you set tracePropagationTargets, requests to your own origin get the header only when an entry matches them.
  • An empty list. tracePropagationTargets: [] turns the header off for every request.
  • A short string. "api" matches every URL that contains those letters, third-party hosts included. Use the full origin with a trailing slash, or an anchored pattern such as /^https:\/\/api\.example\.com\//.
  • Starting the SDK late. Requests made before init are not traced and get no header.

On this page