Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(python): Document sys.exit integration #11153

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/platforms/python/integrations/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ The Sentry SDK uses integrations to hook into the functionality of popular libra
| <LinkWithPlatformIcon platform="python.pure_eval" label="Enhanced Locals" url="/platforms/python/integrations/pure_eval" /> | |
| <LinkWithPlatformIcon platform="python.gnu_backtrace" label="GNU Backtrace" url="/platforms/python/integrations/gnu_backtrace" /> | |
| <LinkWithPlatformIcon platform="python.socket" label="Socket" url="/platforms/python/integrations/socket" /> | |
| <LinkWithPlatformIcon platform="python.sys_exit" label="sys.exit" url="/platforms/python/integrations/sys_exit" /> | |
| <LinkWithPlatformIcon platform="python.tryton" label="Tryton" url="/platforms/python/integrations/tryton" /> | |
| <LinkWithPlatformIcon platform="python.wsgi" label="WSGI" url="/platforms/python/integrations/wsgi" /> | |

Expand Down
73 changes: 73 additions & 0 deletions docs/platforms/python/integrations/sys_exit/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
title: sys.exit
description: 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`.

## Install

```bash
pip install --upgrade "sentry-sdk"
```

## Configure

The `SysExitIntegration` is disabled by default, and the SDK only captures `SystemExit` exceptions automatically if this integration is manually enabled.

szokeasaurusrex marked this conversation as resolved.
Show resolved Hide resolved
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:

<SignInNote />

```python
import sentry_sdk
from sentry_sdk.integrations.sys_exit import SysExitIntegration

sentry_sdk.init(
dsn="___PUBLIC_DSN___",
integrations=[SysExitIntegration()],
)
```

If you wish to capture successful and unsuccessful `sys.exit` calls, use the following, instead:

```python
import sentry_sdk
from sentry_sdk.integrations.sys_exit import SysExitIntegration

sentry_sdk.init(
dsn="___PUBLIC_DSN___",
integrations=[SysExitIntegration(capture_successful_exits=True)],
)
```



## Verify

Running the following snippet should result in a `SystemExit` exception being reported to Sentry.

```python
import sys

import sentry_sdk
from sentry_sdk.integrations.sys_exit import SysExitIntegration

sentry_sdk.init(
dsn="___PUBLIC_DSN___",
integrations=[SysExitIntegration()],
)

sys.exit(1)
```

If you use `capture_successful_exits=True`, calling `sys.exit(0)` should also report a `SystemExit` exception to Sentry.


<Alert level="info" title="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.

</Alert>
Loading