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

Asyncio fixes #1689

Merged
merged 28 commits into from Oct 20, 2022
Merged
Changes from 27 commits
Commits
Show all changes
28 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
cafb0f0
Merge branch 'master' into antonpirker/asyncio-integration
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
53 changes: 27 additions & 26 deletions sentry_sdk/integrations/asyncio.py
Expand Up @@ -16,39 +16,40 @@
from typing import Any


def _sentry_task_factory(loop, coro):
# type: (Any, Any) -> Task[None]
def patch_asyncio():
# type: () -> None
orig_task_factory = None
try:
loop = asyncio.get_running_loop()
orig_task_factory = loop.get_task_factory()

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
def _sentry_task_factory(loop, coro):
# type: (Any, Any) -> Any

# 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)
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

# 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.
# 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

# 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
# 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.

return task
# 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

def patch_asyncio():
# type: () -> None
try:
loop = asyncio.get_running_loop()
loop.set_task_factory(_sentry_task_factory)
except RuntimeError:
# When there is no running loop, we have nothing to patch.
Expand Down