diff --git a/docs/platforms/javascript/common/opentelemetry/custom-setup.mdx b/docs/platforms/javascript/common/opentelemetry/custom-setup.mdx index 26b96725bcb712..bffb259b756e77 100644 --- a/docs/platforms/javascript/common/opentelemetry/custom-setup.mdx +++ b/docs/platforms/javascript/common/opentelemetry/custom-setup.mdx @@ -34,51 +34,7 @@ If you are looking to simply add individual OpenTelemetry instrumentation to you -To use an existing OpenTelemetry setup, set `skipOpenTelemetrySetup: true` in your `init({})` config, then set up all the components that Sentry needs yourself. Finish by installing `@sentry/opentelemetry` and adding the following: - -```javascript -const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node"); -const Sentry = require("@sentry/node"); -const { - SentrySpanProcessor, - SentryPropagator, - SentrySampler, -} = require("@sentry/opentelemetry"); - -const sentryClient = Sentry.init({ - dsn: "___DSN___", - skipOpenTelemetrySetup: true, - - // The SentrySampler will use this to determine which traces to sample - tracesSampleRate: 1.0, -}); - -// Note: This could be BasicTracerProvider or any other provider depending on -// how you are using the OpenTelemetry SDK -const provider = new NodeTracerProvider({ - // Ensure the correct subset of traces is sent to Sentry - // This also ensures trace propagation works as expected - sampler: sentryClient ? new SentrySampler(sentryClient) : undefined, - spanProcessors: [ - // Ensure spans are correctly linked & sent to Sentry - new SentrySpanProcessor(), - // Add additional processors here - ], -}); - -provider.register({ - // Ensure trace propagation works - // This relies on the SentrySampler for correct propagation - propagator: new SentryPropagator(), - // Ensure context & request isolation are correctly managed - contextManager: new Sentry.SentryContextManager(), -}); - -// Validate that the setup is correct -Sentry.validateOpenTelemetrySetup(); -``` - -Make sure that all [Required OpenTelemetry Instrumentation](./#required-instrumentation) is set up correctly. Otherwise, the Sentry SDK may not work as expected. + ## Using Sentry for Error Monitoring Only diff --git a/platform-includes/performance/opentelemetry-setup/javascript.mdx b/platform-includes/performance/opentelemetry-setup/javascript.mdx new file mode 100644 index 00000000000000..5801887f468b0d --- /dev/null +++ b/platform-includes/performance/opentelemetry-setup/javascript.mdx @@ -0,0 +1,78 @@ +To use an existing OpenTelemetry setup, set `skipOpenTelemetrySetup: true` in your `init({})` config, then set up all the components that Sentry needs yourself. Finish by installing `@sentry/opentelemetry` and adding the following: + +```javascript {tabTitle: NodeTracerProvider} +const Sentry = require("@sentry/node"); +const { SentrySpanProcessor, SentryPropagator, SentrySampler } = require("@sentry/opentelemetry"); + +const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node"); + +const sentryClient = Sentry.init({ + dsn: "___DSN___", + skipOpenTelemetrySetup: true, + + // The SentrySampler will use this to determine which traces to sample + tracesSampleRate: 1.0, +}); + +// Note: This could be BasicTracerProvider or any other provider depending on +// how you are using the OpenTelemetry SDK +const provider = new NodeTracerProvider({ + // Ensure the correct subset of traces is sent to Sentry + // This also ensures trace propagation works as expected + sampler: sentryClient ? new SentrySampler(sentryClient) : undefined, + spanProcessors: [ + // Ensure spans are correctly linked & sent to Sentry + new SentrySpanProcessor(), + // Add additional processors here + ], +}); + +provider.register({ + // Ensure trace propagation works + // This relies on the SentrySampler for correct propagation + propagator: new SentryPropagator(), + // Ensure context & request isolation are correctly managed + contextManager: new Sentry.SentryContextManager(), +}); + +// Validate that the setup is correct +Sentry.validateOpenTelemetrySetup(); +``` + +```javascript {tabTitle: NodeSDK} +const Sentry = require("@sentry/node"); +const { SentrySpanProcessor, SentryPropagator, SentrySampler } = require("@sentry/opentelemetry"); + +const { NodeSDK } = require("@opentelemetry/sdk-node") + +const sentryClient = Sentry.init({ + dsn: "___PUBLIC_DSN___", + skipOpenTelemetrySetup: true, + + // The SentrySampler will use this to determine which traces to sample + tracesSampleRate: 1.0, +}); + +const sdk = new NodeSDK({ + // Ensure the correct subset of traces is sent to Sentry + // This also ensures trace propagation works as expected + sampler: sentryClient ? new SentrySampler(sentryClient) : undefined, + spanProcessors: [ + // Ensure spans are correctly linked & sent to Sentry + new SentrySpanProcessor(), + // Add additional processors here + ], + // Ensure trace propagation works + // This relies on the SentrySampler for correct propagation + textMapPropagator: new SentryPropagator(), + // Ensure context & request isolation are correctly managed + contextManager: new Sentry.SentryContextManager() +}); + +sdk.start(); + +// Validate that the setup is correct +Sentry.validateOpenTelemetrySetup(); +``` + +Make sure that all [Required OpenTelemetry Instrumentation](./#required-instrumentation) is set up correctly. Otherwise, the Sentry SDK may not work as expected. diff --git a/platform-includes/performance/opentelemetry-setup/javascript.node.mdx b/platform-includes/performance/opentelemetry-setup/javascript.node.mdx deleted file mode 100644 index fab130b89c5a41..00000000000000 --- a/platform-includes/performance/opentelemetry-setup/javascript.node.mdx +++ /dev/null @@ -1,97 +0,0 @@ -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. - - -```javascript -// 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 - const client = Sentry.init({ - dsn: "___PUBLIC_DSN___", - tracesSampleRate: 1.0, - // set the instrumenter to use OpenTelemetry instead of Sentry - instrumenter: "otel", - // ... - }); - - 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: - -```javascript -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(); -``` - - - -If you are using the [@sentry/opentelemetry-node](https://www.npmjs.com/package/@sentry/opentelemetry-node) package, you can continue to do so. Usage instructions are in the -package's README. This package has a slightly easier setup process but -provides a more limited connection between Sentry and OpenTelemetry than the -instructions outlined above. - -