Custom Instrumentation

On this page you will learn how to manually propagate trace information into and out of your JavaScript application.

Distributed tracing will be set up automatically if:

  • You've Set Up Performance in Sentry.
  • You're using one of the SDKs that include distributed tracing out of the box:
    • @sentry/nextjs
    • @sentry/sveltekit
    • @sentry/remix
    • @sentry/astro

If you are using a different package, and have not enabled performance monitoring, you can manually set up your application for distributed tracing to work.

If you want to use distributed tracing but not performance monitoring, set the tracesSampleRate option to 0.

  dsn: '',

  // If this is 0, performance monitoring is disabled
  // but distributed tracing is not
  tracesSampleRate: 0,

You can also manually extract and inject tracing data into your application. For this, you must:

  • Extract and store incoming tracing information from incoming request headers or similar.
  • Inject tracing information to any outgoing requests.

To learn more about distributed tracing, see our Distributed Tracing docs.

You must extract and store incoming tracing information in memory for later use. Sentry provides the continueTrace() function to help you with this. Incoming tracing information can come from different places:

  • In a web environment, it's sent with HTTP headers, for example, by another Sentry SDK used in your frontend project.
  • In a job queue, it can be retrieved from meta or header variables.
  • You also can pick up tracing information from environment variables.

Here's an example of how to extract and store incoming tracing information using continueTrace():

const http = require('http');

http.createServer((request, response) => {
  const sentryTraceHeaders = request.headers['sentry-trace'];
  const sentryTrace = Array.isArray(sentryTraceHeaders)
    ? sentryTraceHeaders.join(',')
    : sentryTraceHeaders;
  const baggage = request.headers['baggage'];

  Sentry.continueTrace({sentryTrace, baggage}, transactionContext => {
        name: 'my request',
        op: 'http.server',
      () => {
        // Your API code...

In this example, we create a new transaction that is attached to the trace specified in the sentry-trace and baggage headers.

For distributed tracing to work, the two headers that you extracted and stored in the active root span, sentry-trace and baggage, must be added to outgoing HTTP requests.

Here's an example of how to collect and inject this tracing information to outgoing requests:

const activeSpan = Sentry.getActiveSpan();
const rootSpan = activeSpan ? Sentry.getRootSpan(activeSpan) : undefined;

// Create `sentry-trace` header
const sentryTraceHeader = rootSpan ? Sentry.spanToTraceHeader(rootSpan) : undefined;

// Create `baggage` header
const sentryBaggageHeader = rootSpan ? Sentry.spanToBaggageHeader(rootSpan) : undefined;

// Make outgoing request
fetch('', {
  method: 'GET',
  headers: {
    baggage: sentryBaggageHeader,
    'sentry-trace': sentryTraceHeader,
}).then(response => {
  // ...

In this example, tracing information is propagated to the project running at If this project uses a Sentry SDK, it will extract and save the tracing information for later use.

The two services are now connected with your custom distributed tracing implementation.

If you make outgoing requests from your project to other services, check if the headers sentry-trace and baggage are present in the request. If so, distributed tracing is working.

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").