Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore certain Python errors from Sentry capture

I have Sentry configured to capture all errors from a Django+Celery application. It works ok, but I'm finding an obnoxious use case is when I have to restart my Celery workers, PostgreSQL database or messaging server, which causes thousands of various kinds of "database/messaging server cannot be reached" errors. This pollutes the Sentry reports, and sometimes even exceeds my event quota.

Their docs mention an "ignore_exceptions" parameter, but it's in their old deprecated client that I'm not using, nor is recommended to be used for new projects. How would you do this in the new API?

like image 396
Cerin Avatar asked Sep 06 '19 21:09

Cerin


People also ask

How do I ignore all errors in Python?

Using Try Exceptblock to catch the ZeroDivisionError exception and ignore it. In the above code, we catch the ZeroDivisionError exception and use pass to ignore it. So, when this exception happens, nothing will be thrown and the program will just keep running by ignoring the zero number.

Does Sentry catch all errors?

The Sentry SDK will automatically capture and report any unhandled error that happens in your application runtime without any additional configuration or explicit handling.

How do I check Sentry errors?

Step 2: Handle the errorGo to your email and open the notification from Sentry. Click "View on Sentry" to view the full details and context of this error in your Sentry account. Note that Sentry aggregates similar errors (events) into one issue. In your account, scroll down to the "Exception" stack trace.


1 Answers

You can use before-send to filter errors by arbitrary criteria. Since it's unclear what you actually want to filter by, here's an example that filters by type. However, you can extend it with custom logic to e.g. match by exception message.

import sentry_sdk

def before_send(event, hint):
    if 'exc_info' in hint:
        exc_type, exc_value, tb = hint['exc_info']
        if isinstance(exc_value, (IgnoredErrorFoo, IgnoredErrorBar)):
            return None
    return event

sentry_sdk.init(before_send=before_send)
like image 58
Markus Unterwaditzer Avatar answered Sep 28 '22 06:09

Markus Unterwaditzer