Skip to content

Commit

Permalink
🐛 Calculate query count using session events
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Dec 15, 2023
1 parent 26e3e27 commit 0ab0f97
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions backend/transcribee_backend/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ def SessionContextManager(path: str):

@contextmanager
def query_counter(session: Session, path: Optional[str]):
engine = session.connection().engine
count = 0

def callback(*args, **kwargs):
nonlocal count
count += 1

event.listen(engine, "before_cursor_execute", callback)
event.listen(session, "do_orm_execute", callback)
yield
event.remove(engine, "before_cursor_execute", callback)
event.remove(session, "do_orm_execute", callback)
query_histogram.labels(path=path).observe(count)

0 comments on commit 0ab0f97

Please sign in to comment.