Skip to content

Commit 91735ef

Browse files
authored
feat(js): Improve tracesSampler example (#10591)
This was partially done here: #10544 but one place was forgotten.
1 parent 982e2eb commit 91735ef

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed
Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
1-
```javascript
1+
```typescript
2+
// The shape of samplingContext that is passed to the tracesSampler function
3+
interface SamplingContext {
4+
// Name of the span
5+
name: string;
6+
// Initial attributes of the span
7+
attributes: SpanAttributes | undefined;
8+
// If the parent span was sampled - undefined if there is no parent span
9+
parentSampled: boolean | undefined;
10+
}
11+
212
Sentry.init({
313
// ...
414

5-
tracesSampler: samplingContext => {
6-
// Examine provided context data (including parent decision, if any) along
7-
// with anything in the global namespace to compute the sample rate or
8-
// sampling decision for this transaction
15+
tracesSampler: ({ name, attributes, parentSampled }) => {
16+
// Do not sample health checks ever
17+
if (name.includes("healthcheck")) {
18+
// Drop this completelty, by setting its sample rate to 0%
19+
return 0;
20+
}
921

10-
if ("...") {
11-
// These are important - take a big sample
22+
// These are important - take a big sample
23+
if (name.includes("auth")) {
1224
return 1;
13-
} else if ("...") {
14-
// These are less important or happen much more frequently - only take 1%
25+
}
26+
27+
// These are less important or happen much more frequently - only take 1%
28+
if (name.includes("comment")) {
1529
return 0.01;
16-
} else if ("...") {
17-
// These aren't something worth tracking - drop all transactions like this
18-
return 0;
19-
} else {
20-
// Default sample rate
21-
return 0.5;
2230
}
23-
};
31+
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;
39+
},
2440
});
2541
```

0 commit comments

Comments
 (0)