集成

Sentry 提供额外的集成,旨在更改配置或为你的应用程序添加 instrumentation。

Sentry SDK 使用集成来挂钩流行库的功能,以自动为你的应用程序添加 instrumentation,并让你开箱即用地获得最佳数据。

Auto-enabled
python.django iconDjango
python.flask iconFlask
python.fastapi iconFastAPI
python.aiohttp iconAIOHTTP
python.bottle iconBottle
python.falcon iconFalcon
python.pyramid iconPyramid
python.quart iconQuart
python.sanic iconSanic
python.starlette iconStarlette
python.starlite iconStarlite
python.litestar iconLitestar
python.tornado iconTornado

Auto-enabled
python.asyncpg iconasyncpg
python.clickhouse-driver iconClickHouse
python.pymongo iconMongoDB
python.redis iconRedis
python.sqlalchemy iconSQLAlchemy

Auto-enabled
anthropic iconAnthropic
huggingface iconHuggingface Hub
langchain iconLangchain
openai iconOpenAI

Auto-enabled
python.airflow iconApache Airflow
python.beam iconApache Beam
python.spark iconApache Spark
python.arq iconARQ
python.celery iconCelery
python.dramatiq iconDramatiq
python.huey iconhuey
python.rq iconRQ
python.ray iconRay

Auto-enabled
python iconLaunchDarkly
python iconOpenFeature
python iconUnleash

Auto-enabled
python.awslambda iconAWS Lambda
python.boto3 iconBoto3
python.chalice iconChalice
python.cloudresourcecontext iconCloud Resource Context
python.gcpfunctions iconGoogle Cloud Functions
python.serverless iconServerless Framework

Auto-enabled
python.aiohttp iconAIOHTTP
python.httpx iconHTTPX
Python standard HTTP client (in the Default Integrations)
Requests HTTP instrumentation is done via the Default Integrations.

Auto-enabled
python.ariadne iconAriadne
python.gql iconGQL
python.graphene iconGraphene
python.strawberry iconStrawberry

Auto-enabled
python.grpc icongRPC

Auto-enabled
python.logging iconLogging
python.loguru iconLoguru

Auto-enabled
python.asgi iconASGI
python.asyncio iconasyncio
python.pure_eval iconEnhanced Locals
python.gnu_backtrace iconGNU Backtrace
python.rust_tracing iconRust Tracing
python.socket iconSocket
python.sys_exit iconsys.exit
python.tryton iconTryton
python.typer iconTyper
python.wsgi iconWSGI

Integration
Argv
Atexit
Excepthook
Deduplication
Stdlib
Modules
Logging
Threading

可以通过 integrations 配置选项添加集成。

在上表中标记为“自动启用”的集成将自动开启,除非你将 auto_enabling_integrations 设置为 False。如果你想配置特定集成的设置(例如,更改 Flask 的默认 transaction_style),请将其添加到你的 integrations 列表中,就像添加非自动启用的集成一样,并传递所需的选项。

Copied
import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from sentry_sdk.integrations.flask import FlaskIntegration

sentry_sdk.init(
    integrations=[
        # The Flask integration is auto-enabling, but we want to change
        # transaction_style from the default "endpoint" to "url"
        FlaskIntegration(transaction_style="url"),
        # The asyncio integration is not enabled automatically
        # and needs to be added manually.
        AsyncioIntegration(),
    ],
)

要禁用集成,请使用 disabled_integrations 配置选项:

Copied
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

sentry_sdk.init(
    # Do not use the Flask integration even if Flask is installed.
    disabled_integrations=[
        FlaskIntegration(),
    ],
)

也可以禁用所有自动添加的集成。有两种类型:

  • 自动启用的集成,如 FlaskIntegration,如果 SDK 检测到你安装了相应的包(如 Flask),则会自动添加。这发生在 auto_enabling_integrations 选项设置为 True(默认值)时。
  • 默认集成,如 loggingexcepthook,只要 default_integrations 选项为 True(默认值),无论你安装了什么包,它们始终启用。它们提供诸如错误去重或解释器关闭时的事件刷新等基本 SDK 功能。

要禁用所有自动启用的集成,你可以使用 auto_enabling_integrations 选项:

Copied
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

sentry_sdk.init(
    # Turn off all auto-enabling integrations except for Flask
    auto_enabling_integrations=False,
    integrations=[
        FlaskIntegration(),
    ],
)

要禁用所有 默认集成,将 default_integrations 设置为 False。请注意,这会禁用所有自动添加的集成,包括默认和自动启用的集成。你想要使用的任何集成都需要通过 integrations 配置选项手动指定。

Copied
import sentry_sdk
from sentry_sdk.integrations.atexit import AtexitIntegration
from sentry_sdk.integrations.argv import ArgvIntegration
from sentry_sdk.integrations.dedupe import DedupeIntegration
from sentry_sdk.integrations.excepthook import ExcepthookIntegration
from sentry_sdk.integrations.stdlib import StdlibIntegration
from sentry_sdk.integrations.modules import ModulesIntegration
from sentry_sdk.integrations.threading import ThreadingIntegration

sentry_sdk.init(
    # Turn off the default logging integration, but keep the rest.
    default_integrations=False,
    integrations=[
        AtexitIntegration(),
        ArgvIntegration(),
        DedupeIntegration(),
        ExcepthookIntegration(),
        StdlibIntegration(),
        ModulesIntegration(),
        ThreadingIntegration(),
    ],
)