|
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 | +In the following cases, distributed tracing will be set up automatically for you: |
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 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` |
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 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: |
10 | 29 |
|
11 | 30 | - Extract and store incoming tracing information from HTML `meta` tags when loading the page |
12 | 31 | - Inject tracing information to the outgoing request when sending outgoing requests. |
13 | 32 |
|
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. |
15 | 34 |
|
16 | | -## Extract Tracing Information From HTML `meta` Tags |
| 35 | +### Extract Tracing Information From HTML `meta` Tags |
17 | 36 |
|
18 | 37 | 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. |
19 | 38 |
|
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: |
21 | 40 |
|
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: |
23 | 46 |
|
24 | 47 | ```javascript |
25 | 48 | // 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 | +); |
46 | 62 | ``` |
47 | 63 |
|
48 | 64 | 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 | 65 |
|
50 | | -## Inject Tracing Information to Outgoing Requests |
| 66 | +### Inject Tracing Information to Outgoing Requests |
51 | 67 |
|
52 | 68 | 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 | 69 |
|
|
0 commit comments