Question

I searched the internet and didn't find an answer to a very simple question.

I have a simple web application (powered by web.py) which uses SQLAlchemy 0.7.8 + psycopg2, so sqltap doesn't work for me. So, I can enable echo=True in engine or do anything with current session which is saved in threadlocal.

What is the right approach to calculating the number of queries and their execution time? Using some kind of event and increment some global variable? I am almost sure SQLAlchemy can do it by itself, but not a single line about it found in docs.

Was it helpful?

Solution

Pyramid's debugtoolbar does exactly what you describe. They use SQLAlchemy's events to hook into the base Engine class:

from sqlalchemy import event
from sqlalchemy.engine.base import Engine

@event.listens_for(Engine, "before_cursor_execute")
def before_cursor_execute(conn, cursor, stmt, params, context, execmany):
    # do something

@event.listens_for(Engine, "after_cursor_execute")
def after_cursor_execute(conn, cursor, stmt, params, context, execmany):
    # do something

Maybe you can modify that code to work with web.py.

OTHER TIPS

Take a look at pgbadger. It's a log parser for the database logs. You will need to setup logging on the database and parse those logs with pgbadger. See the link for details. http://dalibo.github.io/pgbadger/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top