Skip to content

Commit 7894c9f

Browse files
HazATcursoragent
andauthored
Update documentation on trace propagation (#14232)
Co-authored-by: Cursor Agent <[email protected]>
1 parent ea7a5d9 commit 7894c9f

File tree

1 file changed

+48
-3
lines changed
  • docs/platforms/javascript/common/tracing/distributed-tracing/dealing-with-cors-issues

1 file changed

+48
-3
lines changed

docs/platforms/javascript/common/tracing/distributed-tracing/dealing-with-cors-issues/index.mdx

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,53 @@ notSupported:
1616
- javascript.nestjs
1717
---
1818

19-
If your frontend and backend are hosted on different domains (for example, your frontend is on `https://example.com` and your backend is on `https://api.example.com`), and the frontend does XHR/fetch requests to your backend, you'll need to configure your backend [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers) headers to ensure requests aren't blocked.
19+
If your frontend and backend are hosted on different domains (for example, your frontend is on `https://example.com` and your backend is on `https://api.example.com`), you need to configure your backend [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers) headers to prevent requests from being blocked by the browser.
2020

21-
Configure your backend CORS to allow the `sentry-trace` and `baggage` headers.
21+
## Understanding Trace Propagation
2222

23-
Your server's response header configuration might look like: `"Access-Control-Allow-Headers: sentry-trace, baggage"`. Your configuration will be specific to your setup.
23+
Sentry doesn't attach the `sentry-trace` and `baggage` headers to every outgoing request. Instead, it only attaches these headers to requests whose URLs match the patterns specified in the `tracePropagationTargets` configuration option.
24+
25+
By default, `tracePropagationTargets` is set to `['localhost', /^\//]`, which means trace headers are only attached to:
26+
- Requests containing `localhost` in their URL (e.g., `http://localhost:3000/api`)
27+
- Requests whose URL starts with `/` (e.g., `/api/users`, `/graphql`)
28+
29+
This default behavior helps prevent sending trace data to third-party services and avoids potential CORS issues.
30+
31+
## Configuring Trace Propagation
32+
33+
To enable distributed tracing across different domains, you need to configure `tracePropagationTargets` to include the URLs of your backend services:
34+
35+
```javascript
36+
Sentry.init({
37+
dsn: "___PUBLIC_DSN___",
38+
integrations: [Sentry.browserTracingIntegration()],
39+
tracePropagationTargets: [
40+
"localhost", // For local development
41+
/^\/api\//, // For same-origin API calls
42+
"https://api.example.com", // For your backend domain
43+
"https://auth.example.com" // For additional services
44+
],
45+
});
46+
```
47+
48+
## Configuring CORS Headers
49+
50+
Once you've configured `tracePropagationTargets` to include your backend domains, you need to configure your backend to allow the trace headers:
51+
52+
```
53+
Access-Control-Allow-Headers: sentry-trace, baggage
54+
```
55+
56+
Your server's exact configuration will depend on your setup, but the important part is allowing both the `sentry-trace` and `baggage` headers.
57+
58+
## Disabling Trace Propagation
59+
60+
If you want to disable distributed tracing entirely and ensure no trace headers are sent:
61+
62+
```javascript
63+
Sentry.init({
64+
dsn: "___PUBLIC_DSN___",
65+
// Empty array prevents all trace header propagation
66+
tracePropagationTargets: [],
67+
});
68+
```

0 commit comments

Comments
 (0)