7

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?

user85461
  • 6,510
  • 2
  • 34
  • 40
  • I think the Django integration should ensure that the logger is not used for capturing REPL errors. I opened an issue here: https://github.com/getsentry/sentry-python/issues/122 – Markus Unterwaditzer Oct 12 '18 at 14:13

5 Answers5

9

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

Markus Unterwaditzer
  • 7,992
  • 32
  • 60
  • This doesn't appear to work. I tried both a call to `sentry_sdk.init()` at the module level for the management command, and also within the handler, and calls to `logging.error` were still captured by sentry. Also in the REPL, a call to `sentry_sdk.init()` followed by `logging.error("Don't capture me!")` still gets captured. – user85461 Oct 12 '18 at 18:32
  • 1
    @user85461 you need to update the SDK to at least 0.4.0. This was a bug – Markus Unterwaditzer Oct 12 '18 at 20:25
1

The accepted answer to call init with no args did not work for me.

I had to explicitly pass an empty DSN:

import sentry_sdk
sentry_sdk.init(dsn="")

Tested with sentry_sdk 0.19.1 and 1.17.0.

>>> import sentry_sdk
>>> sentry_sdk.Hub.current.client.dsn
"https://...redacted...@sentry.io/..."
>>> sentry_sdk.init(dsn="")
>>> sentry_sdk.Hub.current.client.dsn
''

After calling init with no args, the DSN remained unchanged.

A B
  • 11
  • 4
  • Maybe it's because it falls back to SENTRY_DSN env variable? https://docs.sentry.io/platforms/python/configuration/options/#dsn – Greg Finley Jan 05 '23 at 23:47
0

Maybe there’s a better way but in any file you can import logging and disable it like so: logging.disable(logging.CRITICAL). This will disabling logging at the level at or below the parameter (since CRITICAL is the highest it’ll disable all logging).

Dmitry M
  • 390
  • 1
  • 2
  • 11
  • I still want logging - for console output and interactive use. I just don't want sentry to capture events based on those logs. – user85461 Oct 12 '18 at 18:32
0

According to the docs for .NET @ https://getsentry.github.io/sentry-dotnet/api/Sentry.SentrySdk.html#Sentry_SentrySdk_Init_System_String_ sentry_sdk.init() "An empty string is interpreted as a disabled SDK". Yes, I know this is a Python question but their API is generally consistent across languages

CAD bloke
  • 8,578
  • 7
  • 65
  • 114
0

Very late to the party, but for anyone that comes to this from April 2021 you can simply:

import sentry_sdk
sentry_sdk.Hub.current.stop_auto_session_tracking()

The call to stop_auto_session_tracking() will always stop sentry errors as long as you include it (1) after you do sentry_sdk.init (2) Before any errors in your application occur.

jaitaiwan
  • 190
  • 1
  • 12