-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: Add custom distributed tracing docs for JS/Node #8469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8805616
feat: Add custom distributed tracing docs for JS/Node
mydea 76dfcd9
Apply suggestions from code review
mydea b56d6f3
[getsentry/action-github-commit] Auto commit
getsantry[bot] a1b03bf
Apply suggestions from code review
mydea df1abe2
PR feedback
mydea 025585f
fix not supported
mydea 0e2d36f
Apply suggestions from code review
mydea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 54 additions & 32 deletions
86
src/platform-includes/distributed-tracing/custom-instrumentation/javascript.mdx
This file contains hidden or 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
119 changes: 119 additions & 0 deletions
119
src/platform-includes/distributed-tracing/custom-instrumentation/node.mdx
This file contains hidden or 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,119 @@ | ||
| On this page, you will learn how to manually propagate trace information into and out of your Node.js application. | ||
|
|
||
| Distributed tracing will be set up automatically if: | ||
|
|
||
| - You've <PlatformLink to="/performance/">Set Up Performance</PlatformLink> in Sentry. | ||
| - You're using one of the platforms that include distributed tracing out of the box: | ||
| - `@sentry/nextjs` | ||
| - `@sentry/sveltekit` | ||
| - `@sentry/remix` | ||
| - `@sentry/astro` | ||
|
|
||
| If you're not using any of the packages above, take a look at the framework-specific Sentry integrations (like our [Express integration](/platforms/node/guides/express/)). These integrations will also enable distributed tracing automatically for your application. | ||
| If you can't find an integration for your framework, it is possible to set up distributed tracing for your application manually. | ||
|
|
||
| ## Enabling Distributed Tracing | ||
|
|
||
| You can setup the `Http` integration in combination with `tracesSampleRate: 0` to enable distributed tracing without performance monitoring: | ||
|
|
||
| To enable distributed tracing for your frontend, configure the `Http` or `Undici` integration in your `Sentry.init()` options as described in the <PlatformLink to="/performance/instrumentation/automatic-instrumentation/">Automatic Instrumentation</PlatformLink> docs. | ||
|
|
||
| If you want to use distributed tracing but not performance monitoring, set the `tracesSampleRate` option to `0`. | ||
|
|
||
| ```javascript | ||
| Sentry.init({ | ||
| dsn: "___PUBLIC_DSN___", | ||
| integrations: [ | ||
| new Sentry.Integrations.Http({ tracing: true }), | ||
| // The Undici integrations instruments fetch in Node 18+ | ||
| new Sentry.Integrations.Undici(), | ||
| ], | ||
|
|
||
| // Optionally disable performance monitoring: | ||
| // tracesSampleRate: 0, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Enabling Distributed Tracing without `Http`/`Undici` Integration | ||
|
|
||
| If you do not want to use the `Http` or `Undici` integrations, you can manually extract and inject tracing data into your application. For this, you must: | ||
|
|
||
| - Extract and store incoming tracing information from incoming request headers or similar. | ||
| - Inject tracing information to any outgoing requests. | ||
|
|
||
| To learn more about distributed tracing, see our <PlatformLink to="/usage/distributed-tracing/">Distributed Tracing</PlatformLink> docs. | ||
|
|
||
| ### Extracting Incoming Tracing Information | ||
|
|
||
| You must extract and store incoming tracing information in memory for later use. Sentry provides the `continueTrace()` function to help you with this. Incoming tracing information can come from different places: | ||
|
|
||
| - In a web environment, it's sent with HTTP headers, for example, by another Sentry SDK used in your frontend project. | ||
| - In a job queue, it can be retrieved from meta or header variables. | ||
| - You also can pick up tracing information from environment variables. | ||
|
|
||
| Here's an example of how to extract and store incoming tracing information using `continueTrace()`: | ||
|
|
||
| ```javascript | ||
| const http = require("http"); | ||
|
|
||
| http.createServer((request, response) => { | ||
| const sentryTraceHeaders = request.headers["sentry-trace"]; | ||
| const sentryTrace = Array.isArray(sentryTraceHeaders) | ||
| ? sentryTraceHeaders.join(",") | ||
| : sentryTraceHeaders; | ||
| const baggage = request.headers["baggage"]; | ||
|
|
||
| const requestTransaction = Sentry.continueTrace( | ||
| { sentryTrace, baggage }, | ||
| (transactionContext) => { | ||
| return Sentry.startTransaction({ | ||
| ...transactionContext, | ||
| name: "my request", | ||
| op: "http.server", | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| // Your API code... | ||
|
|
||
| requestTransaction.finish(); | ||
| }); | ||
| ``` | ||
|
|
||
| In this example, we create a new transaction that is attached to the trace specified in the `sentry-trace` and `baggage` headers. | ||
|
|
||
| ### Inject Tracing Information to Outgoing Requests | ||
|
|
||
| For distributed tracing to work, the two headers that you extracted and stored in the `requestTransaction`, `sentry-trace` and `baggage`, must be added to outgoing http/fetch requests. | ||
|
|
||
| Here's an example of how to collect and inject this tracing information to outgoing requests: | ||
|
|
||
| ```javascript | ||
| // Create `sentry-trace` header | ||
| const sentryTraceHeader = requestTransaction.toTraceparent(); | ||
|
|
||
| // Create `baggage` header | ||
| const dynamicSamplingContext = requestTransaction.getDynamicSamplingContext(); | ||
| const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader( | ||
| dynamicSamplingContext | ||
| ); | ||
|
|
||
| // Make outgoing request | ||
| fetch("https://example.com", { | ||
| method: "GET", | ||
| headers: { | ||
| baggage: sentryBaggageHeader, | ||
| "sentry-trace": sentryTraceHeader, | ||
| }, | ||
| }).then((response) => { | ||
| // ... | ||
| }); | ||
| ``` | ||
|
|
||
| In this example, tracing information is propagated to the project running at `https://example.com`. If this project uses a Sentry SDK, it will extract and save the tracing information for later use. | ||
|
|
||
| The two services are now connected with your custom distributed tracing implementation. | ||
|
|
||
| ## Verification | ||
|
|
||
| If you make outgoing requests from your project to other services, check if the headers `sentry-trace` and `baggage` are present in the request. If so, distributed tracing is working. |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.