|
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. |
2 | 2 |
|
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: |
4 | 4 |
|
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` |
6 | 11 |
|
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. |
8 | 13 |
|
9 | | -If you prefer to implement custom distributed tracing, you must: |
| 14 | +## Enabling Distributed Tracing |
10 | 15 |
|
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. |
13 | 17 |
|
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`. |
15 | 19 |
|
16 | | -## Extract Tracing Information From HTML `meta` Tags |
| 20 | +```javascript |
| 21 | +Sentry.init({ |
| 22 | + dsn: "___PUBLIC_DSN___", |
| 23 | + integrations: [new Sentry.BrowserTracing()], |
17 | 24 |
|
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 | +``` |
19 | 29 |
|
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` |
21 | 31 |
|
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: |
23 | 33 |
|
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. |
28 | 36 |
|
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. |
31 | 38 |
|
32 | | -// Create Dynamic Sampling Context |
33 | | -const dynamicSamplingContext = |
34 | | - baggageHeaderToDynamicSamplingContext(baggageMetaTagValue); |
| 39 | +### Extract Tracing Information From HTML `meta` Tags |
35 | 40 |
|
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. |
43 | 44 |
|
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 | +); |
46 | 68 | ``` |
47 | 69 |
|
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. |
49 | 71 |
|
50 | | -## Inject Tracing Information to Outgoing Requests |
| 72 | +### Inject Tracing Information to Outgoing Requests |
51 | 73 |
|
52 | 74 | 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. |
53 | 75 |
|
|
0 commit comments