Skip to content

Commit 52aa453

Browse files
Lms24mydeavivianyentran
authored
feat(javascript): Document new startNewTrace API (#10146)
-------- Co-authored-by: Francesco Novy <[email protected]> Co-authored-by: vivianyentran <[email protected]>
1 parent 488d0d7 commit 52aa453

File tree

2 files changed

+48
-0
lines changed
  • docs/platforms/javascript/common/distributed-tracing
  • platform-includes/distributed-tracing/custom-instrumentation

2 files changed

+48
-0
lines changed

docs/platforms/javascript/common/distributed-tracing/index.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ What happens in the background is that Sentry uses reads and further propagates
2929

3030
If you run any JavaScript applications in your distributed system, make sure that those two headers are added to your CORS allowlist and won't be blocked or stripped by your proxy servers, gateways, or firewalls.
3131

32+
## Trace Duration
33+
34+
<PlatformCategorySection supported={["browser"]}>
35+
36+
In the browser, the SDK automatically starts a new trace in the following situations:
37+
38+
- On page load: Whenever the page is (re-)loaded, a new trace is started. At the same time, a `pageload` span is created (see <PlatformLink to="/performance/instrumentation/automatic-instrumentation">Performance Monitoring</PlatformLink>). Once this span ends, the trace remains until the next navigation or page load. In case the server serving the initial page already started a trace and sent the necessary [HTML tags](./custom-instrumentation/#extract-tracing-information-from-html-meta-tags) to the browser, the SDK will continue this trace instead of starting a new one.
39+
- On navigation: Whenever a user navigates (for example in a single-page application), a new trace is started. At the same time, a `navigation` span is created (see <PlatformLink to="/performance/instrumentation/automatic-instrumentation">Performance Monitoring</PlatformLink>). Once this span ends, the trace remains until the next navigation or page load.
40+
41+
In both cases, this means that if you start spans after the automatic `pageload` and `navigation` spans ended, they will still be part of the same trace. This makes it easy to connect what happened before and after your span.
42+
43+
</PlatformCategorySection>
44+
45+
<PlatformCategorySection supported={["server", "serverless"]}>
46+
47+
Server-side SDKs handle traces automatically on a per-request basis. This means that SDKs will:
48+
49+
- Continue an existing trace if the incoming request contains a trace header.
50+
- Start a new trace if the incoming request does not contain a trace header. This trace stays active until the response is sent.
51+
52+
</PlatformCategorySection>
53+
54+
If necessary, you can override the default trace duration by [manually starting a new trace](./custom-instrumentation#starting-a-new-trace).
55+
3256
## How to Use Distributed Tracing?
3357

3458
<PlatformContent includePath="distributed-tracing/how-to-use/" />

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,30 @@ In this example, tracing information is propagated to the project running at `ht
173173

174174
The two services are now connected with your custom distributed tracing implementation.
175175

176+
### Starting a New Trace
177+
178+
Available since SDK version `8.5.0`.
179+
180+
In case the SDK's [default behavior](../#trace-duration) for the trace duration does not fit your needs, you can manually start a new trace that will no longer be connected to the current (distributed) trace.
181+
This means that spans or errors collected by the SDK during this new trace will not be connected to spans or errors collected before or after this new trace.
182+
183+
To start a new trace that remains valid throughout the duration of a callback, use `startNewTrace`:
184+
185+
```javascript {2-6}
186+
myButton.addEventListener("click", async () => {
187+
Sentry.startNewTrace(() => {
188+
Sentry.startSpan(
189+
{ op: "ui.interaction.click", name: "fetch click" },
190+
async () => {
191+
await fetch("http://example.com");
192+
}
193+
);
194+
});
195+
});
196+
```
197+
198+
Once the callback ends, the SDK will continue the previous trace (if available).
199+
176200
## Verification
177201
178202
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.

0 commit comments

Comments
 (0)