From 91735ef04dba4babd5fa9e9dcaad0f83b727a2bb Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 5 Jul 2024 09:58:05 +0200 Subject: [PATCH] feat(js): Improve tracesSampler example (#10591) This was partially done here: https://github.com/getsentry/sentry-docs/pull/10544 but one place was forgotten. --- .../traces-sampler-as-sampler/javascript.mdx | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/platform-includes/performance/traces-sampler-as-sampler/javascript.mdx b/platform-includes/performance/traces-sampler-as-sampler/javascript.mdx index 6c981ca0561a4..82fc774389ba3 100644 --- a/platform-includes/performance/traces-sampler-as-sampler/javascript.mdx +++ b/platform-includes/performance/traces-sampler-as-sampler/javascript.mdx @@ -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; + }, }); ```