Skip to content

Commit acf929c

Browse files
mydeaLuca ForstnershanamatthewsLms24
authored
feat: Add custom distributed tracing docs for JS/Node (#8469)
--------- Co-authored-by: Luca Forstner <[email protected]> Co-authored-by: Shana Matthews <[email protected]> Co-authored-by: Lukas Stracke <[email protected]>
1 parent 6451bd9 commit acf929c

File tree

5 files changed

+188
-32
lines changed

5 files changed

+188
-32
lines changed

src/platform-includes/distributed-tracing/custom-instrumentation/javascript.mdx

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,75 @@
1-
On this page you will learn how to manually propagate trace information into and out of your JavaScript application. Please note, that you do not need to do this manually, if you [use one of our supported frameworks](/platforms/javascript/usage/distributed-tracing/#how-to-use-distributed-tracing), or you have our <PlatformLink to="/performance/">performance monitoring feature</PlatformLink> turned on.
1+
On this page you will learn how to manually propagate trace information into and out of your JavaScript application.
22

3-
To set it up manually, all you have to do is to make sure your application extracts incoming headers and to set those headers again when making an outgoing request within your application.
3+
Distributed tracing will be set up automatically if:
44

5-
To enable distributed tracing, add the `BrowserTracing` integration to your `Sentry.init` options, as described in the Performance <PlatformLink to="/performance/instrumentation/automatic-instrumentation/"> Automatic Instrumentation</PlatformLink> docs. The [Next.js](/platforms/javascript/guides/nextjs/) and [SvelteKit](/platforms/javascript/guides/sveltekit/) SDKs include this integration by default.
5+
- You've <PlatformLink to="/performance/">Set Up Performance</PlatformLink> in Sentry.
6+
- You're using one of the SDKs that include distributed tracing out of the box:
7+
- `@sentry/nextjs`
8+
- `@sentry/sveltekit`
9+
- `@sentry/remix`
10+
- `@sentry/astro`
611

7-
If you are using one of the frameworks listed above, see the <PlatformLink to="/distributed-tracing/instrumentation/automatic-instrumentation/">Automatic Instrumentation</PlatformLink>. page on how to configure distributed tracing.
12+
If you are using a different package, and have not enabled performance monitoring, you can manually set up your application for distributed tracing to work.
813

9-
If you prefer to implement custom distributed tracing, you must:
14+
## Enabling Distributed Tracing
1015

11-
- Extract and store incoming tracing information from HTML `meta` tags when loading the page
12-
- Inject tracing information to the outgoing request when sending outgoing requests.
16+
To enable distributed tracing for your frontend, add the `BrowserTracing` integration to your `Sentry.init()` options as described in the <PlatformLink to="/performance/instrumentation/automatic-instrumentation/">Automatic Instrumentation</PlatformLink> docs.
1317

14-
To learn more, see our <PlatformLink to="/distributed-tracing/">Distributed Tracing</PlatformLink> docs.
18+
If you want to use distributed tracing but not performance monitoring, set the `tracesSampleRate` option to `0`.
1519

16-
## Extract Tracing Information From HTML `meta` Tags
20+
```javascript
21+
Sentry.init({
22+
dsn: "___PUBLIC_DSN___",
23+
integrations: [new Sentry.BrowserTracing()],
1724

18-
Assuming the backend that renders the HTML has injected tracing information into the HTML as `meta` tags, you can extract that tracing information on `pageload` and use it to create new transactions connected to that incoming trace.
25+
// Optionally disable performance monitoring:
26+
// tracesSampleRate: 0,
27+
});
28+
```
1929

20-
See the docs on how to [Inject Tracing Information Into Rendered HTML](/platforms/python/distributed-tracing/instrumentation/custom-instrumentation/#inject-tracing-information-into-rendered-html) to learn more about how to configure your backend to inject this information.
30+
## Enabling Distributed Tracing without `BrowserTracing`
2131

22-
For example, on `pageload` you could do the following:
32+
If you don't want to use the `BrowserTracing` integration, you can manually extract and inject tracing data in your application to connect multiple systems. For this, you must:
2333

24-
```javascript
25-
// Read meta tag values
26-
const sentryTraceMetaTagValue = getMetaContent("sentry-trace");
27-
const baggageMetaTagValue = getMetaContent("baggage");
34+
- Extract and store incoming tracing information from HTML `<meta>` tags when loading the page.
35+
- Inject tracing information to any outgoing requests.
2836

29-
// Extract Sentry trace information
30-
const traceParentData = extractTraceparentData(sentryTraceMetaTagValue);
37+
To learn more about distributed tracing, see our <PlatformLink to="/usage/distributed-tracing/">Distributed Tracing</PlatformLink> docs.
3138

32-
// Create Dynamic Sampling Context
33-
const dynamicSamplingContext =
34-
baggageHeaderToDynamicSamplingContext(baggageMetaTagValue);
39+
### Extract Tracing Information From HTML `meta` Tags
3540

36-
// Create transaction context
37-
const transactionContext = {
38-
...traceParentData,
39-
metadata: {
40-
dynamicSamplingContext: dynamicSamplingContext,
41-
},
42-
};
41+
If you have a server that renders your application's HTML (SSR) and is also running a Sentry SDK, you can connect your backend to your backend via tracing.
42+
43+
To do this, have your server render HTML `<meta>` tags with the Sentry trace information. In your frontend, extract that tracing information when the page is loading and use it to create new transactions connected to that incoming backend trace.
4344

44-
// Start transaction with tracing information of meta tags
45-
const pageLoadTransaction = startTransaction(hub, transactionContext);
45+
Some Sentry backend SDKs provide a built-in way to inject these `<meta>` tags into rendered HTML. For example:
46+
47+
- [Python](/platforms/python/usage/distributed-tracing/custom-instrumentation/#inject-tracing-information-into-rendered-html)
48+
- [Ruby](/platforms/ruby/usage/distributed-tracing/custom-instrumentation/#inject-tracing-information-into-rendered-html)
49+
- [PHP](/platforms/php/usage/distributed-tracing/custom-instrumentation/#inject-tracing-information-into-rendered-html)
50+
51+
Then, on `pageload` you can do the following:
52+
53+
```javascript
54+
// Read meta tag values
55+
const sentryTrace = document.querySelector("meta[name=sentry-trace]")?.content;
56+
const baggage = document.querySelector("meta[name=baggage]")?.content;
57+
58+
const pageLoadTransaction = Sentry.continueTrace(
59+
{ sentryTrace, baggage },
60+
(transactionContext) => {
61+
return Sentry.startTransaction({
62+
...transactionContext,
63+
name: `Pageload: ${window.location.pathname}`,
64+
op: "pageload",
65+
});
66+
}
67+
);
4668
```
4769
48-
In this example, we create a new transaction that is attached to the trace specified in the `sentry-trace` and `baggage` HTML `meta` tags.
70+
In this example, we create a new transaction that is attached to the trace specified in the `sentry-trace` and `baggage` HTML `<meta>` tags.
4971
50-
## Inject Tracing Information to Outgoing Requests
72+
### Inject Tracing Information to Outgoing Requests
5173
5274
For distributed tracing to work, the two headers that you extracted and stored in the `pageLoadTransaction`, `sentry-trace` and `baggage`, must be added to outgoing XHR/fetch requests.
5375
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
On this page, you will learn how to manually propagate trace information into and out of your Node.js application.
2+
3+
Distributed tracing will be set up automatically if:
4+
5+
- You've <PlatformLink to="/performance/">Set Up Performance</PlatformLink> in Sentry.
6+
- You're using one of the platforms that include distributed tracing out of the box:
7+
- `@sentry/nextjs`
8+
- `@sentry/sveltekit`
9+
- `@sentry/remix`
10+
- `@sentry/astro`
11+
12+
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.
13+
If you can't find an integration for your framework, it is possible to set up distributed tracing for your application manually.
14+
15+
## Enabling Distributed Tracing
16+
17+
You can setup the `Http` integration in combination with `tracesSampleRate: 0` to enable distributed tracing without performance monitoring:
18+
19+
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.
20+
21+
If you want to use distributed tracing but not performance monitoring, set the `tracesSampleRate` option to `0`.
22+
23+
```javascript
24+
Sentry.init({
25+
dsn: "___PUBLIC_DSN___",
26+
integrations: [
27+
new Sentry.Integrations.Http({ tracing: true }),
28+
// The Undici integrations instruments fetch in Node 18+
29+
new Sentry.Integrations.Undici(),
30+
],
31+
32+
// Optionally disable performance monitoring:
33+
// tracesSampleRate: 0,
34+
});
35+
```
36+
37+
## Enabling Distributed Tracing without `Http`/`Undici` Integration
38+
39+
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:
40+
41+
- Extract and store incoming tracing information from incoming request headers or similar.
42+
- Inject tracing information to any outgoing requests.
43+
44+
To learn more about distributed tracing, see our <PlatformLink to="/usage/distributed-tracing/">Distributed Tracing</PlatformLink> docs.
45+
46+
### Extracting Incoming Tracing Information
47+
48+
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:
49+
50+
- In a web environment, it's sent with HTTP headers, for example, by another Sentry SDK used in your frontend project.
51+
- In a job queue, it can be retrieved from meta or header variables.
52+
- You also can pick up tracing information from environment variables.
53+
54+
Here's an example of how to extract and store incoming tracing information using `continueTrace()`:
55+
56+
```javascript
57+
const http = require("http");
58+
59+
http.createServer((request, response) => {
60+
const sentryTraceHeaders = request.headers["sentry-trace"];
61+
const sentryTrace = Array.isArray(sentryTraceHeaders)
62+
? sentryTraceHeaders.join(",")
63+
: sentryTraceHeaders;
64+
const baggage = request.headers["baggage"];
65+
66+
const requestTransaction = Sentry.continueTrace(
67+
{ sentryTrace, baggage },
68+
(transactionContext) => {
69+
return Sentry.startTransaction({
70+
...transactionContext,
71+
name: "my request",
72+
op: "http.server",
73+
});
74+
}
75+
);
76+
77+
// Your API code...
78+
79+
requestTransaction.finish();
80+
});
81+
```
82+
83+
In this example, we create a new transaction that is attached to the trace specified in the `sentry-trace` and `baggage` headers.
84+
85+
### Inject Tracing Information to Outgoing Requests
86+
87+
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.
88+
89+
Here's an example of how to collect and inject this tracing information to outgoing requests:
90+
91+
```javascript
92+
// Create `sentry-trace` header
93+
const sentryTraceHeader = requestTransaction.toTraceparent();
94+
95+
// Create `baggage` header
96+
const dynamicSamplingContext = requestTransaction.getDynamicSamplingContext();
97+
const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(
98+
dynamicSamplingContext
99+
);
100+
101+
// Make outgoing request
102+
fetch("https://example.com", {
103+
method: "GET",
104+
headers: {
105+
baggage: sentryBaggageHeader,
106+
"sentry-trace": sentryTraceHeader,
107+
},
108+
}).then((response) => {
109+
// ...
110+
});
111+
```
112+
113+
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.
114+
115+
The two services are now connected with your custom distributed tracing implementation.
116+
117+
## Verification
118+
119+
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.

src/platform-includes/distributed-tracing/how-to-use/javascript.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ Sentry.init({
88
});
99
```
1010

11+
### Custom Instrumentation
12+
13+
If you don't want to use performance monitoring, you can set up <PlatformLink to="/usage/distributed-tracing/custom-instrumentation/">Custom Instrumentation</PlatformLink> for distributed tracing.
14+
1115
If you're using version `7.57.x` or below, you'll need to have our <PlatformLink to="/performance/">performance monitoring feature enabled</PlatformLink> in order for distributed tracing to work.

src/platform-includes/distributed-tracing/how-to-use/node.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ app.use(Sentry.Handlers.requestHandler());
2121
app.use(Sentry.Handlers.tracingHandler());
2222
```
2323

24+
### Custom Instrumentation
25+
26+
If you don't want to use performance monitoring, you can set up <PlatformLink to="/usage/distributed-tracing/custom-instrumentation/">Custom Instrumentation</PlatformLink> for distributed tracing.
27+
2428
If you're using version `7.57.x` or below, you'll need to have our <PlatformLink to="/performance/">performance monitoring feature enabled</PlatformLink> in order for distributed tracing to work.

src/platforms/common/usage/distributed-tracing/custom-instrumentation.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ supported:
99
- android
1010
- ruby
1111
- dotnet
12+
- javascript
13+
- node
14+
notSupported:
15+
- elixir
16+
- javascript.cordova
17+
- kotlin-multiplatform
18+
- unreal
1219
---
1320

1421
<PlatformContent includePath="distributed-tracing/custom-instrumentation/" />

0 commit comments

Comments
 (0)