AIOHTTP 客户端

了解 AIOHTTP 集成以及它如何为 AIOHTTP HTTP 客户端添加支持。

AIOHTTP 集成为使用 AIOHTTP 客户端的外发 HTTP 请求添加 instrumentation。

使用此集成可以为外发请求创建跨度,并确保跟踪信息正确传播到下游服务。

此集成还支持 AIOHTTP 服务器。详情请参阅 AIOHTTP 服务器文档

从 PyPI 安装带有 aiohttp 额外依赖的 sentry-sdk

Copied
pip install --upgrade 'sentry-sdk[aiohttp]'

如果你的依赖中包含 aiohttp 包,在初始化 Sentry SDK 时 AIOHTTP 集成将自动启用。

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=1.0,
)

Copied
import asyncio
import aiohttp

async def main():
    sentry_sdk.init(...)  # same as above

    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())

这将在 sentry.io 的性能部分创建一个名为 testing_sentry 的事务,并为外发 HTTP 请求创建跨度。

数据出现在 sentry.io 上需要几秒钟的时间。

  • AIOHTTP: 3.5+
  • Python: 3.7+