pythondjangologgingsentry

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?


Solution

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

    If you want to disable the SDK even though you have set the SENTRY_DSN variable, you need to run sentry_sdk.init(dsn="")