-
Couldn't load subscription status.
- Fork 568
feat(integrations): Add unraisable exception integration #4733
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d706f7
feat(integrations): Add unraisable exception integration
alexander-alderman-webb 3756921
Rename test and unraisable exception integration identifier
alexander-alderman-webb f8661f7
Version gate unraisable exception integration
alexander-alderman-webb 56369a2
Version gate unraisable exception test as well
alexander-alderman-webb 5e8508f
Merge branch 'master' into webb/unraisable-integration
alexander-alderman-webb 1efdd0c
Remove automatic initialization of unraisable hook
alexander-alderman-webb 2f2fef5
Fix unraisablehook tests
alexander-alderman-webb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import sys | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.utils import ( | ||
| capture_internal_exceptions, | ||
| event_from_exception, | ||
| ) | ||
| from sentry_sdk.integrations import Integration | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Callable | ||
| from typing import Any | ||
|
|
||
|
|
||
| class UnraisablehookIntegration(Integration): | ||
| identifier = "unraisable" | ||
|
|
||
| @staticmethod | ||
| def setup_once(): | ||
| # type: () -> None | ||
| sys.unraisablehook = _make_unraisable(sys.unraisablehook) | ||
|
|
||
alexander-alderman-webb marked this conversation as resolved.
Show resolved
Hide resolved
alexander-alderman-webb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def _make_unraisable(old_unraisablehook): | ||
| # type: (Callable[[sys.UnraisableHookArgs], Any]) -> Callable[[sys.UnraisableHookArgs], Any] | ||
| def sentry_sdk_unraisablehook(unraisable): | ||
| # type: (sys.UnraisableHookArgs) -> None | ||
| integration = sentry_sdk.get_client().get_integration(UnraisablehookIntegration) | ||
|
|
||
| # Note: If we replace this with ensure_integration_enabled then | ||
| # we break the exceptiongroup backport; | ||
| # See: https://github.com/getsentry/sentry-python/issues/3097 | ||
| if integration is None: | ||
| return old_unraisablehook(unraisable) | ||
|
|
||
| if unraisable.exc_value and unraisable.exc_traceback: | ||
| with capture_internal_exceptions(): | ||
| event, hint = event_from_exception( | ||
| ( | ||
| unraisable.exc_type, | ||
| unraisable.exc_value, | ||
| unraisable.exc_traceback, | ||
| ), | ||
| client_options=sentry_sdk.get_client().options, | ||
| mechanism={"type": "unraisablehook", "handled": False}, | ||
| ) | ||
| sentry_sdk.capture_event(event, hint=hint) | ||
|
|
||
| return old_unraisablehook(unraisable) | ||
|
|
||
| return sentry_sdk_unraisablehook | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import pytest | ||
| import sys | ||
| import subprocess | ||
|
|
||
| from textwrap import dedent | ||
|
|
||
|
|
||
| TEST_PARAMETERS = [("", "HttpTransport")] | ||
|
|
||
| if sys.version_info >= (3, 8): | ||
| TEST_PARAMETERS.append(('_experiments={"transport_http2": True}', "Http2Transport")) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("options, transport", TEST_PARAMETERS) | ||
| def test_excepthook(tmpdir, options, transport): | ||
alexander-alderman-webb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| app = tmpdir.join("app.py") | ||
| app.write( | ||
| dedent( | ||
| """ | ||
| from sentry_sdk import init, transport | ||
| class Undeletable: | ||
| def __del__(self): | ||
| 1 / 0 | ||
| def capture_envelope(self, envelope): | ||
| print("capture_envelope was called") | ||
| event = envelope.get_event() | ||
| if event is not None: | ||
| print(event) | ||
| transport.{transport}.capture_envelope = capture_envelope | ||
| init("http://foobar@localhost/123", {options}) | ||
| undeletable = Undeletable() | ||
| del undeletable | ||
| """.format( | ||
| transport=transport, options=options | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| output = subprocess.check_output( | ||
| [sys.executable, str(app)], stderr=subprocess.STDOUT | ||
| ) | ||
|
|
||
| assert b"ZeroDivisionError" in output | ||
| assert b"capture_envelope was called" in output | ||
alexander-alderman-webb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.