@@ -36,7 +36,9 @@ yarn add @sentry/node-core @sentry/opentelemetry @opentelemetry/api @opentelemet
36
36
37
37
Sentry should be initialized as early in your app as possible. It is essential that you call ` Sentry.init ` before you
38
38
require any other modules in your application, otherwise any auto-instrumentation will ** not** work.
39
- You also need to set up OpenTelemetry, if you prefer not to, consider using the ` @sentry/node ` SDK instead.
39
+
40
+ You also ** have to** set up OpenTelemetry, if you prefer not to, consider using the ` @sentry/node ` SDK instead.
41
+ Without setting up OpenTelemetry, you only get basic error tracking out of the box without proper scope isolation.
40
42
41
43
You need to create a file named ` instrument.js ` that imports and initializes Sentry:
42
44
@@ -57,23 +59,29 @@ const sentryClient = Sentry.init({
57
59
// ...
58
60
});
59
61
60
- // Note: This could be BasicTracerProvider or any other provider depending on how you want to use the
61
- // OpenTelemetry SDK
62
- const provider = new NodeTracerProvider ({
63
- // Ensure the correct subset of traces is sent to Sentry
64
- // This also ensures trace propagation works as expected
65
- sampler: sentryClient ? new SentrySampler (sentryClient) : undefined ,
66
- spanProcessors: [
67
- // Ensure spans are correctly linked & sent to Sentry
68
- new SentrySpanProcessor (),
69
- // Add additional processors here
70
- ],
71
- });
72
-
73
- trace .setGlobalTracerProvider (provider);
74
- propagation .setGlobalPropagator (new SentryPropagator ());
75
- context .setGlobalContextManager (new Sentry.SentryContextManager ());
76
-
62
+ if (sentryClient) {
63
+ // Note: This could be BasicTracerProvider or any other provider depending on how you want to use the
64
+ // OpenTelemetry SDK
65
+ const provider = new NodeTracerProvider ({
66
+ // Ensure the correct subset of traces is sent to Sentry
67
+ // This also ensures trace propagation works as expected
68
+ sampler: new SentrySampler (sentryClient),
69
+ spanProcessors: [
70
+ // Ensure spans are correctly linked & sent to Sentry
71
+ new SentrySpanProcessor (),
72
+ // Add additional processors here
73
+ ],
74
+ });
75
+
76
+ trace .setGlobalTracerProvider (provider);
77
+ propagation .setGlobalPropagator (new SentryPropagator ());
78
+ context .setGlobalContextManager (new Sentry.SentryContextManager ());
79
+ }
80
+
81
+ // Set up the OpenTelemetry logger to use Sentry's logger
82
+ Sentry .setupOpenTelemetryLogger ();
83
+
84
+ // validate your setup
77
85
Sentry .validateOpenTelemetrySetup ();
78
86
```
79
87
0 commit comments