Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(javascript): Document new startNewTrace API #10146

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/platforms/javascript/common/distributed-tracing/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ What happens in the background is that Sentry uses reads and further propagates

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.

## Trace Duration

<PlatformCategorySection supported={["browser"]}>

In the browser, the SDK automatically starts a new trace in the following situation:
Lms24 marked this conversation as resolved.
Show resolved Hide resolved

- On page load: Whenever the page is (re-)loaded, a new trace is started. Simultaneously, 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.
Lms24 marked this conversation as resolved.
Show resolved Hide resolved
- On navigation: Whenever a user navigates (for example in a single-page application), a new trace is started. Simultaneously, 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.
Lms24 marked this conversation as resolved.
Show resolved Hide resolved

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what's missing for me is a description of what causes a trace to end.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the paragraphs above: "the trace remains until the next navigation or page load"
So IOW, the span only ends when users reload the page or navigate to another part of the page. Does this make sense?


</PlatformCategorySection>

<PlatformCategorySection supported={["server"]}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Should we instead make this notSupported={['browser']}? There is also serverless which I think is not covered by either of these 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I had no idea that serverless isn't covered by "server". Good to know!
Yup, will change it to notSupported then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, so turns out, <PlatformCategorySection> doesn't have a notSupported prop. So for now I just added serverless here. Tbh, this feels like a huge footgun to me. Historically we always assumed serverless===server in docs. I'm afraid we have some places where we should include serverless but don't exactly for this reason. I'll see if there's a simple improvement we can make here...


Server-side SDKs handle traces automatically on a per-request basis. This means that SDKs will
Lms24 marked this conversation as resolved.
Show resolved Hide resolved

- continue an existing trace if the incoming request contains a trace header
- start a new trace if the incoming request does not contain a trace header. This trace stays active until the response is sent.
Lms24 marked this conversation as resolved.
Show resolved Hide resolved

</PlatformCategorySection>

If necessary, you can override the default trace duration by [manually starting a new trace](./custom-instrumentation#starting-a-new-trace).

## How to Use Distributed Tracing?

<PlatformContent includePath="distributed-tracing/how-to-use/" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,28 @@ In this example, tracing information is propagated to the project running at `ht

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

### Starting a New Trace

In case the SDK's [default behavior](../#trace-duration) for the trace duration does not fir your use case, you can manually start a new trace that will no longer be connected to the current (distributed) trace.
Lms24 marked this conversation as resolved.
Show resolved Hide resolved
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.

To start a new trace that remains valid throughout the duration of a callback, use `startNewTrace`:

```javascript {2-6}
myButton.addEventListener("click", async () => {
Sentry.startNewTrace(() => {
Sentry.startSpan(
{ op: "ui.interaction.click", name: "fetch click" },
async () => {
await fetch("http://example.com");
}
);
});
});
```

Once the callback ended, the SDK will continue the previous trace (if available).
Lms24 marked this conversation as resolved.
Show resolved Hide resolved

## Verification

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.
Loading