Configure Sampling
Learn how to configure sampling in your app.
Sentry's tracing functionality helps you monitor application performance by capturing distributed traces, attaching attributes, and span performance across your application. However, Capturing traces for every transaction can generate significant volumes of data. Sampling allows you to control the amount of spans that are sent to Sentry from your application.
The JavaScript SDK provides two main options for controlling the sampling rate:
- Uniform Sample Rate (
tracesSampleRate
)tracesSampleRate
is floating point value 0.0 and 1.0, which controls the probability that a transaction will be sampled.
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
// Capture 25% of all transactions
tracesSampleRate: 0.25,
});
With tracesSampleRate
set to 0.25
, each transaction in your application is randomly sampled with a probability of 25%, so you can expect that one in every four transactions will be sent to Sentry.
For more granular control, you provide a tracesSampler
function. This approach allows you to:
- Apply different sampling rates to different types of transactions
- Filter out specific transactions entirely
- Make sampling decisions based on transaction data
- Control the inheritance of sampling decisions in distributed traces
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
tracesSampler: (samplingContext) => {
// Access transaction details from the sampling context
const { name, attributes, inheritOrSampleWith } = samplingContext;
// Skip health checks entirely
if (name.includes("healthcheck")) {
return 0;
}
// Capture all auth-related transactions
if (name.includes("auth")) {
return 1;
}
// Sample only 1% of comment-related transactions
if (name.includes("comment")) {
return 0.01;
}
// For everything else, inherit parent sampling decision or use 0.5
return inheritOrSampleWith(0.5);
},
});
When the tracesSampler
function is called, it receives a samplingContext
object with valuable information to help make sampling decisions:
interface SamplingContext {
// Name of the span/transaction
name: string;
// Initial attributes of the span/transaction
attributes: SpanAttributes | undefined;
// Whether the parent span was sampled (undefined if no incoming trace)
parentSampled: boolean | undefined;
// Sample rate from incoming trace (undefined if no incoming trace)
parentSampleRate: number | undefined;
// Utility function to inherit parent decision or fallback
inheritOrSampleWith: (fallbackRate: number) => number;
}
- Prioritizing Critical User Flows
tracesSampler: (samplingContext) => {
const { name, attributes, inheritOrSampleWith } = samplingContext;
// Sample all checkout transactions
if (name.includes("/checkout") || attributes?.flow === "checkout") {
return 1.0;
}
// Sample 50% of login transactions
if (name.includes("/login") || attributes?.flow === "login") {
return 0.5;
}
// Sample 10% of everything else
return inheritOrSampleWith(0.1);
};
- Handling Different Environments
tracesSampler: (samplingContext) => {
const { inheritOrSampleWith } = samplingContext;
// Sample all transactions in development
if (process.env.NODE_ENV === "development") {
return 1.0;
}
// Sample 5% in production
if (process.env.NODE_ENV === "production") {
return 0.05;
}
// Sample 20% in staging
return inheritOrSampleWith(0.2);
};
- Controlling Sampling Based on User or Transaction Properties
tracesSampler: (samplingContext) => {
const { attributes, inheritOrSampleWith } = samplingContext;
// Always sample for premium users
if (attributes?.userTier === "premium") {
return 1.0;
}
// Sample more transactions for users experiencing errors
if (attributes?.hasRecentErrors === true) {
return 0.8;
}
// Sample less for high-volume, low-value paths
if (attributes?.path?.includes("/api/metrics")) {
return 0.01;
}
// Default sampling rate
return inheritOrSampleWith(0.2);
};
When multiple sampling mechanisms could apply, Sentry follows this order of precedence:
- If
tracesSampler
is defined, its decision is used. Although thetracesSampler
can override the parent sampling decision, most users will want to ensure theirtracesSampler
respects the parent sampling decision. - If no
tracesSampler
is defined, but there is a parent sampling decision from an incoming distributed trace, we use the parent sampling decision - If neither of the above,
tracesSampleRate
is used - If
tracesSampleRate
is set to 0, no spans will be sampled, and no downstream spans will be sampled either since they inherit the parent sampling decision - If none of the above are set, no transactions are sampled and tracing will be disabled
Effective sampling is key to getting the most value from Sentry's performance monitoring while minimizing overhead. The tracesSampler
function gives you precise control over which transactions to record, allowing you to focus on the most important parts of your application.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").