AIOHTTP Client for Python
Learn about the AIOHTTP integration and how it adds support for the AIOHTTP HTTP client.
The AIOHTTP integration instruments outgoing HTTP requests using the AIOHTTP client.
Use this integration to create spans for outgoing requests and ensure traces are properly propagated to downstream services.
This integration also supports AIOHTTP servers. See AIOHTTP server documentation for details.
If you're using stream mode, this page's references to "transaction" should be applied to service spans instead. See Streamed Spans for more information.
Install sentry-sdk from PyPI:
pip install sentry-sdk
pip install sentry-sdk
uv add sentry-sdk
If you have the aiohttp package in your dependencies, the AIOHTTP integration will be enabled automatically when you initialize the Sentry SDK.
import sentry_sdk
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"user_info": True,
},
# ___PRODUCT_OPTION_START___ performance
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# ___PRODUCT_OPTION_END___ performance
# ___PRODUCT_OPTION_START___ profiling
# To collect profiles for all profile sessions,
# set `profile_session_sample_rate` to 1.0.
profile_session_sample_rate=1.0,
# Profiles will be automatically collected while
# there is an active span.
profile_lifecycle="trace",
# ___PRODUCT_OPTION_END___ profiling
)
import sentry_sdk
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"user_info": True,
},
# ___PRODUCT_OPTION_START___ performance
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# ___PRODUCT_OPTION_END___ performance
# ___PRODUCT_OPTION_START___ profiling
# To collect profiles for all profile sessions,
# set `profile_session_sample_rate` to 1.0.
profile_session_sample_rate=1.0,
# Profiles will be automatically collected while
# there is an active span.
profile_lifecycle="trace",
# ___PRODUCT_OPTION_END___ profiling
)
import asyncio
import aiohttp
async def main():
sentry_sdk.init(...) # same as above
# or sentry_sdk.traces.start_span(name="your_span_name", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
async with aiohttp.ClientSession() as session:
async with session.get("https://sentry.io/") as response:
print("Status:", response.status)
async with session.post("http://httpbin.org/post") as response:
print("Status:", response.status)
asyncio.run(main())
import asyncio
import aiohttp
async def main():
sentry_sdk.init(...) # same as above
# or sentry_sdk.traces.start_span(name="your_span_name", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
async with aiohttp.ClientSession() as session:
async with session.get("https://sentry.io/") as response:
print("Status:", response.status)
async with session.post("http://httpbin.org/post") as response:
print("Status:", response.status)
asyncio.run(main())
This will create a transaction called testing_sentry in the Performance section of sentry.io and will create spans for the outgoing HTTP requests.
It takes a couple of moments for the data to appear in sentry.io.
- AIOHTTP:
- 3.4+ (SDK <3.0)
- 3.7+ (SDK 3.0+)
- Python: 3.7+
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").