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.
pip install --upgrade 'sentry-sdk[sqlalchemy]'
The SQLAlchemy integration is enabled automatically if you have the sqlalchemy
package installed.
Configuration should happen as early as possible in your application's lifecycle.
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,
)
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.
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").