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

Add exception handling to Asyncio Integration #1695

Merged
merged 32 commits into from
Oct 20, 2022
Merged
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a93565b
First version of asyncio integration
antonpirker Oct 10, 2022
18a02c5
Create a hub and span for each corotine run.
antonpirker Oct 10, 2022
82f0329
Fixed some typing
antonpirker Oct 10, 2022
00678e0
Fixed some typing
antonpirker Oct 10, 2022
e570c85
Merge branch 'antonpirker/asyncio-integration' of github.com:getsentr…
antonpirker Oct 10, 2022
5ed18da
Fixed linting
antonpirker Oct 10, 2022
d5800d9
Merge branch 'master' into antonpirker/asyncio-integration
antonpirker Oct 10, 2022
192b578
Merge branch 'master' into antonpirker/asyncio-integration
antonpirker Oct 11, 2022
64d8e1a
Added tests
antonpirker Oct 12, 2022
8f74a50
Moved fixtured into test because it uses modules that are not always …
antonpirker Oct 12, 2022
3fb9d11
Using constant
antonpirker Oct 12, 2022
3597375
Using the 'function' op.
antonpirker Oct 14, 2022
402b192
Wrap import with try except
antonpirker Oct 14, 2022
d37cfb1
Make sure to use custom task factory if there is one
antonpirker Oct 14, 2022
9b7e111
Merge branch 'master' into antonpirker/asyncio-integration
antonpirker Oct 14, 2022
7c60637
Update sentry_sdk/integrations/asyncio.py
antonpirker Oct 14, 2022
4b4fbdb
Merge branch 'master' into antonpirker/asyncio-integration
antonpirker Oct 14, 2022
2de0b43
Error handling
antonpirker Oct 14, 2022
ff17782
Merge branch 'master' into antonpirker/asyncio-integration
antonpirker Oct 14, 2022
e0a51c0
Merge branch 'master' into antonpirker/asyncio-integration
antonpirker Oct 14, 2022
380ca42
Merge branch 'master' into antonpirker/asyncio-integration
antonpirker Oct 17, 2022
26f7c80
Merge branch 'master' into antonpirker/asyncio-integration
antonpirker Oct 17, 2022
e7e5ebb
Update sentry_sdk/integrations/asyncio.py
antonpirker Oct 17, 2022
01a9fe6
Ignoring typing
antonpirker Oct 17, 2022
0e1ca1a
Make sure we do not override a user set custom task factory.
antonpirker Oct 18, 2022
c22a934
Merge branch 'master' into antonpirker/asyncio-integration
antonpirker Oct 18, 2022
770957c
Fixed typing
antonpirker Oct 18, 2022
dc8497e
Added exception handling to asyncio integration
antonpirker Oct 20, 2022
7b33605
Merge branch 'master' into antonpirker/asyncio-exceptions
antonpirker Oct 20, 2022
8123926
Merge branch 'master' into antonpirker/asyncio-exceptions
antonpirker Oct 20, 2022
3a3b696
Added test for testing for exception
antonpirker Oct 20, 2022
8a2f1c4
Merge branch 'antonpirker/asyncio-exceptions' of github.com:getsentry…
antonpirker Oct 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 56 additions & 28 deletions sentry_sdk/integrations/asyncio.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import absolute_import
import sys

from sentry_sdk._compat import reraise
from sentry_sdk.consts import OP
from sentry_sdk.hub import Hub
from sentry_sdk.integrations import Integration, DidNotEnable
from sentry_sdk._types import MYPY
from sentry_sdk.utils import event_from_exception

try:
import asyncio
Expand All @@ -15,46 +18,71 @@
if MYPY:
from typing import Any


def _sentry_task_factory(loop, coro):
# type: (Any, Any) -> Task[None]

async def _coro_creating_hub_and_span():
# type: () -> None
hub = Hub(Hub.current)
with hub:
with hub.start_span(op=OP.FUNCTION, description=coro.__qualname__):
await coro

# Trying to use user set task factory (if there is one)
orig_factory = loop.get_task_factory()
if orig_factory:
return orig_factory(loop, _coro_creating_hub_and_span)

# The default task factory in `asyncio` does not have its own function
# but is just a couple of lines in `asyncio.base_events.create_task()`
# Those lines are copied here.

# WARNING:
# If the default behavior of the task creation in asyncio changes,
# this will break!
task = Task(_coro_creating_hub_and_span, loop=loop) # type: ignore
if task._source_traceback: # type: ignore
del task._source_traceback[-1] # type: ignore

return task
from sentry_sdk._types import ExcInfo


def patch_asyncio():
# type: () -> None
orig_task_factory = None
try:
loop = asyncio.get_running_loop()
orig_task_factory = loop.get_task_factory()

def _sentry_task_factory(loop, coro):
# type: (Any, Any) -> Any

async def _coro_creating_hub_and_span():
# type: () -> None
hub = Hub(Hub.current)
with hub:
with hub.start_span(op=OP.FUNCTION, description=coro.__qualname__):
try:
await coro
except Exception:
reraise(*_capture_exception(hub))

# Trying to use user set task factory (if there is one)
if orig_task_factory:
return orig_task_factory(loop, _coro_creating_hub_and_span()) # type: ignore

# The default task factory in `asyncio` does not have its own function
# but is just a couple of lines in `asyncio.base_events.create_task()`
# Those lines are copied here.

# WARNING:
# If the default behavior of the task creation in asyncio changes,
# this will break!
task = Task(_coro_creating_hub_and_span(), loop=loop)
if task._source_traceback: # type: ignore
del task._source_traceback[-1] # type: ignore

return task

loop.set_task_factory(_sentry_task_factory)
except RuntimeError:
# When there is no running loop, we have nothing to patch.
pass


def _capture_exception(hub):
# type: (Hub) -> ExcInfo
exc_info = sys.exc_info()

integration = hub.get_integration(AsyncioIntegration)
if integration is not None:
# If an integration is there, a client has to be there.
client = hub.client # type: Any

event, hint = event_from_exception(
exc_info,
client_options=client.options,
mechanism={"type": "asyncio", "handled": False},
)
hub.capture_event(event, hint=hint)

return exc_info


class AsyncioIntegration(Integration):
identifier = "asyncio"

Expand Down