asyncio.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import sys
  2. import functools
  3. import sentry_sdk
  4. from sentry_sdk.consts import OP
  5. from sentry_sdk.integrations import Integration, DidNotEnable
  6. from sentry_sdk.utils import event_from_exception, logger, reraise
  7. try:
  8. import asyncio
  9. from asyncio.tasks import Task
  10. except ImportError:
  11. raise DidNotEnable("asyncio not available")
  12. from typing import cast, TYPE_CHECKING
  13. if TYPE_CHECKING:
  14. from typing import Any, Callable, TypeVar
  15. from collections.abc import Coroutine
  16. from sentry_sdk._types import ExcInfo
  17. T = TypeVar("T", bound=Callable[..., Any])
  18. def get_name(coro: "Any") -> str:
  19. return (
  20. getattr(coro, "__qualname__", None)
  21. or getattr(coro, "__name__", None)
  22. or "coroutine without __name__"
  23. )
  24. def _wrap_coroutine(wrapped: "Coroutine[Any, Any, Any]") -> "Callable[[T], T]":
  25. # Only __name__ and __qualname__ are copied from function to coroutine in CPython
  26. return functools.partial(
  27. functools.update_wrapper,
  28. wrapped=wrapped, # type: ignore
  29. assigned=("__name__", "__qualname__"),
  30. updated=(),
  31. )
  32. def patch_asyncio() -> None:
  33. orig_task_factory = None
  34. try:
  35. loop = asyncio.get_running_loop()
  36. orig_task_factory = loop.get_task_factory()
  37. # Check if already patched
  38. if getattr(orig_task_factory, "_is_sentry_task_factory", False):
  39. return
  40. def _sentry_task_factory(
  41. loop: "asyncio.AbstractEventLoop",
  42. coro: "Coroutine[Any, Any, Any]",
  43. **kwargs: "Any",
  44. ) -> "asyncio.Future[Any]":
  45. @_wrap_coroutine(coro)
  46. async def _task_with_sentry_span_creation() -> "Any":
  47. result = None
  48. with sentry_sdk.isolation_scope():
  49. with sentry_sdk.start_span(
  50. op=OP.FUNCTION,
  51. name=get_name(coro),
  52. origin=AsyncioIntegration.origin,
  53. ):
  54. try:
  55. result = await coro
  56. except StopAsyncIteration as e:
  57. raise e from None
  58. except Exception:
  59. reraise(*_capture_exception())
  60. return result
  61. task = None
  62. # Trying to use user set task factory (if there is one)
  63. if orig_task_factory:
  64. task = orig_task_factory(
  65. loop, _task_with_sentry_span_creation(), **kwargs
  66. )
  67. if task is None:
  68. # The default task factory in `asyncio` does not have its own function
  69. # but is just a couple of lines in `asyncio.base_events.create_task()`
  70. # Those lines are copied here.
  71. # WARNING:
  72. # If the default behavior of the task creation in asyncio changes,
  73. # this will break!
  74. task = Task(_task_with_sentry_span_creation(), loop=loop, **kwargs)
  75. if task._source_traceback: # type: ignore
  76. del task._source_traceback[-1] # type: ignore
  77. # Set the task name to include the original coroutine's name
  78. try:
  79. cast("asyncio.Task[Any]", task).set_name(
  80. f"{get_name(coro)} (Sentry-wrapped)"
  81. )
  82. except AttributeError:
  83. # set_name might not be available in all Python versions
  84. pass
  85. return task
  86. _sentry_task_factory._is_sentry_task_factory = True # type: ignore
  87. loop.set_task_factory(_sentry_task_factory) # type: ignore
  88. except RuntimeError:
  89. # When there is no running loop, we have nothing to patch.
  90. logger.warning(
  91. "There is no running asyncio loop so there is nothing Sentry can patch. "
  92. "Please make sure you call sentry_sdk.init() within a running "
  93. "asyncio loop for the AsyncioIntegration to work. "
  94. "See https://docs.sentry.io/platforms/python/integrations/asyncio/"
  95. )
  96. def _capture_exception() -> "ExcInfo":
  97. exc_info = sys.exc_info()
  98. client = sentry_sdk.get_client()
  99. integration = client.get_integration(AsyncioIntegration)
  100. if integration is not None:
  101. event, hint = event_from_exception(
  102. exc_info,
  103. client_options=client.options,
  104. mechanism={"type": "asyncio", "handled": False},
  105. )
  106. sentry_sdk.capture_event(event, hint=hint)
  107. return exc_info
  108. class AsyncioIntegration(Integration):
  109. identifier = "asyncio"
  110. origin = f"auto.function.{identifier}"
  111. @staticmethod
  112. def setup_once() -> None:
  113. patch_asyncio()
  114. def enable_asyncio_integration(*args: "Any", **kwargs: "Any") -> None:
  115. """
  116. Enable AsyncioIntegration with the provided options.
  117. This is useful in scenarios where Sentry needs to be initialized before
  118. an event loop is set up, but you still want to instrument asyncio once there
  119. is an event loop. In that case, you can sentry_sdk.init() early on without
  120. the AsyncioIntegration and then, once the event loop has been set up,
  121. execute:
  122. ```python
  123. from sentry_sdk.integrations.asyncio import enable_asyncio_integration
  124. async def async_entrypoint():
  125. enable_asyncio_integration()
  126. ```
  127. Any arguments provided will be passed to AsyncioIntegration() as is.
  128. If AsyncioIntegration has already patched the current event loop, this
  129. function won't have any effect.
  130. If AsyncioIntegration was provided in
  131. sentry_sdk.init(disabled_integrations=[...]), this function will ignore that
  132. and the integration will be enabled.
  133. """
  134. client = sentry_sdk.get_client()
  135. if not client.is_active():
  136. return
  137. # This function purposefully bypasses the integration machinery in
  138. # integrations/__init__.py. _installed_integrations/_processed_integrations
  139. # is used to prevent double patching the same module, but in the case of
  140. # the AsyncioIntegration, we don't monkeypatch the standard library directly,
  141. # we patch the currently running event loop, and we keep the record of doing
  142. # that on the loop itself.
  143. logger.debug("Setting up integration asyncio")
  144. integration = AsyncioIntegration(*args, **kwargs)
  145. integration.setup_once()
  146. if "asyncio" not in client.integrations:
  147. client.integrations["asyncio"] = integration