Skip to content

Commit

Permalink
feat: Update docs for node opentelemetry (getsentry#8366)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Shana Matthews <[email protected]>
  • Loading branch information
mydea and shanamatthews authored Nov 10, 2023
1 parent 55b3faf commit 6b308b1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 42 deletions.
15 changes: 0 additions & 15 deletions src/includes/feature-stage-beta-opentelemetry.mdx

This file was deleted.

12 changes: 6 additions & 6 deletions src/platform-includes/performance/opentelemetry-install/node.mdx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
```bash {tabTitle:npm}
npm install @sentry/node @sentry/opentelemetry-node
npm install @sentry/node @sentry/opentelemetry
```

```bash {tabTitle:Yarn}
yarn add @sentry/node @sentry/opentelemetry-node
yarn add @sentry/node @sentry/opentelemetry
```

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

<Note>

Note that @sentry/opentelemetry-node depends on the following peer dependencies:
Note that `@sentry/opentelemetry` depends on the following peer dependencies:

- @opentelemetry/api, version 1.0.0 or greater
- @opentelemetry/sdk-trace-base, version 1.0.0 or greater, or a package that implements it, like @opentelemetry/sdk-node
- `@opentelemetry/api`, version 1.0.0 or greater
- `@opentelemetry/sdk-trace-base`, version 1.0.0 or greater, or a package that implements it, like `@opentelemetry/sdk-node`

</Note>
96 changes: 77 additions & 19 deletions src/platform-includes/performance/opentelemetry-setup/node.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
You need to register `SentrySpanProcessor` and `SentryPropagator` with your OpenTelemetry installation:
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.

<SignInNote />

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

// OpenTelemetry dependencies
const opentelemetry = require("@opentelemetry/sdk-node");
const otelApi = require("@opentelemetry/api");
const {
Expand All @@ -17,25 +25,75 @@ const {
const {
OTLPTraceExporter,
} = require("@opentelemetry/exporter-trace-otlp-grpc");
const {
AsyncLocalStorageContextManager,
} = require("@opentelemetry/context-async-hooks");

// Make sure to call `Sentry.init` BEFORE initializing the OpenTelemetry SDK
Sentry.init({
dsn: "___PUBLIC_DSN___",
tracesSampleRate: 1.0,
// set the instrumenter to use OpenTelemetry instead of Sentry
instrumenter: "otel",
// ...
});
function setupSentry() {
setupGlobalHub();

// Make sure to call `Sentry.init` BEFORE initializing the OpenTelemetry SDK
Sentry.init({
dsn: "___PUBLIC_DSN___",
tracesSampleRate: 1.0,
// set the instrumenter to use OpenTelemetry instead of Sentry
instrumenter: "otel",
// ...
});

const client = getCurrentHub().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),
});

const sdk = new opentelemetry.NodeSDK({
// Existing config
traceExporter: new OTLPTraceExporter(),
instrumentations: [getNodeAutoInstrumentations()],
// Ensure OpenTelemetry Context & Sentry Hub/Scope is synced
setOpenTelemetryContextAsyncContextStrategy();

// Sentry config
spanProcessor: new SentrySpanProcessor(),
textMapPropagator: new SentryPropagator(),
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:

```js
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
});

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

<Note>

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.

</Note>
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ notSupported:
description: "Using OpenTelemetry with Sentry Performance."
---

<Include name="feature-stage-beta-opentelemetry.mdx" />

<PlatformSection notSupported={["java"]}>

You can configure your [OpenTelemetry SDK](https://opentelemetry.io/) to send traces and spans to Sentry.
Expand Down

0 comments on commit 6b308b1

Please sign in to comment.