Skip to content

Commit 1e771f3

Browse files
committed
query recording: also record bind_key of query
1 parent 168cb4b commit 1e771f3

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

docs/record-queries.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ object has the following attributes:
2525
``location``
2626
A string description of where in your application code the query was executed. This
2727
may be unknown in certain cases.
28+
``bind_key``
29+
The bind key of the engine which issued the query.

src/flask_sqlalchemy/extension.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ def init_app(self, app: Flask) -> None:
378378
if app.config.setdefault("SQLALCHEMY_RECORD_QUERIES", False):
379379
from . import record_queries
380380

381-
for engine in engines.values():
382-
record_queries._listen(engine)
381+
for bind_key, engine in engines.items():
382+
record_queries._listen(bind_key, engine)
383383

384384
if app.config.setdefault("SQLALCHEMY_TRACK_MODIFICATIONS", False):
385385
from . import track_modifications

src/flask_sqlalchemy/record_queries.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dataclasses
44
import inspect
55
import typing as t
6+
from functools import partial
67
from time import perf_counter
78

89
import sqlalchemy as sa
@@ -65,15 +66,21 @@ class _QueryInfo:
6566
start_time: float
6667
end_time: float
6768
location: str
69+
bind_key: str | None
6870

6971
@property
7072
def duration(self) -> float:
7173
return self.end_time - self.start_time
7274

7375

74-
def _listen(engine: sa.engine.Engine) -> None:
76+
def _listen(bind_key: str | None, engine: sa.engine.Engine) -> None:
7577
sa_event.listen(engine, "before_cursor_execute", _record_start, named=True)
76-
sa_event.listen(engine, "after_cursor_execute", _record_end, named=True)
78+
sa_event.listen(
79+
engine,
80+
"after_cursor_execute",
81+
partial(_record_end, bind_key),
82+
named=True,
83+
)
7784

7885

7986
def _record_start(context: sa.engine.ExecutionContext, **kwargs: t.Any) -> None:
@@ -83,7 +90,11 @@ def _record_start(context: sa.engine.ExecutionContext, **kwargs: t.Any) -> None:
8390
context._fsa_start_time = perf_counter() # type: ignore[attr-defined]
8491

8592

86-
def _record_end(context: sa.engine.ExecutionContext, **kwargs: t.Any) -> None:
93+
def _record_end(
94+
bind_key: str | None,
95+
context: sa.engine.ExecutionContext,
96+
**kwargs: t.Any,
97+
) -> None:
8798
if not has_app_context():
8899
return
89100

@@ -113,5 +124,6 @@ def _record_end(context: sa.engine.ExecutionContext, **kwargs: t.Any) -> None:
113124
start_time=context._fsa_start_time, # type: ignore[attr-defined]
114125
end_time=perf_counter(),
115126
location=location,
127+
bind_key=bind_key,
116128
)
117129
)

tests/test_record_queries.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ class Todo(db.Model): # type: ignore[no-redef]
4949
assert info.duration == info.end_time - info.start_time
5050
assert os.path.join("tests", "test_record_queries.py:") in info.location
5151
assert "(test_query_info)" in info.location
52+
assert info.bind_key is None

0 commit comments

Comments
 (0)