Official Sentry SDK for OpenTelemetry

This package allows you to send your OpenTelemetry trace data to Sentry via OpenTelemetry SpanProcessors.
If you are using @sentry/node, OpenTelemetry support is included out of the box. This package is only necessary if you
are setting up OpenTelemetry support for Sentry yourself.
Installation
npm install @sentry/opentelemetry
yarn add @sentry/opentelemetry
Note that @sentry/opentelemetry depends on the following peer dependencies:
@opentelemetry/api version 1.0.0 or greater
Usage
This package exposes a few building blocks you can add to your OpenTelemetry setup in order to capture OpenTelemetry
traces to Sentry.
This is how you can use this in your app:
- Initialize Sentry, e.g.
@sentry/node!
- Call
setupEventContextTrace(client)
- Add
SentrySampler as sampler
- Add
SentrySpanProcessor as span processor
- Register the Sentry context manager (
SentryAsyncLocalStorageContextManager, or wrapContextManagerClass for a custom base)
- Add
SentryPropagator as propagator
- Setup OTEL-powered async context strategy for Sentry via
setOpenTelemetryContextAsyncContextStrategy()
For example, you could set this up as follows:
import * as Sentry from '@sentry/node';
import {
SentryAsyncLocalStorageContextManager,
SentryPropagator,
SentrySampler,
SentrySpanProcessor,
setupEventContextTrace,
setOpenTelemetryContextAsyncContextStrategy,
} from '@sentry/opentelemetry';
import { context, propagation, trace } from '@opentelemetry/api';
function setupSentry() {
Sentry.init({
dsn: 'xxx',
});
const client = Sentry.getClient();
setupEventContextTrace(client);
const provider = new BasicTracerProvider({
sampler: new SentrySampler(client),
});
provider.addSpanProcessor(new SentrySpanProcessor());
trace.setGlobalTracerProvider(provider);
context.setGlobalContextManager(new SentryAsyncLocalStorageContextManager());
setOpenTelemetryContextAsyncContextStrategy();
}
A full setup example can be found in
node-experimental.
Sentry Tracer Provider
SentryTracerProvider is a minimal OpenTelemetry tracer provider which creates native Sentry spans directly.
It is useful when code uses the global OpenTelemetry API and you do not need the full OpenTelemetry SDK span processor
and exporter pipeline.
import { trace } from '@opentelemetry/api';
import { SentryTracerProvider } from '@sentry/opentelemetry';
trace.setGlobalTracerProvider(new SentryTracerProvider());
const span = trace.getTracer('example').startSpan('work');
span.end();
In @sentry/node, this is the default tracer provider.
The SentryTracerProvider does not handle OpenTelemetry logs and metrics.
Links