Skip to content

Commit

Permalink
feat(js): Improve tracesSampler example (#10591)
Browse files Browse the repository at this point in the history
This was partially done here: #10544 but one place was forgotten.
  • Loading branch information
mydea committed Jul 5, 2024
1 parent 982e2eb commit 91735ef
Showing 1 changed file with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
```javascript
```typescript
// The shape of samplingContext that is passed to the tracesSampler function
interface SamplingContext {
// Name of the span
name: string;
// Initial attributes of the span
attributes: SpanAttributes | undefined;
// If the parent span was sampled - undefined if there is no parent span
parentSampled: boolean | undefined;
}

Sentry.init({
// ...

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

if ("...") {
// These are important - take a big sample
// These are important - take a big sample
if (name.includes("auth")) {
return 1;
} else if ("...") {
// These are less important or happen much more frequently - only take 1%
}

// These are less important or happen much more frequently - only take 1%
if (name.includes("comment")) {
return 0.01;
} else if ("...") {
// These aren't something worth tracking - drop all transactions like this
return 0;
} else {
// Default sample rate
return 0.5;
}
};

// Continue trace decision, if there is any parentSampled information
if (typeof parentSampled === "boolean") {
return parentSampled;
}

// Else, use default sample rate
return 0.5;
},
});
```

0 comments on commit 91735ef

Please sign in to comment.