Skip to content

Commit ffb4cf8

Browse files
committed
Updates-Dist Tracing-Samples and configs
1 parent 91abaad commit ffb4cf8

31 files changed

+860
-124
lines changed

docs/platforms/javascript/common/tracing/configure-sampling/index.mdx

Lines changed: 67 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@ The JavaScript SDK provides two main options for controlling the sampling rate:
1313
1. Uniform Sample Rate (`tracesSampleRate`)
1414
This option sets a fixed percentage of transactions to be captured:
1515

16-
```javascript
17-
Sentry.init({
18-
dsn: "https://[email protected]/0",
19-
integrations: [Sentry.browserTracingIntegration()],
20-
// Capture 25% of all transactions
21-
tracesSampleRate: 0.25
22-
});
23-
```
16+
<PlatformContent includePath="/tracing/sample-rate" />
2417

2518
With `tracesSampleRate` set to `0.25`, approximately 25% of transactions will be recorded and sent to Sentry. This provides an even cross-section of transactions regardless of where in your app they occur.
2619

@@ -33,33 +26,74 @@ For more granular control, you can use the `tracesSampler` function. This approa
3326
- Make sampling decisions based on transaction data
3427
- Control the inheritance of sampling decisions in distributed traces
3528

29+
<PlatformContent includePath="/tracing/trace-sampler" />
30+
31+
### Trace Sampler Examples
32+
33+
1. Prioritizing Critical User Flows
34+
3635
```javascript
37-
Sentry.init({
38-
dsn: "https://[email protected]/0",
39-
integrations: [Sentry.browserTracingIntegration()],
40-
tracesSampler: (samplingContext) => {
41-
// Access transaction details from the sampling context
42-
const { name, attributes, inheritOrSampleWith } = samplingContext;
43-
44-
// Skip health checks entirely
45-
if (name.includes('healthcheck')) {
46-
return 0;
47-
}
48-
49-
// Capture all auth-related transactions
50-
if (name.includes('auth')) {
51-
return 1;
52-
}
53-
54-
// Sample only 1% of comment-related transactions
55-
if (name.includes('comment')) {
56-
return 0.01;
57-
}
58-
59-
// For everything else, inherit parent sampling decision or use 0.5
60-
return inheritOrSampleWith(0.5);
36+
tracesSampler: (samplingContext) => {
37+
const { name, attributes } = samplingContext;
38+
39+
// Sample all checkout transactions
40+
if (name.includes('/checkout') || attributes?.flow === 'checkout') {
41+
return 1.0;
42+
}
43+
44+
// Sample 50% of login transactions
45+
if (name.includes('/login') || attributes?.flow === 'login') {
46+
return 0.5;
6147
}
62-
});
48+
49+
// Sample 10% of everything else
50+
return 0.1;
51+
}
52+
```
53+
54+
2. Handling Different Environments
55+
56+
```javascript
57+
tracesSampler: (samplingContext) => {
58+
// Sample all transactions in development
59+
if (process.env.NODE_ENV === 'development') {
60+
return 1.0;
61+
}
62+
63+
// Sample 5% in production
64+
if (process.env.NODE_ENV === 'production') {
65+
return 0.05;
66+
}
67+
68+
// Sample 20% in staging
69+
return 0.2;
70+
}
71+
```
72+
73+
3. Controlling Sampling Based on User or Transaction Properties
74+
75+
```javascript
76+
tracesSampler: (samplingContext) => {
77+
const { attributes, inheritOrSampleWith } = samplingContext;
78+
79+
// Always sample for premium users
80+
if (attributes?.userTier === 'premium') {
81+
return 1.0;
82+
}
83+
84+
// Sample more transactions for users experiencing errors
85+
if (attributes?.hasRecentErrors === true) {
86+
return 0.8;
87+
}
88+
89+
// Sample less for high-volume, low-value paths
90+
if (attributes?.path?.includes('/api/metrics')) {
91+
return 0.01;
92+
}
93+
94+
// Default sampling rate
95+
return inheritOrSampleWith(0.2);
96+
}
6397
```
6498
6599
## The Sampling Context Object
@@ -152,74 +186,6 @@ webSocket.send(JSON.stringify({
152186
}));
153187
```
154188
155-
## Real-world Use Cases for Trace Samplers
156-
157-
1. Prioritizing Critical User Flows
158-
159-
```javascript
160-
tracesSampler: (samplingContext) => {
161-
const { name, attributes } = samplingContext;
162-
163-
// Sample all checkout transactions
164-
if (name.includes('/checkout') || attributes?.flow === 'checkout') {
165-
return 1.0;
166-
}
167-
168-
// Sample 50% of login transactions
169-
if (name.includes('/login') || attributes?.flow === 'login') {
170-
return 0.5;
171-
}
172-
173-
// Sample 10% of everything else
174-
return 0.1;
175-
}
176-
```
177-
178-
2. Handling Different Environments
179-
180-
```javascript
181-
tracesSampler: (samplingContext) => {
182-
// Sample all transactions in development
183-
if (process.env.NODE_ENV === 'development') {
184-
return 1.0;
185-
}
186-
187-
// Sample 5% in production
188-
if (process.env.NODE_ENV === 'production') {
189-
return 0.05;
190-
}
191-
192-
// Sample 20% in staging
193-
return 0.2;
194-
}
195-
```
196-
197-
3. Controlling Sampling Based on User or Transaction Properties
198-
199-
```javascript
200-
tracesSampler: (samplingContext) => {
201-
const { attributes, inheritOrSampleWith } = samplingContext;
202-
203-
// Always sample for premium users
204-
if (attributes?.userTier === 'premium') {
205-
return 1.0;
206-
}
207-
208-
// Sample more transactions for users experiencing errors
209-
if (attributes?.hasRecentErrors === true) {
210-
return 0.8;
211-
}
212-
213-
// Sample less for high-volume, low-value paths
214-
if (attributes?.path?.includes('/api/metrics')) {
215-
return 0.01;
216-
}
217-
218-
// Default sampling rate
219-
return inheritOrSampleWith(0.2);
220-
}
221-
```
222-
223189
## Conclusion
224190
225191
Effective sampling is key to getting the most value from Sentry's performance monitoring while minimizing overhead. The `tracesSampler` function gives you precise control over which transactions to record, allowing you to focus on the most important parts of your application.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ notSupported:
66
- javascript.cordova
77
---
88

9-
If the overall application landscape that you want to observe with Sentry consists of more than just a single service or application, distributed tracing can add a lot of value.
10-
11-
## What is Distributed Tracing?
12-
139
<PlatformContent includePath="distributed-tracing/explanation" />
1410

1511
## Basic Example
@@ -25,8 +21,12 @@ What happens in the background is that Sentry uses reads and further propagates
2521
- `sentry-trace`
2622
- `baggage`
2723

24+
<Alert>
25+
2826
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.
2927

28+
</Alert>
29+
3030
## How to Use Distributed Tracing?
3131

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

docs/platforms/javascript/common/tracing/span-metrics/examples.mdx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ Sentry.startSpan(
172172
span.setAttribute('ui.total_request_time_ms', totalRequestTime);
173173
span.setAttribute('stream.rendering_mode', 'markdown');
174174
span.setAttribute('stream.tokens_received', tokensReceived);
175-
span.setAttribute('user.perceived_latency_ms',
176-
firstTokenReceived ? totalRequestTime - span.getAttribute('ui.time_to_first_token_ms') : totalRequestTime);
177175
}
178176
}
179177
);
@@ -197,14 +195,6 @@ Sentry.startSpan(
197195
const span = Sentry.getActiveSpan();
198196
const startTime = Date.now();
199197

200-
// Record queue time if applicable
201-
if (requestQueuedAt) {
202-
const queueTime = startTime - requestQueuedAt;
203-
if (span) {
204-
span.setAttribute('llm.queue_time_ms', queueTime);
205-
}
206-
}
207-
208198
try {
209199
// Check rate limits before processing
210200
const rateLimits = await getRateLimits();
@@ -419,9 +409,10 @@ Sentry.startSpan(
419409
'outcome.output_size_bytes': 31457280 // 30MB
420410
}
421411
},
422-
async () => {
412+
async () => {
423413
// Job processing implementation
424-
}
414+
}
415+
)
425416
);
426417
```
427418

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Distributed tracing connects and records the path of requests as they travel through your distributed system, creating a comprehensive timeline of events that would otherwise be fragmented across different components.
1+
Distributed tracing connects and records the path of requests as they travel through the different tiers of your application architecture. If your architecture consists of multiple services that live on different sub-domains (e.g. `fe.example.com` and `api.example.com`), distributed tracing will help you follow the path of events as they move from one service to another.
22

33
This end-to-end visibility allows developers to identify bottlenecks, pinpoint the root cause of errors, and understand component interactions—turning what would be a complex debugging nightmare into a manageable process that improves system reliability and performance.

platform-includes/distributed-tracing/how-to-use/javascript.astro.mdx

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,68 @@
11
If you're using our Astro SDK, distributed tracing will work out of the box for the client and server runtimes.
2-
To get around possible [Browser CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) issues, you should define `tracePropagationTargets` on the client-side.
2+
3+
When you are interacting with other external API systems, you might have to define `tracePropagationTargets` to get around possible [Browser CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) issues.
34

45
```javascript
56
// sentry.client.config.js
67
Sentry.init({
78
dsn: "___PUBLIC_DSN___",
89
integrations: [Sentry.browserTracingIntegration()],
10+
// Capture 100% of spans. This is useful for development and debugging. Consider reducing in production or using traceSampler
11+
tracesSampleRate: 1.0,
912
tracePropagationTargets: ["https://myproject.org", /^\/api\//],
1013
});
1114
```
1215

16+
### Trace Propagation Examples
17+
18+
#### Example 1: Microservices E-commerce Platform
19+
20+
```javascript
21+
// sentry.client.config.js
22+
Sentry.init({
23+
dsn: "___PUBLIC_DSN___",
24+
integrations: [Sentry.browserTracingIntegration()],
25+
// Capture 100% of spans. This is useful for development and debugging. Consider reducing in production or using traceSampler
26+
tracesSampleRate: 1.0,
27+
tracePropagationTargets: [
28+
"https://api.myecommerce.com",
29+
"https://auth.myecommerce.com",
30+
/^\/internal-api\//
31+
],
32+
});
33+
```
34+
35+
This tells Sentry to track user journeys across three places:
36+
37+
* Your main API server (where product data comes from)
38+
* Your authentication server (where logins happen)
39+
* Any API calls that start with "/internal-api/" on your current domain
40+
41+
This way, if a customer experiences an error during checkout, you can see the complete path their request took across these different services.
42+
43+
#### Example 2: Mobile App with Backend Services
44+
45+
```javascript
46+
// sentry.client.config.js
47+
Sentry.init({
48+
dsn: "___PUBLIC_DSN___",
49+
integrations: [Sentry.browserTracingIntegration()],
50+
tracePropagationTargets: [
51+
"https://api.myapp.com",
52+
"https://media.myapp.com",
53+
/^\/local-api\//
54+
],
55+
});
56+
```
57+
58+
This configuration lets your app track user actions across:
59+
60+
* Your main API server (handles most app functions)
61+
* Your media server (handles images, videos, etc.)
62+
* Any local API endpoints in your app
63+
64+
If your app crashes while a user is uploading a photo, you can trace exactly where the problem occurred - in the app itself, the main API, or the media service.
65+
1366
### Disabling Distributed Tracing
1467

1568
If you want to disable distributed tracing and ensure no Sentry trace headers are sent, you can configure your SDK like this:

platform-includes/distributed-tracing/how-to-use/javascript.cloudflare.mdx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,55 @@ If you want to disable distributed tracing, set the `tracePropagationTargets` op
1111
```javascript
1212
Sentry.init({
1313
dsn: "___PUBLIC_DSN___",
14-
14+
// Capture 100% of spans. This is useful for development and debugging. Consider reducing in production or using traceSampler
15+
tracesSampleRate: 1.0,
1516
// Overwrite the defaults to ensure no trace headers are sent
1617
tracePropagationTargets: [],
1718
});
1819
```
20+
21+
### Trace Propagation Examples
22+
23+
#### Example 1: Microservices E-commerce Platform
24+
25+
```javascript
26+
Sentry.init({
27+
dsn: "___PUBLIC_DSN___",
28+
// Capture 100% of spans. This is useful for development and debugging. Consider reducing in production or using traceSampler
29+
tracesSampleRate: 1.0,
30+
tracePropagationTargets: [
31+
"https://api.myecommerce.com",
32+
"https://auth.myecommerce.com",
33+
/^\/internal-api\//
34+
],
35+
});
36+
```
37+
38+
This tells Sentry to track user journeys across three places:
39+
40+
* Your main API server (where product data comes from)
41+
* Your authentication server (where logins happen)
42+
* Any API calls that start with "/internal-api/" on your current domain
43+
44+
This way, if a customer experiences an error during checkout, you can see the complete path their request took across these different services.
45+
46+
#### Example 2: Mobile App with Backend Services
47+
48+
```javascript
49+
Sentry.init({
50+
dsn: "___PUBLIC_DSN___",
51+
tracePropagationTargets: [
52+
"https://api.myapp.com",
53+
"https://media.myapp.com",
54+
/^\/local-api\//
55+
],
56+
});
57+
```
58+
59+
This configuration lets your app track user actions across:
60+
61+
* Your main API server (handles most app functions)
62+
* Your media server (handles images, videos, etc.)
63+
* Any local API endpoints in your app
64+
65+
If your app crashes while a user is uploading a photo, you can trace exactly where the problem occurred - in the app itself, the main API, or the media service.

0 commit comments

Comments
 (0)