Title: SDK not instrumenting Asyncio calls when `sentry_sdk.init` called at app lifecycle's beginning, outside an `async` function · Issue #2328 · getsentry/sentry-python · GitHub
Open Graph Title: SDK not instrumenting Asyncio calls when `sentry_sdk.init` called at app lifecycle's beginning, outside an `async` function · Issue #2328 · getsentry/sentry-python
X Title: SDK not instrumenting Asyncio calls when `sentry_sdk.init` called at app lifecycle's beginning, outside an `async` function · Issue #2328 · getsentry/sentry-python
Description: How do you use Sentry? Sentry Saas (sentry.io) Version 1.30.0 Steps to Reproduce To observe the issue, we can run the following code snippet: import asyncio import time import sentry_sdk from sentry_sdk.integrations.asyncio import Asynci...
Open Graph Description: How do you use Sentry? Sentry Saas (sentry.io) Version 1.30.0 Steps to Reproduce To observe the issue, we can run the following code snippet: import asyncio import time import sentry_sdk from sentr...
X Description: How do you use Sentry? Sentry Saas (sentry.io) Version 1.30.0 Steps to Reproduce To observe the issue, we can run the following code snippet: import asyncio import time import sentry_sdk from sentr...
Opengraph URL: https://github.com/getsentry/sentry-python/issues/2328
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"SDK not instrumenting Asyncio calls when `sentry_sdk.init` called at app lifecycle's beginning, outside an `async` function","articleBody":"### How do you use Sentry?\r\n\r\nSentry Saas (sentry.io)\r\n\r\n### Version\r\n\r\n1.30.0\r\n\r\n### Steps to Reproduce\r\n\r\nTo observe the issue, we can run the following code snippet:\r\n```python\r\n\r\nimport asyncio\r\nimport time\r\n\r\nimport sentry_sdk\r\nfrom sentry_sdk.integrations.asyncio import AsyncioIntegration\r\n\r\n\r\nsentry_sdk.init(\r\n dsn=\"[your dsn here]\",\r\n traces_sample_rate=1.0,\r\n integrations=(AsyncioIntegration(),),\r\n)\r\n\r\n\r\nasync def fib(n):\r\n if n == 0:\r\n return 0\r\n\r\n if n == 1:\r\n return 1\r\n\r\n time.sleep(0.05)\r\n\r\n async with asyncio.TaskGroup() as tg:\r\n fib1 = tg.create_task(fib(n - 1))\r\n fib2 = tg.create_task(fib(n - 2))\r\n\r\n return fib1.result() + fib2.result()\r\n\r\n\r\nasync def main():\r\n with sentry_sdk.start_transaction(op=\"test\", name=\"Fibonacci\"):\r\n result = await fib(5)\r\n\r\n print(result)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n asyncio.run(main())\r\n```\r\n\r\n### Expected Result\r\n\r\nWe would expect the SDK to record spans for all async calls. In particular, calls to the function `fib` should appear as spans in the performance transaction, so that we observe the following when viewing the performance transaction in Sentry:\r\n\r\n\u003cimg width=\"960\" alt=\"image\" src=\"https://github.com/getsentry/sentry-python/assets/7881302/270069a5-2744-4098-9c74-e47eab4a1123\">\r\n\r\n### Actual Result\r\n\r\nHowever, if we actually run the above code snippet and then view the generated performance transaction in Sentry, we will observe that the `fib` spans are **missing**! Instead, our transaction looks like the following:\r\n\r\n\u003cimg width=\"965\" alt=\"image\" src=\"https://github.com/getsentry/sentry-python/assets/7881302/810dbda6-a109-4173-964c-558228fb4017\">\r\n\r\nThe missing spans indicate that **the SDK is not instrumenting async task calls** as we would expect it to.\r\n\r\n### Workaround\r\n\r\nFor now, we can work around this bug by initializing the SDK within our first `async` function call, like so:\r\n```python\r\n\r\nimport asyncio\r\nimport time\r\n\r\nimport sentry_sdk\r\nfrom sentry_sdk.integrations.asyncio import AsyncioIntegration\r\n\r\n\r\nasync def fib(n):\r\n if n == 0:\r\n return 0\r\n\r\n if n == 1:\r\n return 1\r\n\r\n time.sleep(0.05)\r\n\r\n async with asyncio.TaskGroup() as tg:\r\n fib1 = tg.create_task(fib(n - 1))\r\n fib2 = tg.create_task(fib(n - 2))\r\n\r\n return fib1.result() + fib2.result()\r\n\r\n\r\nasync def main():\r\n # Note that the SDK init call has been moved here!\r\n sentry_sdk.init(\r\n dsn=\"[your dsn here]\",\r\n traces_sample_rate=1.0,\r\n integrations=(AsyncioIntegration(),),\r\n )\r\n with sentry_sdk.start_transaction(op=\"test\", name=\"Fibonacci\"):\r\n result = await fib(5)\r\n\r\n print(result)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n asyncio.run(main())\r\n```\r\n\r\nIf we run the workaround code, we observe the expected result. However, we should fix the bug so that we also **observe the expected result when we initialize the SDK outside an async function call**.\r\n\r\n### Possible fix\r\nThe issue appears to arise from within the `patch_asyncio` function, which is defined in `sentry_sdk/integrations/asyncio.py`, specifically by the line which obtains the loop like so:\r\n```python\r\nloop = asyncio.get_running_loop()\r\n```\r\n`asyncio.get_running_loop` can [only be called from within a coroutine or callback](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_running_loop). When we initialize the SDK outside an async function, we violate this precondition and a `RuntimeError` is raised. The SDK handles this error, but **the error prevents the SDK from auto-instrumenting calls to async functions**. \r\n\r\nReplacing the `asyncio.get_running_loop()` call with a call to [`asyncio.get_event_loop()`](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop) would likely fix this bug, but we need to ensure that `get_event_loop` does not cause any unintended side effects before implementing this change.\r\n\r\n**Alternatively**, this issue could likely be resolved by instrumenting asyncio through the use of a custom [event loop policy](https://docs.python.org/3/library/asyncio-policy.html#asyncio-policies) which instruments the SDK when a new event loop is created in addition to instrumenting the current event loop. **This solution may be preferable since it would likely also resolve #2333.**","author":{"url":"https://github.com/szokeasaurusrex","@type":"Person","name":"szokeasaurusrex"},"datePublished":"2023-08-29T14:52:54Z","url":"https://github.com/getsentry/sentry-python/issues/2328"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:87e49d64-a7bc-44fe-1d7a-0f6841ace9fb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA92:33D24F:6A198D:89FCF9:6B00E363 |
| html-safe-nonce | 2fd502ca5e77713686a24e1c0aadf3a16b0d1c45a23ebaf2189587143f040b02 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTkyOjMzRDI0Rjo2QTE5OEQ6ODlGQ0Y5OjZCMDBFMzYzIiwidmlzaXRvcl9pZCI6Ijg3MjQ0NzgxNTk0ODkxMzEzNjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 64e1a3dd529747ed41e8f3dc6e7e15303d0d82636f8c05a08d1efd14e0f96d9e |
| hovercard-subject-tag | issue:1871850249 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-python/2328/issue_layout |
| twitter:image | https://opengraph.githubassets.com/77848e6b400b97a4023c35bd4cff1c92e367cfb49c365cef213ee47fc8785b33/getsentry/sentry-python/issues/2328 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/77848e6b400b97a4023c35bd4cff1c92e367cfb49c365cef213ee47fc8785b33/getsentry/sentry-python/issues/2328 |
| og:image:alt | How do you use Sentry? Sentry Saas (sentry.io) Version 1.30.0 Steps to Reproduce To observe the issue, we can run the following code snippet: import asyncio import time import sentry_sdk from sentr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | szokeasaurusrex |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-python git https://github.com/getsentry/sentry-python.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 137814779 |
| octolytics-dimension-repository_nwo | getsentry/sentry-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 137814779 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width