Skip to content

Commit 9f98b14

Browse files
feat(python): Add docs on enable_asyncio_integration() (#15988)
Added new API in getsentry/sentry-python#5288. **Preview:** https://sentry-docs-git-ivana-add-asyncio-post-patch.sentry.dev/platforms/python/integrations/asyncio/ ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [x] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [x] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [x] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## LEGAL BOILERPLATE <!-- Sentry employees and contractors can delete or ignore this section. --> Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/) --------- Co-authored-by: Alex Krawiec <alex.krawiec@sentry.io>
1 parent 184d3cd commit 9f98b14

File tree

1 file changed

+48
-2
lines changed
  • docs/platforms/python/integrations/asyncio

1 file changed

+48
-2
lines changed

docs/platforms/python/integrations/asyncio/index.mdx

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ uv add "sentry-sdk"
1616

1717
## Configure
1818

19+
`AsyncioIntegration` works by instrumenting the current event loop, which is why it needs to be enabled when there's an event loop running.
20+
21+
### Enable `Asynciointegration` At `Init` Time
22+
1923
Add `AsyncioIntegration()` to your list of `integrations`, enable tracing and be sure to call `sentry_sdk.init()` **at the beginning of the first `async` function you call**, as shown in the example below.
2024

2125
<OnboardingOptionButtons
@@ -60,6 +64,43 @@ async def main():
6064
asyncio.run(main())
6165
```
6266

67+
### Enable `AsyncioIntegration` After `init`
68+
69+
Alternatively, if you're not in control of the event loop, you have multiple event loops, or you simply need a way to enable the integration on demand without setting up the whole SDK each time, you can use the `sentry_sdk.integrations.asyncio.enable_asyncio_integration` helper function.
70+
71+
```python {filename:main.py}
72+
import sentry_sdk
73+
from sentry_sdk.integrations.asyncio import enable_asyncio_integration
74+
75+
# Initializing the SDK as early as possible, when there is no event loop yet
76+
sentry_sdk.init(
77+
dsn="___PUBLIC_DSN___",
78+
# Add data like request headers and IP for users, if applicable;
79+
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
80+
send_default_pii=True,
81+
# ___PRODUCT_OPTION_START___ performance
82+
# Set traces_sample_rate to 1.0 to capture 100%
83+
# of transactions for tracing.
84+
traces_sample_rate=1.0,
85+
# ___PRODUCT_OPTION_END___ performance
86+
# ___PRODUCT_OPTION_START___ profiling
87+
# To collect profiles for all profile sessions,
88+
# set `profile_session_sample_rate` to 1.0.
89+
profile_session_sample_rate=1.0,
90+
# Profiles will be automatically collected while
91+
# there is an active span.
92+
profile_lifecycle="trace",
93+
# ___PRODUCT_OPTION_END___ profiling
94+
# No AsyncioIntegration in explicitly provided `integrations`
95+
)
96+
97+
98+
async def main():
99+
enable_asyncio_integration() # instruments the current event loop
100+
# Your code...
101+
```
102+
103+
63104
## Verify
64105

65106
Trigger an error in your code and see it show up in [sentry.io](https://sentry.io).
@@ -68,7 +109,6 @@ Trigger an error in your code and see it show up in [sentry.io](https://sentry.i
68109
import asyncio
69110

70111
import sentry_sdk
71-
from sentry_sdk.integrations.asyncio import AsyncioIntegration
72112

73113
async def my_task():
74114
1 / 0 # raises an error!
@@ -88,12 +128,18 @@ asyncio.run(main())
88128

89129
## Troubleshooting
90130

91-
<Expandable title="Asyncio integration not working">
131+
<Expandable title="The asyncio integration isn't working.">
92132

93133
The SDK needs the event loop to be running when instrumenting asyncio. Make sure to call `sentry_sdk.init()` inside of an `async` function.
94134

95135
</Expandable>
96136

137+
<Expandable title="There is no event loop when I need to initialize the SDK. How do I initialize the asyncio integration?">
138+
139+
You can initialize the SDK without `AsyncioIntegration` and then run `sentry_sdk.integrations.asyncio.enable_asyncio_integration()` once there is an event loop running. See [this section](#enable-asynciointegration-after-init) for an example.
140+
141+
</Expandable>
142+
97143
## Supported Versions
98144

99145
- Python: 3.7+

0 commit comments

Comments
 (0)