Skip to content

Commit 80c4df2

Browse files
codydemydea
andauthored
JavaScript Tracing Section Corrections (#13175)
* removing queues example * Sampling clarity updates * minor corrections to span metric details * Adding detail for sampling propagation in distributed traces * Updating descripotion to replace performance with trace * Update docs/platforms/javascript/common/tracing/configure-sampling/index.mdx Co-authored-by: Francesco Gringl-Novy <[email protected]> * Update docs/platforms/javascript/common/tracing/configure-sampling/index.mdx Co-authored-by: Francesco Gringl-Novy <[email protected]> * Correcting span sampling language * Update docs/platforms/javascript/common/tracing/configure-sampling/index.mdx Co-authored-by: Francesco Gringl-Novy <[email protected]> --------- Co-authored-by: Francesco Gringl-Novy <[email protected]>
1 parent 0310f39 commit 80c4df2

File tree

5 files changed

+64
-189
lines changed

5 files changed

+64
-189
lines changed

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

Lines changed: 32 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ Sentry's tracing functionality helps you monitor application performance by capt
1010

1111
The JavaScript SDK provides two main options for controlling the sampling rate:
1212

13-
### Uniform Sample Rate (`tracesSampleRate`)
14-
This option sets a fixed percentage of transactions to be captured:
13+
1. Uniform Sample Rate (`tracesSampleRate`)
14+
`tracesSampleRate` is floating point value 0.0 and 1.0, which controls the probability that a transaction will be sampled.
1515

1616
<PlatformContent includePath="/tracing/sample-rate" />
1717

18-
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.
18+
With `tracesSampleRate` set to `0.25`, each transaction in your application is randomly sampled with a probability of 25%, so you can expect that one in every four transactions will be sent to Sentry.
1919

2020
### Sampling Function (`tracesSampler`)
2121

22-
For more granular control, you can use the `tracesSampler` function. This approach allows you to:
22+
For more granular control, you provide a `tracesSampler` function. This approach allows you to:
2323

2424
- Apply different sampling rates to different types of transactions
2525
- Filter out specific transactions entirely
@@ -28,7 +28,30 @@ For more granular control, you can use the `tracesSampler` function. This approa
2828

2929
<PlatformContent includePath="/tracing/trace-sampler" />
3030

31-
#### Trace Sampler Examples
31+
### The Sampling Context Object
32+
33+
When the `tracesSampler` function is called, it receives a `samplingContext` object with valuable information to help make sampling decisions:
34+
35+
```typescript
36+
interface SamplingContext {
37+
// Name of the span/transaction
38+
name: string;
39+
40+
// Initial attributes of the span/transaction
41+
attributes: SpanAttributes | undefined;
42+
43+
// Whether the parent span was sampled (undefined if no incoming trace)
44+
parentSampled: boolean | undefined;
45+
46+
// Sample rate from incoming trace (undefined if no incoming trace)
47+
parentSampleRate: number | undefined;
48+
49+
// Utility function to inherit parent decision or fallback
50+
inheritOrSampleWith: (fallbackRate: number) => number;
51+
}
52+
```
53+
54+
### Trace Sampler Examples
3255

3356
1. Prioritizing Critical User Flows
3457

@@ -98,98 +121,17 @@ tracesSampler: (samplingContext) => {
98121
}
99122
```
100123
101-
## The Sampling Context Object
102-
103-
When the `tracesSampler` function is called, it receives a `samplingContext` object with valuable information to help make sampling decisions:
104-
105-
```typescript
106-
typescriptCopyinterface SamplingContext {
107-
// Name of the span/transaction
108-
name: string;
109-
110-
// Initial attributes of the span/transaction
111-
attributes: SpanAttributes | undefined;
112-
113-
// Whether the parent span was sampled (undefined if no incoming trace)
114-
parentSampled: boolean | undefined;
115-
116-
// Sample rate from incoming trace (undefined if no incoming trace)
117-
parentSampleRate: number | undefined;
118-
119-
// Utility function to inherit parent decision or fallback
120-
inheritOrSampleWith: (fallbackRate: number) => number;
121-
}
122-
```
123-
124-
The sampling context contains:
125-
126-
- `name`: The name of the transaction/span
127-
- `attributes`: Initial tags and attributes set on the transaction
128-
- `parentSampled`: Whether the parent transaction was sampled (for distributed tracing)
129-
- `parentSampleRate`: The sample rate used in the parent transaction
130-
- `inheritOrSampleWith`: A utility function to handle inheritance logic (recommended)
131-
132-
## Inheritance in Distributed Tracing
133-
134-
In distributed systems, trace information is propagated between services. The inheritOrSampleWith function simplifies handling parent sampling decisions:
135-
136-
```javascript
137-
tracesSampler: (samplingContext) => {
138-
const { name, inheritOrSampleWith } = samplingContext;
139-
140-
// Apply specific rules first
141-
if (name.includes('critical-path')) {
142-
return 1.0; // Always sample
143-
}
144-
145-
// Otherwise inherit parent sampling decision or fall back to 0.1
146-
return inheritOrSampleWith(0.1);
147-
}
148-
```
149-
This approach ensures consistent sampling decisions across your entire distributed trace. All transactions in a given trace will share the same sampling decision, preventing broken or incomplete traces.
150-
151-
**Note:** The `inheritOrSampleWith` helper was introduced in version 9 of the SDK. For earlier versions, you can implement similar logic manually using the `parentSampled` property.
152-
153124
## Sampling Decision Precedence
154125
155126
When multiple sampling mechanisms could apply, Sentry follows this order of precedence:
156127
157-
- If `tracesSampler` is defined, its decision is used (can consider parent sampling)
158-
- If no `tracesSampler` but parent sampling is available, parent decision is used
128+
- If `tracesSampler` is defined, its decision is used. Although the `tracesSampler` can override the parent sampling decision, most users will want to ensure their `tracesSampler` respects the parent sampling decision.
129+
- If no `tracesSampler` is defined, but there is a parent sampling decision from an incoming distributed trace, we use the parent sampling decision
159130
- If neither of the above, `tracesSampleRate` is used
160-
- If none of the above are set, no transactions are sampled (0%)
161-
162-
## How Sampling Propagates in Distributed Traces
163-
164-
Sentry uses a "head-based" sampling approach:
165-
166-
- A sampling decision is made in the originating service (the "head")
167-
- This decision is propagated to all downstream services via HTTP headers
168-
169-
The two key headers are:
170-
- `sentry-trace`: Contains trace ID, span ID, and sampling decision
171-
- `baggage`: Contains additional trace metadata including sample rate
172-
173-
Sentry automatically attaches these headers to outgoing HTTP requests when using the `browserTracingIntegration`. For other communication channels like WebSockets, you can manually propagate trace information:
174-
175-
```javascript
176-
// Extract trace data from the current scope
177-
const traceData = Sentry.getTraceData();
178-
const sentryTraceHeader = traceData["sentry-trace"];
179-
const sentryBaggageHeader = traceData["baggage"];
180-
181-
// Add to your custom request (example using WebSocket)
182-
webSocket.send(JSON.stringify({
183-
message: "Your data here",
184-
metadata: {
185-
sentryTrace: sentryTraceHeader,
186-
baggage: sentryBaggageHeader
187-
}
188-
}));
189-
```
131+
- If `tracesSampleRate` is set to 0, no spans will be sampled, and no downstream spans will be sampled either since they inherit the parent sampling decision
132+
- If none of the above are set, no transactions are sampled and tracing will be disabled
190133
191134
## Conclusion
192135
193136
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.
194137
195-
By implementing a thoughtful sampling strategy, you'll get the performance insights you need without overwhelming your systems or your Sentry quota.

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,32 @@ Server-side SDKs handle traces automatically on a per-request basis. This means
6060
</PlatformCategorySection>
6161

6262
If necessary, you can override the default trace duration by [manually starting a new trace](./custom-instrumentation#starting-a-new-trace).
63+
64+
## How Sampling Propagates in Distributed Traces
65+
66+
Sentry uses a "head-based" sampling approach:
67+
68+
- A sampling decision is made in the originating service (the "head")
69+
- This decision is propagated to all downstream services
70+
71+
The two key headers are:
72+
- `sentry-trace`: Contains trace ID, span ID, and sampling decision
73+
- `baggage`: Contains additional trace metadata including sample rate
74+
75+
Sentry automatically attaches these headers to outgoing HTTP requests when using the `browserTracingIntegration`. For other communication channels like WebSockets, you can manually propagate trace information:
76+
77+
```javascript
78+
// Extract trace data from the current scope
79+
const traceData = Sentry.getTraceData();
80+
const sentryTraceHeader = traceData["sentry-trace"];
81+
const sentryBaggageHeader = traceData["baggage"];
82+
83+
// Add to your custom request (example using WebSocket)
84+
webSocket.send(JSON.stringify({
85+
message: "Your data here",
86+
metadata: {
87+
sentryTrace: sentryTraceHeader,
88+
baggage: sentryBaggageHeader
89+
}
90+
}));
91+
```

docs/platforms/javascript/common/tracing/instrumentation/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Instrumentation
3-
description: "Learn how to configure spans to capture performance data on any action in your app."
3+
description: "Learn how to configure spans to capture trace data on any action in your app."
44
sidebar_order: 40
55
---
66

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

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -330,99 +330,3 @@ The frontend span tracks the user's checkout experience, while the backend span
330330
- Monitor inventory availability impact on conversions
331331
- Measure end-to-end order completion times
332332
- Identify friction points in the user experience
333-
334-
## Job Scheduling and Processing Pipeline
335-
336-
**Challenge:** Understanding performance and reliability of distributed job processing systems, from job creation through completion.
337-
338-
**Solution:** Comprehensive tracking of job lifecycle across scheduling, queueing, and processing stages.
339-
340-
**Frontend Instrumentation:**
341-
```javascript
342-
// Client-side job submission and monitoring
343-
Sentry.startSpan(
344-
{
345-
name: 'Job Submission Flow',
346-
op: 'job.client',
347-
attributes: {
348-
// Job configuration
349-
'job.type': 'video_transcoding',
350-
'job.priority': 'high',
351-
'job.estimated_duration_ms': 300000,
352-
353-
// Input metrics
354-
'input.size_bytes': 52428800, // 50MB
355-
'input.format': 'mp4',
356-
'input.segments': 5,
357-
358-
// Client-side scheduling
359-
'schedule.requested_start': '2024-03-15T10:00:00Z',
360-
'schedule.deadline': '2024-03-15T11:00:00Z',
361-
362-
// Progress monitoring
363-
'monitor.polling_interval_ms': 5000,
364-
'monitor.status_updates_received': 12,
365-
'monitor.last_progress_percent': 45
366-
}
367-
},
368-
async () => {
369-
// Job submission and progress tracking implementation
370-
}
371-
);
372-
```
373-
374-
**Backend Instrumentation:**
375-
```javascript
376-
// Server-side job processing
377-
Sentry.startSpan(
378-
{
379-
name: 'Job Processing Pipeline',
380-
op: 'job.server',
381-
attributes: {
382-
// Queue metrics
383-
'queue.name': 'video-processing',
384-
'queue.provider': 'redis',
385-
'queue.length_at_enqueue': 23,
386-
'queue.wait_time_ms': 45000,
387-
388-
// Worker metrics
389-
'worker.id': 'worker-pod-123',
390-
'worker.current_load': 0.75,
391-
'worker.memory_usage_mb': 1024,
392-
393-
// Processing stages
394-
'processing.stages_completed': ['download', 'transcode', 'thumbnail'],
395-
'processing.stage_durations_ms': {
396-
'download': 12000,
397-
'transcode': 180000,
398-
'thumbnail': 5000
399-
},
400-
401-
// Resource utilization
402-
'resource.cpu_percent': 85,
403-
'resource.gpu_utilization': 0.92,
404-
'resource.memory_peak_mb': 2048,
405-
406-
// Job outcome
407-
'outcome.status': 'completed',
408-
'outcome.retry_count': 0,
409-
'outcome.output_size_bytes': 31457280 // 30MB
410-
}
411-
},
412-
async () => {
413-
// Job processing implementation
414-
}
415-
)
416-
);
417-
```
418-
419-
**How the Trace Works Together:**
420-
The frontend span tracks job submission and monitoring, while the backend span captures queue management and processing details. The distributed trace provides visibility into the entire job lifecycle, enabling you to:
421-
422-
- Monitor end-to-end job processing times and success rates
423-
- Track queue health and worker resource utilization
424-
- Identify bottlenecks in specific processing stages
425-
- Analyze job scheduling efficiency and queue wait times
426-
- Optimize resource allocation based on job characteristics
427-
428-
For more information about implementing these examples effectively, see our <PlatformLink to="/tracing/span-metrics/">Span Metrics guide</PlatformLink> which includes detailed best practices and implementation guidelines.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ When adding metrics as span attributes:
4141

4242
- Use consistent naming conventions (for example, `category.metric_name`)
4343
- Keep attribute names concise but descriptive
44-
- Use appropriate data types (string, number, boolean, or (non-mixed) arrays of these types)
44+
- Use appropriate data types (string, number, boolean, or an array containing only one of these types)
4545

4646
## Creating Dedicated Metric Spans
4747

48-
For more detailed operations, task, or process tracking, you can create custom dedicated spans that focus on specific metrics or attributes that you want to track. This approach provides better discoverability and more precise span configurations, however it can also appear to create more noise in your trace waterfall.
48+
For more detailed operations, tasks, or process tracking, you can create custom dedicated spans that focus on specific metrics or attributes that you want to track. This approach provides better discoverability and more precise span configurations, however it can also create more noise in your trace waterfall.
4949

5050
```javascript
5151
Sentry.startSpan(

0 commit comments

Comments
 (0)