sys.exit
Learn how to use Sentry to capture sys.exit calls.
The SysExitIntegration
records sys.exit
calls by capturing the SystemExit
exception raised by sys.exit
.
pip install --upgrade "sentry-sdk"
The SysExitIntegration
is disabled by default, and the SDK only captures SystemExit
exceptions automatically if this integration is manually enabled.
A sys.exit
call is considered "successful" when the function is passed a value of 0
or None
. Passing any other value results in an "unsuccessful" exit.
To enable the SysExitIntegration
and configure it to only capture unsuccessful sys.exit
calls, use the following:
import sentry_sdk
from sentry_sdk.integrations.sys_exit import SysExitIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
integrations=[SysExitIntegration()],
)
If you wish to capture successful and unsuccessful sys.exit
calls, use the following, instead:
import sentry_sdk
from sentry_sdk.integrations.sys_exit import SysExitIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
integrations=[SysExitIntegration(capture_successful_exits=True)],
)
Running the following snippet should result in a SystemExit
exception being reported to Sentry.
import sys
import sentry_sdk
from sentry_sdk.integrations.sys_exit import SysExitIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
integrations=[SysExitIntegration()],
)
sys.exit(1)
If you use capture_successful_exits=True
, calling sys.exit(0)
should also report a SystemExit
exception to Sentry.
Manually-raised SystemExit
Please note that the SysExitIntegration
only captures SystemExit
exceptions which are raised by calling sys.exit
. If your code raises SystemExit
without calling sys.exit
, the exception will not be reported to Sentry.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").