Title: Asyncio integration only instruments one Asyncio event loop · Issue #2333 · getsentry/sentry-python · GitHub
Open Graph Title: Asyncio integration only instruments one Asyncio event loop · Issue #2333 · getsentry/sentry-python
X Title: Asyncio integration only instruments one Asyncio event loop · Issue #2333 · getsentry/sentry-python
Description: How do you use Sentry? Sentry Saas (sentry.io) Version 1.30.0 Steps to Reproduce To observe this error, we can add a print statement within the inner _coro_creating_hub_and_span async function (defined within _sentry_task_factory which i...
Open Graph Description: How do you use Sentry? Sentry Saas (sentry.io) Version 1.30.0 Steps to Reproduce To observe this error, we can add a print statement within the inner _coro_creating_hub_and_span async function (def...
X Description: How do you use Sentry? Sentry Saas (sentry.io) Version 1.30.0 Steps to Reproduce To observe this error, we can add a print statement within the inner _coro_creating_hub_and_span async function (def...
Opengraph URL: https://github.com/getsentry/sentry-python/issues/2333
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Asyncio integration only instruments one Asyncio event loop","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 this error, we can add a `print` statement within the inner `_coro_creating_hub_and_span` async function (defined within `_sentry_task_factory` which is itself defined within `patch_asyncio`) in the file `sentry_sdk/integrations/asyncio.py`. This inner function is supposed to wrap every `async` call when the AsyncioIntegration is enabled, and so adding the print statement allows us to verify that asyncio has been correctly instrumented.\r\n\r\nNext, we run the following sample script:\r\n```python\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 sentry_sdk.init(\r\n dsn=\"\u003cdsn here>\",\r\n traces_sample_rate=1.0,\r\n integrations=(AsyncioIntegration(),),\r\n )\r\n\r\n await runner() # This call is instrumented\r\n\r\n\r\nasync def runner():\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 print(\"initial call\")\r\n asyncio.run(main())\r\n print(\"initial call has ended\\n\")\r\n\r\n print(\"second call\")\r\n asyncio.run(runner()) # This call is NOT instrumented\r\n print(\"second call has ended\\n\")\r\n```\r\n\r\n### Expected Result\r\n\r\nBoth calls to `runner` should be auto-instrumented by the SDK's asyncio integration.\r\n\r\n### Actual Result\r\n\r\nThe SDK only auto-instruments the first call to `runner`, which is made within the `main` function. The SDK fails to instrument the second call to `runner`, which is made outside the `main` function. \r\n\r\n### Likely Cause\r\n\r\nThis issue likely has the same cause as #2328; that is to say, this issue appears to be caused by the Sentry SDK only instrumenting the currently running asyncio event loop. `asyncio.run()` [always creates a new event loop](https://docs.python.org/3/library/asyncio-runner.html#asyncio.run) every time it is called, which is why the second call to runner fails.\r\n\r\n### Potential Solutions\r\n\r\n1. **Don't fix, and close the issue**\r\n Calling `asyncio.run()` multiple times, while technically possible, is likely not a common use case, and the Python docs recommend avoiding multiple calls to the function from the same thread. From the [docs](https://docs.python.org/3/library/asyncio-runner.html#asyncio.run), \"[`asyncio.run()`] should be used as a main entry point for asyncio programs, and should ideally only be called once.\" If we go this route, perhaps we should document only supporting a single `asyncio.run()` call as a known SDK limitation. \r\n2. **Modify the SDK to instrument newly created event loops**\r\n This solution would also likely solve the related issue #2328. And, even if calling `asyncio.run()` multiple times is not a common or even a recommended design pattern, it is possible to do and so we should support it. \r\n\r\nIf we go with second option, 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. ","author":{"url":"https://github.com/szokeasaurusrex","@type":"Person","name":"szokeasaurusrex"},"datePublished":"2023-08-31T12:33:53Z","url":"https://github.com/getsentry/sentry-python/issues/2333"}
| 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:6b3a8f9b-5fdb-b2bf-057f-8cd09ee6828b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BF82:3C21B:491D75:603416:6B00C994 |
| html-safe-nonce | a381e9ffe9ec6a74db17976fa9390314a95a2278448898cca660171ff3d16e98 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRjgyOjNDMjFCOjQ5MUQ3NTo2MDM0MTY6NkIwMEM5OTQiLCJ2aXNpdG9yX2lkIjoiNTQ4OTkzODMxMDM0NjM2MTIzNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0e262c2372558647a74a88636dd3d882a505bd7dd851de261e85347a6421afda |
| hovercard-subject-tag | issue:1875438594 |
| 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/2333/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1497091967d4acdf6938d643c5cecf95e604ac16c8e903d80f63872a65d90a03/getsentry/sentry-python/issues/2333 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1497091967d4acdf6938d643c5cecf95e604ac16c8e903d80f63872a65d90a03/getsentry/sentry-python/issues/2333 |
| og:image:alt | How do you use Sentry? Sentry Saas (sentry.io) Version 1.30.0 Steps to Reproduce To observe this error, we can add a print statement within the inner _coro_creating_hub_and_span async function (def... |
| 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 | 98dc00e171c204462a6f367482a0f8c3c6b5dbca99cd7390e82d2df49ce096e4 |
| 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 | ef013399cf0a3c324bea644a685d7c1e0f06b5b7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width