Skip to content

Commit 57aa3b2

Browse files
authored
Document SQLAlchemy's Engine Kwargs' Behaviour (#1402)
1 parent 0c3fc81 commit 57aa3b2

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

docs/integrations/databases/sqlalchemy.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,37 @@ Install `logfire` with the `sqlalchemy` extra:
1414

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

17-
```py title="main.py"
18-
import logfire
19-
from sqlalchemy import create_engine
17+
=== "Instrument a Single Engine"
2018

21-
logfire.configure()
19+
```py title="main.py"
20+
import logfire
21+
from sqlalchemy import create_engine
2222

23-
engine = create_engine("sqlite:///:memory:")
24-
logfire.instrument_sqlalchemy(engine=engine)
25-
```
23+
logfire.configure()
24+
25+
engine = create_engine("sqlite:///:memory:")
26+
logfire.instrument_sqlalchemy(engine=engine)
27+
```
28+
29+
=== "Instrument Multiple Engines"
30+
31+
```py title="main.py"
32+
import logfire
33+
from sqlalchemy import create_engine
34+
35+
logfire.configure()
36+
37+
engine_one = create_engine("sqlite:///:memory:")
38+
engine_two = create_engine("sqlite:///:memory:")
39+
logfire.instrument_sqlalchemy(engines=[engine_one, engine_two])
40+
```
2641

2742
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].
2843

44+
!!! warning
45+
It's best to use the `engine` or `engines` arguments. If no engine is specified, then `instrument_sqlalchemy` may
46+
only work if it's called before `sqlalchemy` is imported, in which case all engines are instrumented.
47+
2948
!!! tip
3049
If you use [SQLModel][sqlmodel], you can use the same `SQLAlchemyInstrumentor` to instrument it.
3150

0 commit comments

Comments
 (0)