Configure Sampling for Cordova
Learn how to configure sampling in your app using the Sentry JavaScript SDK.
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 (recommended)
- Sampling Function
tracesSampleRate is floating point value 0.0 and 1.0, which controls the probability that a transaction will be sampled.
Sentry.init({
dsn: "___PUBLIC_DSN___",
integrations: [Sentry.browserTracingIntegration()],
// Capture 25% of all transactions
tracesSampleRate: 0.25,
});
Sentry.init({
dsn: "___PUBLIC_DSN___",
integrations: [Sentry.browserTracingIntegration()],
// 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: "___PUBLIC_DSN___",
integrations: [Sentry.browserTracingIntegration()],
tracesSampler: (samplingContext) => {
// Access transaction details from the sampling context
const { transactionContext, parentSampled } = samplingContext;
// Skip health checks entirely
if (transactionContext.name.includes("healthcheck")) {
return 0;
}
// Capture all auth-related transactions
if (transactionContext.name.includes("auth")) {
return 1;
}
// Sample only 1% of comment-related transactions
if (transactionContext.name.includes("comment")) {
return 0.01;
}
// For everything else, inherit the parent sampling decision or use 0.5
return parentSampled !== undefined ? parentSampled : 0.5;
},
});
Sentry.init({
dsn: "___PUBLIC_DSN___",
integrations: [Sentry.browserTracingIntegration()],
tracesSampler: (samplingContext) => {
// Access transaction details from the sampling context
const { transactionContext, parentSampled } = samplingContext;
// Skip health checks entirely
if (transactionContext.name.includes("healthcheck")) {
return 0;
}
// Capture all auth-related transactions
if (transactionContext.name.includes("auth")) {
return 1;
}
// Sample only 1% of comment-related transactions
if (transactionContext.name.includes("comment")) {
return 0.01;
}
// For everything else, inherit the parent sampling decision or use 0.5
return parentSampled !== undefined ? parentSampled : 0.5;
},
});
When the tracesSampler function is called, it receives a samplingContext object with valuable information to help make sampling decisions:
interface SamplingContext {
// Context of the transaction, including its initial `name` and `op`
transactionContext: TransactionContext;
// Whether the parent transaction was sampled (undefined if there's no parent)
parentSampled: boolean | undefined;
// The current `window.location`
location: Location | undefined;
}
interface SamplingContext {
// Context of the transaction, including its initial `name` and `op`
transactionContext: TransactionContext;
// Whether the parent transaction was sampled (undefined if there's no parent)
parentSampled: boolean | undefined;
// The current `window.location`
location: Location | undefined;
}
- Prioritizing Critical User Flows
tracesSampler: (samplingContext) => {
const { transactionContext, parentSampled } = samplingContext;
// Sample all checkout transactions
if (transactionContext.name.includes("/checkout")) {
return 1.0;
}
// Sample 50% of login transactions
if (transactionContext.name.includes("/login")) {
return 0.5;
}
// Sample 10% of everything else, unless there's a parent sampling decision
return parentSampled !== undefined ? parentSampled : 0.1;
};
tracesSampler: (samplingContext) => {
const { transactionContext, parentSampled } = samplingContext;
// Sample all checkout transactions
if (transactionContext.name.includes("/checkout")) {
return 1.0;
}
// Sample 50% of login transactions
if (transactionContext.name.includes("/login")) {
return 0.5;
}
// Sample 10% of everything else, unless there's a parent sampling decision
return parentSampled !== undefined ? parentSampled : 0.1;
};
- Handling Different Environments
tracesSampler: (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 0.2;
};
tracesSampler: (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 0.2;
};
When multiple sampling mechanisms could apply, Sentry follows this order of precedence:
- If
tracesSampleris defined, its decision is used. Although thetracesSamplercan override the parent sampling decision, most users will want to ensure theirtracesSamplerrespects the parent sampling decision. - If no
tracesSampleris defined, but there is a parent sampling decision from an incoming distributed trace, we use the parent sampling decision - If neither of the above,
tracesSampleRateis used - If
tracesSampleRateis 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").