Skip to content

Commit 1065f99

Browse files
cursoragentHazAT
andcommitted
Update distributed tracing docs with detailed CORS and trace propagation guidance
Co-authored-by: dgriesser <[email protected]>
1 parent 7030caa commit 1065f99

File tree

1 file changed

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

1 file changed

+89
-3
lines changed

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

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,94 @@ 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 may need to configure your backend [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers) headers to ensure distributed tracing works properly.
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 reduces 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+
## Example Scenarios
59+
60+
### Single Backend Domain
61+
62+
If your frontend calls a single backend domain:
63+
64+
```javascript
65+
// Frontend configuration
66+
Sentry.init({
67+
dsn: "___PUBLIC_DSN___",
68+
tracePropagationTargets: [
69+
"localhost", // For development
70+
"https://api.example.com" // Your backend
71+
],
72+
});
73+
```
74+
75+
```
76+
# Backend CORS configuration
77+
Access-Control-Allow-Headers: sentry-trace, baggage
78+
```
79+
80+
### Multiple Services
81+
82+
If your frontend calls multiple backend services:
83+
84+
```javascript
85+
// Frontend configuration
86+
Sentry.init({
87+
dsn: "___PUBLIC_DSN___",
88+
tracePropagationTargets: [
89+
"localhost",
90+
"https://api.example.com",
91+
"https://auth.example.com",
92+
"https://media.example.com"
93+
],
94+
});
95+
```
96+
97+
Each backend service needs to allow the headers in their CORS configuration.
98+
99+
### Disabling Trace Propagation
100+
101+
If you want to disable distributed tracing entirely and ensure no trace headers are sent:
102+
103+
```javascript
104+
Sentry.init({
105+
dsn: "___PUBLIC_DSN___",
106+
// Empty array prevents all trace header propagation
107+
tracePropagationTargets: [],
108+
});
109+
```

0 commit comments

Comments
 (0)