Skip to content

Commit 8805616

Browse files
committed
feat: Add custom distributed tracing docs for JS/Node
1 parent 55b3faf commit 8805616

File tree

5 files changed

+158
-30
lines changed

5 files changed

+158
-30
lines changed

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

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,69 @@
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+
In the following cases, distributed tracing will be set up automatically for you:
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 have our <PlatformLink to="/performance/">performance monitoring feature</PlatformLink> turned on.
6+
- You are 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`
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 without Performance
15+
16+
You can add the `BrowserTracing` integration in your `Sentry.init()` options, as described in the Performance <PlatformLink to="/performance/instrumentation/automatic-instrumentation/">Automatic Instrumentation</PlatformLink> docs. If you combine this with `tracesSampleRate: 0`, you will have distributed tracing without performance monitoring:
17+
18+
```javascript
19+
Sentry.init({
20+
dsn: "___PUBLIC_DSN___",
21+
integrations: [new Sentry.BrowserTracing()],
22+
tracesSampleRate: 0,
23+
});
24+
```
25+
26+
## Enabling Distributed Tracing without `BrowserTracing`
27+
28+
If you do not want to use the `BrowserTracing` integration, you can manually extract and inject tracing information into your application. For this, you must:
1029

1130
- Extract and store incoming tracing information from HTML `meta` tags when loading the page
1231
- Inject tracing information to the outgoing request when sending outgoing requests.
1332

14-
To learn more, see our <PlatformLink to="/distributed-tracing/">Distributed Tracing</PlatformLink> docs.
33+
To learn more, see our <PlatformLink to="/usage/distributed-tracing/">Distributed Tracing</PlatformLink> docs.
1534

16-
## Extract Tracing Information From HTML `meta` Tags
35+
### Extract Tracing Information From HTML `meta` Tags
1736

1837
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.
1938

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.
39+
Some of our backend SDKs provide a built-in way to inject tracing information into rendered HTML. For example:
2140

22-
For example, on `pageload` you could do the following:
41+
- [Python](/platforms/python/usage/distributed-tracing/custom-instrumentation/#inject-tracing-information-into-rendered-html)
42+
- [Ruby](/platforms/ruby/usage/distributed-tracing/custom-instrumentation/#inject-tracing-information-into-rendered-html)
43+
- [PHP](/platforms/php/usage/distributed-tracing/custom-instrumentation/#inject-tracing-information-into-rendered-html)
44+
45+
Then, on `pageload` you can do the following:
2346

2447
```javascript
2548
// Read meta tag values
26-
const sentryTraceMetaTagValue = getMetaContent("sentry-trace");
27-
const baggageMetaTagValue = getMetaContent("baggage");
28-
29-
// Extract Sentry trace information
30-
const traceParentData = extractTraceparentData(sentryTraceMetaTagValue);
31-
32-
// Create Dynamic Sampling Context
33-
const dynamicSamplingContext =
34-
baggageHeaderToDynamicSamplingContext(baggageMetaTagValue);
35-
36-
// Create transaction context
37-
const transactionContext = {
38-
...traceParentData,
39-
metadata: {
40-
dynamicSamplingContext: dynamicSamplingContext,
41-
},
42-
};
43-
44-
// Start transaction with tracing information of meta tags
45-
const pageLoadTransaction = startTransaction(hub, transactionContext);
49+
const sentryTrace = getMetaContent("sentry-trace");
50+
const baggage = getMetaContent("baggage");
51+
52+
const pageLoadTransaction = Sentry.continueTrace(
53+
{ sentryTrace, baggage },
54+
(transactionContext) => {
55+
return Sentry.startTransaction({
56+
...transactionContext,
57+
name: "my pageload",
58+
op: "pageload",
59+
});
60+
}
61+
);
4662
```
4763

4864
In this example, we create a new transaction that is attached to the trace specified in the `sentry-trace` and `baggage` HTML `meta` tags.
4965

50-
## Inject Tracing Information to Outgoing Requests
66+
### Inject Tracing Information to Outgoing Requests
5167

5268
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.
5369

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
On this page you will learn how to manually propagate trace information into and out of your JavaScript application.
2+
3+
In the following cases, distributed tracing will be set up automatically for you:
4+
5+
- You have our <PlatformLink to="/performance/">performance monitoring feature</PlatformLink> turned on.
6+
- You are 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 are using a different package, and have not enabled performance monitoring, you can manually set up your application for distributed tracing to work.
13+
14+
## Enabling Distributed Tracing without Performance
15+
16+
You can setup the `Http` integration in combination with `tracesSampleRate: 0` to enable distributed tracing without performance monitoring:
17+
18+
```javascript
19+
Sentry.init({
20+
dsn: "___PUBLIC_DSN___",
21+
integrations: [new Sentry.Integrations.Http({ tracing: true })],
22+
tracesSampleRate: 0,
23+
});
24+
```
25+
26+
## Enabling Distributed Tracing without `Http` Integration
27+
28+
If you do not want to use the `Http` integration in conjuction with `tracing: true`, you can manually extract and inject tracing information into your application. For this, you must:
29+
30+
- Extract and store incoming tracing information from incoming request headers or similar.
31+
- Inject tracing information to the outgoing request when sending outgoing requests.
32+
33+
To learn more, see our <PlatformLink to="/usage/distributed-tracing/">Distributed Tracing</PlatformLink> docs.
34+
35+
### Extracting Incoming Tracing Information
36+
37+
Incoming tracing information has to be extracted and stored in memory for later use. Sentry provides the `continueTrace()` function to help you with this. Incoming tracing information can come from different places:
38+
39+
- In a web environment, it's sent with HTTP headers, for example, by another Sentry SDK used in your frontend project.
40+
- In a job queue, it can be retrieved from meta or header variables.
41+
- You also can pick up tracing information from environment variables.
42+
43+
Here's an example of how to extract and store incoming tracing information using `continueTrace()`:
44+
45+
```javascript
46+
const sentryTrace = getRequestHeader("sentry-trace");
47+
const baggage = getRequestHeader("baggage");
48+
49+
const requestTransaction = Sentry.continueTrace(
50+
{ sentryTrace, baggage },
51+
(transactionContext) => {
52+
return Sentry.startTransaction({
53+
...transactionContext,
54+
name: "my request",
55+
op: "http.server",
56+
});
57+
}
58+
);
59+
```
60+
61+
In this example, we create a new transaction that is attached to the trace specified in the `sentry-trace` and `baggage` headers.
62+
63+
### Inject Tracing Information to Outgoing Requests
64+
65+
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.
66+
67+
Here's an example of how to collect and inject this tracing information to outgoing requests:
68+
69+
```javascript
70+
// Create `sentry-trace` header
71+
const sentryTraceHeader = requestTransaction.toTraceparent();
72+
73+
// Create `baggage` header
74+
const dynamicSamplingContext = requestTransaction.getDynamicSamplingContext();
75+
const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(
76+
dynamicSamplingContext
77+
);
78+
79+
// Make outgoing request
80+
fetch("https://example.com", {
81+
method: "GET",
82+
headers: {
83+
baggage: sentryBaggageHeader,
84+
"sentry-trace": sentryTraceHeader,
85+
},
86+
}).then((response) => {
87+
// ...
88+
});
89+
```
90+
91+
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.
92+
93+
The two services are now connected with your custom distributed tracing implementation.
94+
95+
## Verification
96+
97+
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 do not want to use performance monitoring, you can instead 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 do not want to use performance monitoring, you can instead 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+
- javascript.nextjs
16+
- javascript.remix
17+
- javascript.sveltekit
18+
- javascript.astro
1219
---
1320

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

0 commit comments

Comments
 (0)