OpenTelemetry Support

You can configure your OpenTelemetry SDK to send traces and spans to Sentry.

Copied
npm install @sentry/nextjs @sentry/opentelemetry-node

The minimum required version of the @sentry/node and the @sentry/opentelemetry-node package is 7.20.0 and their versions should always be aligned.

  1. Follow the Next.js instructions to set up a manual OpenTelemetry configuration.

  2. Check to make sure you've added instrumentation.{js,ts} and instrumentation.node.{js,ts} files to your application. Note that, until this feature moves out of the experimental phase, Next.js requires you to set experimental.instrumentationHook: true in your next.config.js to enable these files.

  3. Enable your sentry.server.config.js file to be able to use the OpenTelemetry instrumenter option.

sentry.server.config.js
Copied
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  tracesSampleRate: 1.0,
  // set the instrumenter to use OpenTelemetry instead of Sentry
  instrumenter: "otel",
  // ...
});
  1. Update your instrumentation.node.{js,ts} so that it uses Sentry's OpenTelemetry SpanProcessor and SpanPropagator.
instrumentation.node.js
Copied
import { trace, context } from "@opentelemetry/api";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { Resource } from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node";
import {
  SentrySpanProcessor,
  SentryPropagator,
} from "@sentry/opentelemetry-node";

const sdk = new NodeSDK({
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: "next-app",
  }),
  // Sentry config
  spanProcessor: new SentrySpanProcessor(),
  textMapPropagator: new SentryPropagator(),
});

sdk.start();

With Sentry’s OpenTelemetry SDK, an OpenTelemetry Span becomes a Sentry Transaction or Span. The first Span sent through the Sentry SpanProcessor is a Transaction, and any child Span gets attached to the first Transaction upon checking the parent Span context. This is true for the OpenTelemetry root Span and any top level Span in the system. For example, a request sent from frontend to backend will create an OpenTelemetry root Span with a corresponding Sentry Transaction. The backend request will create a new Sentry Transaction for the OpenTelemetry Span. The Sentry Transaction and Span are linked as a trace for navigation and error tracking purposes.

If you need more fine grained control over Sentry, take a look at the Configuration page. In case you'd like to filter out transactions before sending them to Sentry (to get rid of health checks, for example), you may find the Filtering page 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").