Skip to content

Commit 0b49bc7

Browse files
authored
[v9] Encourage using inheritOrSampleWith for tracesSampler in js (#12544)
1 parent 5ceda8b commit 0b49bc7

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

docs/platforms/javascript/common/configuration/sampling.mdx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ The Sentry SDKs have two configuration options to control the volume of transact
4242

4343
2. Sampling function (<PlatformIdentifier name="traces-sampler" />) which:
4444
- Samples different transactions at different rates
45-
- <PlatformLink to="/configuration/filtering/">Filters</PlatformLink> out some
46-
transactions entirely
45+
- <PlatformLink to="/configuration/filtering/">Filters</PlatformLink> out
46+
some transactions entirely
4747
- Modifies default [precedence](#precedence) and [inheritance](#inheritance) behavior
4848

4949
By default, none of these options are set, meaning no transactions will be sent to Sentry. You must set either one of the options to start sending transactions.
@@ -98,7 +98,6 @@ There are multiple ways for a transaction to end up with a sampling decision.
9898

9999
When there's the potential for more than one of these to come into play, the following precedence rules apply:
100100

101-
1. If a sampling decision is passed to <PlatformIdentifier name="start-transaction" />, that decision will be used, overriding everything else.
102101
1. If <PlatformIdentifier name="traces-sampler" /> is defined, its decision will be used. It can choose to keep or ignore any parent sampling decision, use the sampling context data to make its own decision, or choose a sample rate for the transaction. We advise against overriding the parent sampling decision because it will break distributed traces)
103102
1. If <PlatformIdentifier name="traces-sampler" /> is not defined, but there's a parent sampling decision, the parent sampling decision will be used.
104103
1. If <PlatformIdentifier name="traces-sampler" /> is not defined and there's no parent sampling decision, <PlatformIdentifier name="traces-sample-rate" /> will be used.

platform-includes/performance/sampling-function-intro/javascript.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ To use the sampling function, set the <PlatformIdentifier name="traces-sampler"
33
<PlatformContent includePath="performance/traces-sampler-as-sampler" />
44

55
For convenience, the function can also return a boolean. Returning `true` is equivalent to returning `1`, and will guarantee the transaction will be sent to Sentry. Returning `false` is equivalent to returning `0` and will guarantee the transaction will **not** be sent to Sentry.
6+
Note that sampling decisions will be inherited for downstream services if you have set up distributed tracing.

platform-includes/performance/traces-sampler-as-sampler/javascript.mdx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ interface SamplingContext {
55
name: string;
66
// Initial attributes of the span
77
attributes: SpanAttributes | undefined;
8-
// If the parent span was sampled - undefined if there is no parent span
8+
// If the parent span was sampled - undefined if there is no incoming trace
99
parentSampled: boolean | undefined;
10+
// Sample rate that is coming from the incoming trace - undefined if there is no incoming trace
11+
parentSampleRate: number | undefined;
1012
}
1113

1214
Sentry.init({
1315
// ...
1416

15-
tracesSampler: ({ name, attributes, parentSampled }) => {
17+
tracesSampler: ({ name, attributes, inheritOrSampleWith }) => {
1618
// Do not sample health checks ever
1719
if (name.includes("healthcheck")) {
18-
// Drop this completely by setting its sample rate to 0%
1920
return 0;
2021
}
2122

@@ -29,13 +30,17 @@ Sentry.init({
2930
return 0.01;
3031
}
3132

32-
// Continue trace decision, if there is any parentSampled information
33-
if (typeof parentSampled === "boolean") {
34-
return parentSampled;
35-
}
36-
37-
// Else, use default sample rate
38-
return 0.5;
33+
// Otherwise, inherit the sample sampling decision of the incoming trace, or use a fallback sampling rate.
34+
return inheritOrSampleWith(0.5);
3935
},
4036
});
4137
```
38+
39+
<Alert title="parentSampleRate">
40+
41+
The `inheritOrSampleWith` sampling context utility was introduced in version 9 of the SDK.
42+
To inherit sampling decisions in earlier versions of the SDK, use the `parentSampled` sampling context.
43+
44+
Going forward, using `inheritOrSampleWith()` is strongly encouraged over using `parentSampled`, because it allows for deterministic sampling and metric extrapolation for downstream traces.
45+
46+
</Alert>

0 commit comments

Comments
 (0)