Skip to content

Commit 072bc24

Browse files
committed
Add sampling
1 parent 3ed61ed commit 072bc24

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

develop-docs/sdk/telemetry/spans/span-sampling.mdx

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,99 @@ title: Span Sampling & Filtering
44

55
Any APIs exposed to the user to sample or filter spans must adhere to the following design principles:
66

7-
- The APIs are optimized for trace completness
7+
- The APIs are optimized for trace completeness
88
- The APIs are optimized for conclusive sampling decisions
99

1010
## Sample root spans with `tracesSampleRate` & `tracesSampler`
1111

12+
The SDK is automatically initialized with a `tracesSampleRate` of `0`.
13+
When starting a root span, the configured rate is compared against a random number between 0 and 1 to decide if this root span will be sampled or not.
1214

15+
If the SDK is configured with a `tracesSampler`, the `tracesSampleRate` no longer applies.
16+
The `tracesSampler` callback must receive sufficient arguments from users to define their own sampling rules.
17+
This can include but is not limited to certain attributes from the root span, such as HTTP headers.
18+
The return value of the `tracesSampler` is a float between `0.0` and `1.0`.
19+
20+
```js
21+
Sentry.init({
22+
tracesSampler: ({ name, attributes, parentSampleRate }) => {
23+
// Inherit the trace parent's sample rate if there is one. Sampling is deterministic
24+
// for one trace, e.g. if the parent was sampled, all children will be sampled at the same rate.
25+
if (typeof parentSampleRate === "number") {
26+
return parentSampleRate;
27+
}
28+
29+
// Else, use a default sample rate (replacing tracesSampleRate).
30+
return 0.5;
31+
}
32+
})
33+
```
34+
35+
The `parentSampleRate` is a propagated value inside the baggage, using key `sentry-sample_rand`.
36+
The value stems from a truly random number between 0 and 1, generated when a new trace is started. If the SDK does not receive such a number in an incoming trace, a new, truly random number between 0 and 1 is generated.
37+
38+
In the following cases, the SDK must compare sample rates against this `parentSampleRate` instead of `math.random()`:
39+
40+
- When a `tracesSampler` is configured, e.g. `trace["sentry-sample_rand"] < tracesSampler()`
41+
42+
- When the SDK is the head of trace, this applies to sample decisions based on `tracesSampleRate`, e.g. `trace['sentry-sample_rand'] < config.tracesSampleRate`
43+
44+
If the `sentry-sample_rate` (`parentSampleRate`) is not available for any reason for an inbound trace, but the trace has the sampled flag set to true, the SDK injects `parentSampleRate: 1.0` into the `tracesSampler`.
45+
46+
If no `tracesSampler` is configured, a propagated sampling decision via the traceparent takes precedence over the `tracesSampleRate`. This behavior can be disabled by defining a `tracesSampler`.
47+
48+
### Parent Sampling Origins
49+
50+
If the SDK can parse an org ID from the configured DSN, this value must be propagated as a baggage entry with the key `sentry-org`. Given a DSN of `https://[email protected]/1`, the org ID is 1, based on `o1`.
51+
52+
On incoming traces, the SDK must compare the `sentry-org` baggage value against its own parsed value from the DSN. Only if both match, the parent sampling decisions applies.
53+
54+
This behavior can be disabled by setting `strictTracePropagation: false` in the SDK init call.
55+
56+
The SDK must be configurable with an optional `org: <org-id>` setting that takes precedence over the parsed value from the DSN.
1357

1458
## Filter spans with `ignoreSpans` & integration config
1559

60+
The SDK must implement a mechanism for users to filter out spans. The result must be binary (true/false).
61+
The `ignoreSpans` option accepts a glob pattern or string.
62+
The `integrations` option can perform in similar fashion or make explicit opt-out possible via a bool flag.
1663

64+
If both options are not feasible to be implemented in certain SDKs, other approaches must be explored that have the same outcome.
65+
66+
```js
67+
Sentry.init({
68+
ignoreSpans: [
69+
'GET /about',
70+
'events.signal *',
71+
],
72+
integrations: [
73+
fsIntegration: {
74+
ignoreSpans: [
75+
'fs.read',
76+
],
77+
readSpans: true,
78+
writeSpans: false,
79+
}
80+
]
81+
})
82+
```
1783

1884
## Sanitize span attributes with `beforeSendSpans`
1985

86+
This callback must not allow the removal of any spans from the span tree.
87+
It receives a deep copy of all spans in the span tree and their attributes.
88+
89+
```
90+
[
91+
{
92+
'name': 'GET /',
93+
'attributes': [
94+
'http.request.method': 'GET',
95+
'http.response.status_code': 200,
96+
]
97+
},
98+
]
99+
```
20100

101+
Users can mutate any exposed properties to perform sanitation on sensitive data or Pii.
102+
The return value `beforeSendSpans` should be merged with the original span tree prior to emission.

0 commit comments

Comments
 (0)