Skip to main content

Can I set different sample rates for different errors?

Issue

Sentry offers tracesSampler to set different sample rates for different transactions. I want to know whether I can do the same for errors and choose a different sample rate per error, for example based on the event level.

Applies To

  • All SaaS Customers and Self-Hosted Users

  • Error Monitoring

  • Filtering/Sampling

Resolution

The error sample rate applies equally to all error events. It works as a random filter: for every event the SDK tries to send, it generates a random number. If the number is smaller than your sample rate, the SDK sends the event. Otherwise, it drops the event.

There is no built-in option to set a different sample rate per error. You can achieve this with beforeSend and a switch statement (or the equivalent in your language). The example below samples by event level:

function beforeSend(event) {
try {
switch (event.level) {
case "error":
if (Math.random() > 0.5) {
return null;
}
break;
case "warning":
if (Math.random() > 0.1) {
return null;
}
break;
}
return event;
} catch (e) {
return event;
}
}

This keeps roughly 50% of error-level events and 10% of warning-level events. For more on filtering events, see the filtering documentation.

Did this answer your question?