-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Add more detailed OTEL docs (#10872)
--------- Co-authored-by: Sigrid Huemer <32902192+s1gr1d@users.noreply.github.com> Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io> Co-authored-by: Liza Mock <liza.mock@sentry.io>
- Loading branch information
1 parent
7c0f986
commit def093a
Showing
8 changed files
with
307 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
docs/platforms/javascript/common/opentelemetry/custom-setup.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
--- | ||
title: Using Your Existing OpenTelemetry Setup | ||
description: "Learn how to use your existing custom OpenTelemetry setup with Sentry." | ||
supported: | ||
- javascript.nextjs | ||
- javascript.node | ||
- javascript.aws-lambda | ||
- javascript.azure-functions | ||
- javascript.connect | ||
- javascript.express | ||
- javascript.fastify | ||
- javascript.gcp-functions | ||
- javascript.hapi | ||
- javascript.koa | ||
- javascript.nestjs | ||
- javascript.sveltekit | ||
- javascript.astro | ||
- javascript.remix | ||
notSupported: | ||
- javascript | ||
sidebar_order: 0 | ||
--- | ||
|
||
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, | ||
}); | ||
|
||
// Ensure spans are correctly linked & sent to Sentry | ||
provider.addSpanProcessor(new SentrySpanProcessor()); | ||
|
||
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 | ||
|
||
If you have a custom OpenTelemetry setup and only want to use Sentry for error monitoring, you can skip adding the `SentrySpanProcessor`. You'll still need to add the `SentryContextManager`, `SentryPropagator`, and `SentrySampler` to your setup even if you don't want to send any tracing data to Sentry. Read on to learn why this is needed. | ||
|
||
|
||
In order for the Sentry SDK to work as expected, and for it to be in sync with OpenTelemetry, we need a few components to be in place. | ||
|
||
**Components needed for Sentry to work correctly:** | ||
|
||
- **SentryContextManager**: Ensures that the OpenTelemetry context is in sync with Sentry, for example to correctly isolate data between simultaneous requests. | ||
- **SentrySampler**: Ensures that the Sentry `tracesSampleRate` is respected. Even if you don't use Sentry for tracing, you'll still need this in order for trace propagation to work as expected. Read [Using a Custom Sampler](./#using-a-custom-sampler) if you want to use a custom sampler. | ||
- **SentryPropagator**: Ensures that trace propagation works correctly. | ||
- [Required Instrumentation](./#required-instrumentation): Ensures that trace propagation works correctly. | ||
|
||
**Additional components needed to also use Sentry for tracing:** | ||
|
||
- **SentrySpanProcessor**: Ensures that spans are correctly sent to Sentry. | ||
|
||
<Note> | ||
Trace propagation is needed for Sentry to automatically connect services together. (For example, if you want to connect the frontend and backend, or different backend services.) This makes it possible to see related errors across services. <PlatformLink to="/tracing/trace-propagation">Learn more about Trace Propagation.</PlatformLink> | ||
</Note> | ||
|
||
## Required Instrumentation | ||
|
||
By default, Sentry will register OpenTelemetry instrumentation to automatically capture spans for traces spanning incoming and outgoing HTTP requests, DB queries, and more. | ||
|
||
If tracing is not enabled (no `tracesSampleRate` is defined in the SDK configuration), only a minimal amount of OpenTelemetry instrumentation will be registered. This includes the following: | ||
|
||
- <PlatformLink to="/configuration/integrations/http/">httpIntegration</PlatformLink> registers [@opentelemetry/instrumentation-http](https://www.npmjs.com/package/@opentelemetry/instrumentation-http) | ||
- <PlatformLink to="/configuration/integrations/nodefetch/">nativeNodeFetchIntegration</PlatformLink> registers [opentelemetry-instrumentation-fetch-node](https://www.npmjs.com/package/opentelemetry-instrumentation-fetch-node) | ||
|
||
<PlatformSection notSupported={['javascript.aws-lambda', 'javascript.gcp-functions']}> | ||
<Note> | ||
If tracing is not enabled, performance instrumentations will not be registered but they will still be included in the bundle. If you | ||
want to reduce the bundle size or used dependencies, you can also <PlatformLink to="/configuration/tree-shaking/#setting-up-sentry-without-performance-integrations">Set up Sentry without Performance Integrations</PlatformLink>. | ||
</Note> | ||
</PlatformSection> | ||
|
||
These are needed to make sure that trace propagation works correctly. Additionally, the HTTP instrumentation is configured to ensure that request isolation is automatically applied for Sentry. | ||
|
||
If you want to add your own http/node-fetch instrumentation, you have to follow the following steps: | ||
|
||
### Custom HTTP Instrumentation | ||
|
||
You won't be able to add `@opentelemetry/instrumentation-http` yourself and will need to use `httpIntegration` in order for Sentry to work as expected. If you want to use custom configuration for your HTTP instrumentation, you can use the <PlatformLink to="/configuration/integrations/http/">httpIntegration configuration</PlatformLink>. | ||
|
||
### Custom Node Fetch Instrumentation | ||
|
||
If tracing is disabled, the Node Fetch instrumentation will not emit any spans. In this scenario, it will only inject sentry-specific trace propagation headers. You are free to add your own Node Fetch instrumentation on top of this which may emit spans as you like. | ||
|
||
## Using a Custom Sampler | ||
|
||
While you can use your own sampler, we recommend that you use the `SentrySampler`. This will ensure that the correct subset of traces will be sent to Sentry, based on your `tracesSampleRate`. It will also ensure that all other Sentry features like trace propagation work as expected. If you do need to use your own sampler, make sure to wrap your `SamplingResult` with our `wrapSamplingDecision` method like in the example below: | ||
|
||
```javascript | ||
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node"); | ||
const Sentry = require("@sentry/node"); | ||
const { | ||
SentrySpanProcessor, | ||
SentryPropagator, | ||
SentrySampler, | ||
wrapSamplingDecision | ||
} = require("@sentry/opentelemetry"); | ||
|
||
// implements Sampler from "@opentelemetry/sdk-trace-node" | ||
class CustomSampler { | ||
shouldSample(context, _traceId, _spanName, _spanKind, attributes, _links) { | ||
const decision = yourDecisionLogic(); | ||
|
||
// wrap the result | ||
return wrapSamplingDecision({ | ||
decision, | ||
context, | ||
spanAttributes: attributes, | ||
}); | ||
} | ||
|
||
toString() { | ||
return CustomSampler.name; | ||
} | ||
} | ||
|
||
const sentryClient = Sentry.init({ | ||
dsn: "___DSN___", | ||
skipOpenTelemetrySetup: true, | ||
|
||
// By defining any sample rate, | ||
// tracing intergations will be added by default | ||
// omit this if you do not want any performance integrations to be added | ||
tracesSampleRate: 0, | ||
}); | ||
|
||
const provider = new NodeTracerProvider({ | ||
sampler: new CustomSampler(), | ||
}); | ||
|
||
// ...rest of your setup | ||
|
||
// Validate that the setup is correct | ||
Sentry.validateOpenTelemetrySetup(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: OpenTelemetry Support | ||
description: "Learn how to use OpenTelemetry with Sentry." | ||
sidebar_order: 7000 | ||
supported: | ||
- javascript.nextjs | ||
- javascript.node | ||
- javascript.aws-lambda | ||
- javascript.azure-functions | ||
- javascript.connect | ||
- javascript.express | ||
- javascript.fastify | ||
- javascript.gcp-functions | ||
- javascript.hapi | ||
- javascript.koa | ||
- javascript.nestjs | ||
- javascript.sveltekit | ||
- javascript.astro | ||
- javascript.remix | ||
notSupported: | ||
- javascript | ||
--- | ||
|
||
The Sentry SDK uses [OpenTelemetry](https://opentelemetry.io/) under the hood. This means that any OpenTelemetry instrumentation that emits spans will automatically be picked up by Sentry without any further configuration. | ||
|
||
To start capturing traces and spans, set up <PlatformLink to='/tracing'>Tracing and Performance Monitoring with your Sentry SDK</PlatformLink>. If you don't use tracing, Sentry still connects to OpenTelemetry under the hood to ensure that context isolation and trace propagation works correctly. | ||
|
||
By default, Sentry will automatically set up OpenTelemetry for you, but you can also use your own OpenTelemetry setup. Read the guides below to learn how to use a custom OpenTelemetry setup or how to get the most out of the Sentry and OpenTelemetry integration. | ||
|
||
<PageGrid /> |
76 changes: 76 additions & 0 deletions
76
docs/platforms/javascript/common/opentelemetry/using-opentelemetry-apis.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
title: Using OpenTelemetry APIs | ||
description: "Learn how to use OpenTelemetry APIs with Sentry." | ||
supported: | ||
- javascript.nextjs | ||
- javascript.node | ||
- javascript.aws-lambda | ||
- javascript.azure-functions | ||
- javascript.connect | ||
- javascript.express | ||
- javascript.fastify | ||
- javascript.gcp-functions | ||
- javascript.hapi | ||
- javascript.koa | ||
- javascript.nestjs | ||
- javascript.sveltekit | ||
- javascript.astro | ||
- javascript.remix | ||
notSupported: | ||
- javascript | ||
sidebar_order: 1 | ||
--- | ||
|
||
Sentry supports OpenTelemetry APIs out of the box. Any spans started using OpenTelemetry APIs will be automatically captured by Sentry, while any spans started using the Sentry SDK will be automatically propagated to OpenTelemetry. | ||
|
||
## Adding Additional OpenTelemetry Instrumentation | ||
|
||
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 the example below: | ||
|
||
```javascript | ||
const Sentry = require("@sentry/node"); | ||
const { | ||
GenericPoolInstrumentation, | ||
} = require("@opentelemetry/instrumentation-generic-pool"); | ||
|
||
Sentry.init({ | ||
dsn: "___DSN___", | ||
|
||
// The SentrySampler will use this to determine which traces to sample | ||
tracesSampleRate: 1.0, | ||
}); | ||
|
||
// Afterwards, you can add additional instrumentation: | ||
Sentry.addOpenTelemetryInstrumentation(new GenericPoolInstrumentation()); | ||
``` | ||
|
||
Any instrumentation added like this or via `registerInstrumentations()` from `@opentelemetry/instrumentation` will be picked up by Sentry. | ||
|
||
## Using an OpenTelemetry Tracer | ||
|
||
We recommend using `Sentry.startSpan()` and related APIs to create spans, but you can also create spans using native OpenTelemetry APIs. | ||
|
||
You can access the tracer Sentry uses via `client.tracer` and then create spans with OpenTelemetry APIs, as shown below: | ||
|
||
```javascript | ||
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. | ||
## Modifying the default OpenTelemetry TracerProvider | ||
You can access the tracer provider set up by Sentry when using Sentry's default OpenTelemetry instrumentation. This makes it easy to, for example, add additional span processors if you want to send span data to another backend or similar. | ||
```javascript | ||
const Sentry = require("@sentry/node"); | ||
|
||
const provider = Sentry.getClient()?.traceProvider; | ||
provider?.addSpanProcessor(new MySpanProcessor()); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.