Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent sentry from capturing events for some uncaught exceptions and logging messages?

As recommended by Sentry's docs [1][2] for their new unified python sdk (sentry_sdk), I've configured it with my Django application to capture events on all exceptions or "error"-level logs:

import sentry_sdk
import logging
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import LoggingIntegration

sentry_logging = LoggingIntegration(
    level=logging.DEBUG,
    event_level=logging.ERROR
)
sentry_sdk.init(
    dsn="{{sentry_dsn}}",
    integrations=[DjangoIntegration(), sentry_logging]
)

However, since this hooks directly into python's logging module and internal exception handling, it means anything that uses this Django environment will be shipping events to sentry. There are some tasks (such as interactive manage.py commands, or work in the REPL) that need the Django environment, but for which I don't want events created in Sentry.

Is there a way to indicate to sentry that I'd like it to not capture events from exceptions or logging calls for the current task? Or a way to temporarily disable it after it's been globally configured?

like image 628
user85461 Avatar asked Oct 12 '18 00:10

user85461


People also ask

How do I report an exception in the Sentry SDK?

The Sentry SDK contains several methods that you can use to explicitly report errors, events, and custom messages in except clauses, critical areas of your code, and so on. Open the views.py file. Notice that we import sentry_sdk lib which contains the capture_exception method:

What are the logging integrations in sentry?

Logging integrations, commonly referred to as appenders, used to translate log records generated by your application via a logging framework into Sentry events. These integrations are packaged as sentry-logback , sentry-log4j, and sentry-log4j2. Sentry, used by the logging integrations to send events to a Sentry server.

What happens when an error occurs in sentry Python?

There will be an error event with the message "I am an event". "I am a breadcrumb" will be attached as a breadcrumb to that event. bar will end up in the event's extra attributes. "An exception happened" will send the current exception from sys.exc_info () with the stack trace and everything to the Sentry Python SDK.

What is event_level in sentry?

event_level (default ERROR ): The Sentry Python SDK will report log records with a level higher than or equal to event_level as events. If a value of None occurs, the SDK won't send log records as events.


1 Answers

You can run sentry_sdk.init() (notably without a DSN) again to disable the SDK.

like image 111
Markus Unterwaditzer Avatar answered Nov 15 '22 00:11

Markus Unterwaditzer