Skip to content

SQLAlchemy

The logfire.instrument_sqlalchemy() method will create a span for every query executed by a SQLAlchemy engine.

Installation

Install logfire with the sqlalchemy extra:

Terminal
pip install 'logfire[sqlalchemy]'

Usage

Let’s see a minimal example below. You can run it with python main.py:

main.py
from sqlalchemy import create_engine

import logfire

logfire.configure()

engine = create_engine('sqlite:///:memory:')
logfire.instrument_sqlalchemy(engine=engine)

The keyword arguments of logfire.instrument_sqlalchemy() are passed to the SQLAlchemyInstrumentor().instrument() method of the OpenTelemetry SQLAlchemy Instrumentation package, read more about it here.