-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): Improve tracesSampler example (#10591)
This was partially done here: #10544 but one place was forgotten.
- Loading branch information
Showing
1 changed file
with
32 additions
and
16 deletions.
There are no files selected for viewing
48 changes: 32 additions & 16 deletions
48
platform-includes/performance/traces-sampler-as-sampler/javascript.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}); | ||
``` |