OpenTelemetry Support

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

Copied
npm install @sentry/node @sentry/opentelemetry

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

There are a few steps necessary in order to ensure that your OpenTelemetry setup is fully synced with your Sentry SDK. The following code snippet walks through the needed steps.

Copied
// Sentry dependencies
const Sentry = require("@sentry/node");
const {
  getClient,
  setupGlobalHub,
  SentryPropagator,
  SentrySampler,
  SentrySpanProcessor,
  setupEventContextTrace,
  wrapContextManagerClass,
  setOpenTelemetryContextAsyncContextStrategy,
} = require("@sentry/opentelemetry");

// OpenTelemetry dependencies
const opentelemetry = require("@opentelemetry/sdk-node");
const otelApi = require("@opentelemetry/api");
const {
  getNodeAutoInstrumentations,
} = require("@opentelemetry/auto-instrumentations-node");
const {
  OTLPTraceExporter,
} = require("@opentelemetry/exporter-trace-otlp-grpc");
const {
  AsyncLocalStorageContextManager,
} = require("@opentelemetry/context-async-hooks");

function setupSentry() {
  setupGlobalHub();

  // Make sure to call `Sentry.init` BEFORE initializing the OpenTelemetry SDK
  Sentry.init({
    dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
    tracesSampleRate: 1.0,
    // set the instrumenter to use OpenTelemetry instead of Sentry
    instrumenter: "otel",
    // ...
  });

  const client = getClient();
  setupEventContextTrace(client);

  // You can wrap whatever local storage context manager you want to use
  const SentryContextManager = wrapContextManagerClass(
    AsyncLocalStorageContextManager
  );

  const sdk = new opentelemetry.NodeSDK({
    // Existing config
    traceExporter: new OTLPTraceExporter(),
    instrumentations: [getNodeAutoInstrumentations()],

    // Sentry config
    spanProcessor: new SentrySpanProcessor(),
    textMapPropagator: new SentryPropagator(),
    contextManager: new SentryContextManager(),
    sampler: new SentrySampler(client),
  });

  // Ensure OpenTelemetry Context & Sentry Hub/Scope is synced
  setOpenTelemetryContextAsyncContextStrategy();

  sdk.start();
}

setupSentry();

Note that with this setup, you can only use OpenTelemetry tracing for performance. Sentry.startTransaction() will not work. You can either create spans via tracer.startActiveSpan(), or use the startSpan() / startInactiveSpan() methods exported from @sentry/opentelemetry, which are just thin wrappers around the OpenTelemetry API:

Copied
import { startSpan, startInactiveSpan } from "@sentry/opentelemetry";

startSpan({ name: "my span" }, (span) => {
  // span is an OpenTelemetry Span!
  span.setAttribute("my_attr", "value");
  // span is automatically ended at the end of the callback
});

const span = startInactiveSpan({ name: "my span" });
// do something
span.end();

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