SQLAlchemy

Learn about importing the SQLAlchemy integration and how it captures queries from SQLAlchemy as breadcrumbs.

The SQLAlchemy integration captures queries from SQLAlchemy as breadcrumbs and spans.

Install sentry-sdk from PyPI with the sqlalchemy extra.

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

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    enable_tracing=True,
)

The SQLAlchemy integration is enabled automatically if you have the sqlalchemy package installed.

Copied
from sqlalchemy import create_engine
from sqlalchemy.sql import text

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

    engine = create_engine(DATABASE_URL, echo=True)
    statement = text("SELECT 'Hello World'")

    with engine.connect() as conn:
        with sentry_sdk.start_transaction(name="testing_sentry"):
            result = conn.execute(statement)

main()

This will create a transaction called testing_sentry in the Performance section of sentry.io and will create a span for the SELECT statement.

It takes a couple of moments for the data to appear in sentry.io.

  • SQLAlchemy: 1.2+
  • Python: 3.6+

The versions above apply for Sentry Python SDK version 2.0+, which drops support for some legacy Python and framework versions. If you're looking to use Sentry with older Python or framework versions, consider using an SDK version from the 1.x major line of releases.

Help improve this content
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").