2025-12-01
This commit is contained in:
@@ -3,7 +3,7 @@ import sys
|
||||
import sentry_sdk
|
||||
from sentry_sdk.consts import OP
|
||||
from sentry_sdk.integrations import Integration, DidNotEnable
|
||||
from sentry_sdk.utils import event_from_exception, reraise
|
||||
from sentry_sdk.utils import event_from_exception, logger, reraise
|
||||
|
||||
try:
|
||||
import asyncio
|
||||
@@ -11,7 +11,7 @@ try:
|
||||
except ImportError:
|
||||
raise DidNotEnable("asyncio not available")
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import cast, TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Any
|
||||
@@ -39,7 +39,7 @@ def patch_asyncio():
|
||||
def _sentry_task_factory(loop, coro, **kwargs):
|
||||
# type: (asyncio.AbstractEventLoop, Coroutine[Any, Any, Any], Any) -> asyncio.Future[Any]
|
||||
|
||||
async def _coro_creating_hub_and_span():
|
||||
async def _task_with_sentry_span_creation():
|
||||
# type: () -> Any
|
||||
result = None
|
||||
|
||||
@@ -51,32 +51,54 @@ def patch_asyncio():
|
||||
):
|
||||
try:
|
||||
result = await coro
|
||||
except StopAsyncIteration as e:
|
||||
raise e from None
|
||||
except Exception:
|
||||
reraise(*_capture_exception())
|
||||
|
||||
return result
|
||||
|
||||
task = None
|
||||
|
||||
# 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(), **kwargs)
|
||||
task = orig_task_factory(
|
||||
loop, _task_with_sentry_span_creation(), **kwargs
|
||||
)
|
||||
|
||||
# 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.
|
||||
if task is None:
|
||||
# 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, **kwargs)
|
||||
if task._source_traceback: # type: ignore
|
||||
del task._source_traceback[-1] # type: ignore
|
||||
# WARNING:
|
||||
# If the default behavior of the task creation in asyncio changes,
|
||||
# this will break!
|
||||
task = Task(_task_with_sentry_span_creation(), loop=loop, **kwargs)
|
||||
if task._source_traceback: # type: ignore
|
||||
del task._source_traceback[-1] # type: ignore
|
||||
|
||||
# Set the task name to include the original coroutine's name
|
||||
try:
|
||||
cast("asyncio.Task[Any]", task).set_name(
|
||||
f"{get_name(coro)} (Sentry-wrapped)"
|
||||
)
|
||||
except AttributeError:
|
||||
# set_name might not be available in all Python versions
|
||||
pass
|
||||
|
||||
return task
|
||||
|
||||
loop.set_task_factory(_sentry_task_factory) # type: ignore
|
||||
|
||||
except RuntimeError:
|
||||
# When there is no running loop, we have nothing to patch.
|
||||
pass
|
||||
logger.warning(
|
||||
"There is no running asyncio loop so there is nothing Sentry can patch. "
|
||||
"Please make sure you call sentry_sdk.init() within a running "
|
||||
"asyncio loop for the AsyncioIntegration to work. "
|
||||
"See https://docs.sentry.io/platforms/python/integrations/asyncio/"
|
||||
)
|
||||
|
||||
|
||||
def _capture_exception():
|
||||
|
||||
Reference in New Issue
Block a user