Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions docs/integrations/databases/sqlalchemy.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,37 @@ Install `logfire` with the `sqlalchemy` extra:

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

```py title="main.py"
import logfire
from sqlalchemy import create_engine
=== "Instrument a Single Engine"

logfire.configure()
```py title="main.py"
import logfire
from sqlalchemy import create_engine

engine = create_engine("sqlite:///:memory:")
logfire.instrument_sqlalchemy(engine=engine)
```
logfire.configure()

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

=== "Instrument Multiple Engines"

```py title="main.py"
import logfire
from sqlalchemy import create_engine

logfire.configure()

engine_one = create_engine("sqlite:///:memory:")
engine_two = create_engine("sqlite:///:memory:")
logfire.instrument_sqlalchemy(engines=[engine_one, engine_two])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this PR, but we also need to handle async engines in this list the way we do with 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][opentelemetry-sqlalchemy].

!!! warning
It's best to use the `engine` or `engines` arguments. If no engine is specified, then `instrument_sqlalchemy` may
only work if it's called before `sqlalchemy` is imported, in which case all engines are instrumented.

!!! tip
If you use [SQLModel][sqlmodel], you can use the same `SQLAlchemyInstrumentor` to instrument it.

Expand Down
Loading