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:
pip install 'logfire[sqlalchemy]'
uv add 'logfire[sqlalchemy]'
rye add logfire -E sqlalchemy
poetry add 'logfire[sqlalchemy]'
Usage¶
Let's see a minimal example below. You can run it with python main.py
:
main.py
import logfire
from sqlalchemy import create_engine
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.
Tip
If you use SQLModel, you can use the same SQLAlchemyInstrumentor
to instrument it.