OpenTelemetry Support

Using OpenTelemetry with Sentry Performance.

The Sentry SDK uses OpenTelemetry under the hood. This means that any OpenTelemetry instrumentation that emits spans will automatically be picked up by Sentry - no further configuration needed!

While the Sentry SDK includes some OpenTelemetry instrumentation out of the box, you may want to add additional instrumentation to your application. This can be done by registering the instrumentation through OpenTelemetry like this:

Copied
const Sentry = require("@sentry/node");
const {
  GenericPoolInstrumentation,
} = require("@opentelemetry/instrumentation-generic-pool");
const { registerInstrumentations } = require("@opentelemetry/instrumentation");

Sentry.init({
  dsn: "__DSN__",

  // The SentrySampler will use this to determine which traces to sample
  tracesSampleRate: 1.0,
});

// Afterwards, you can add additional instrumentation:
registerInstrumentations({
  instrumentations: [new GenericPoolInstrumentation()],
});

Any instrumentation added like this will be automatically picked up by Sentry.

If you already have OpenTelemetry set up yourself, you can also use your existing setup.

In this case, you need to set skipOpenTelemetrySetup: true in your init({}) config, and ensure you setup all the components that Sentry needs yourself. In this case, you need to install @sentry/opentelemetry, and add the following:

Copied
const Sentry = require("@sentry/node");
const {
  SentrySpanProcessor,
  SentryPropagator,
  SentryContextManager,
  SentrySampler,
} = require("@sentry/opentelemetry");

Sentry.init({
  dsn: "__DSN__",
  skipOpenTelemetrySetup: true,

  // The SentrySampler will use this to determine which traces to sample
  tracesSampleRate: 1.0,
});

// This e.g. sets up @opentelemetry/sdk-trace-node
const provider = manuallySetUpOpenTelemetry();

// We need a custom span processor
provider.addSpanProcessor(new SentrySpanProcessor());
// We need a custom propagator and context manager
provier.register({
  propagator: new SentryPropagator(),
  contextManager: new SentryContextManager(),
});

// We need our sampler to ensure the correct subset of traces is sent to Sentry
const provider = new BasicTracerProvider({
  sampler: new SentrySampler(Sentry.getClient()),
});

We recommend using Sentry.startSpan() and related APIs to create spans. However, you can also use native OpenTelemetry APIs to create spans.

You can access the tracer Sentry uses via client.tracer and then create spans with OpenTelemetry APIs, as shown below:

Copied
const Sentry = require("@sentry/node");

const tracer = Sentry.getClient()?.tracer;
// Now you can use native APIs on the tracer:
tracer.startActiveSpan("span name", () => {
  // measure something
});

You can also use any other tracer; all OpenTelemetry spans will be picked up by Sentry automatically.

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