Filtering and Sampling Errors
You'll learn how to intelligently reduce error noise and manage your monitoring quota by filtering irrelevant errors and sampling high-volume events.
The Problem of Error Noise
Not every error deserves your immediate attention. Third-party browser extensions, network hiccups, and bot traffic can flood your error tracking system with thousands of irrelevant reports. Meanwhile, your monitoring service may charge based on event volume or impose rate limits. You need smart filtering and sampling to see what actually matters.
Plain English: What Are We Doing?
Think of error tracking like a security camera system. You don't want to record every leaf blowing past—you'd run out of storage and miss the actual break-in. Filtering is deciding which events to ignore completely (leaves blowing). Sampling is recording only a percentage of repetitive events (record every 10th car passing, not all 10,000).
Implementing beforeSend Hooks
The beforeSend hook intercepts errors before they're sent to your monitoring service. Here you can filter out noise:
Sentry.init({
beforeSend(event, hint) {
// Filter third-party script errors
if (event.exception?.values?.[0]?.stacktrace?.frames?.[0]?.filename?.includes('chrome-extension://')) {
return null; // Don't send this error
}
// Filter network errors from specific domains
if (event.message?.includes('cdn.external-ads.com')) {
return null;
}
return event; // Send everything else
}
});
Setting Sample Rates
Configure sampling to capture a representative subset of high-frequency errors:
Sentry.init({
dsn: 'your-dsn',
sampleRate: 0.25, // Send 25% of errors
tracesSampleRate: 0.1 // Send 10% of performance traces
});
Key Takeaway: Use beforeSend hooks to filter unwanted errors and configure sample rates to control volume—keeping your monitoring costs down and signal-to-noise ratio high.