Augment SQL statements with meta information about frameworks and the running environment.
Likely abandoned. No release or commit for years (last release over 5 years ago).
pip install google-cloud-sqlcommenter
sqlcommenter
Python modules for popular projects that add meta info to your SQL queries as comments.
Local Install
pip3 install --user google-cloud-sqlcommenter
If you'd like to record the OpenCensus trace context as well, just install it:
pip3 install google-cloud-sqlcommenter[opencensus]
If you'd like to record the OpenTelemetry trace context as well (Python 3+ only), just install it:
pip3 install google-cloud-sqlcommenter[opentelemetry]
Usage
Django
Add the provided Django middleware to your Django project's settings. All queries executed within the standard request→response cycle will have the SQL comment prepended to them.
MIDDLEWARE = [
'google.cloud.sqlcommenter.django.middleware.SqlCommenter',
...
]
which when viewed say on Postgresql logs, produces
2019-05-28 11:54:50.780 PDT [64128] LOG: statement: INSERT INTO "polls_question"
("question_text", "pub_date") VALUES
('Wassup?', '2019-05-28T18:54:50.767481+00:00'::timestamptz) RETURNING
"polls_question"."id" /*controller='index',framework='django%3A2.2.1',route='%5Epolls/%24'*/
If you want the OpenCensus attributes included, you must set the
SQLCOMMENTER_WITH_OPENCENSUS setting to True.
If you want the OpenTelemetry attributes included, you must set the
SQLCOMMENTER_WITH_OPENTELEMETRY setting to True.
You cannot use OpenTelemetry and OpenCensus together, as they use the same attributes.
SQLAlchemy
Attach the provided event listener to the before_cursor_execute event of the database engine, with retval=True. All queries executed with that engine will have the SQL comment prepended to them.
import sqlalchemy
from google.cloud.sqlcommenter.sqlalchemy.executor import BeforeExecuteFactory
engine = sqlalchemy.create_engine(...)
listener = BeforeExecuteFactory(
with_db_driver=True,
with_db_framework=True,
# you may use one of opencensus or opentelemetry
with_opencensus=True,
with_opentelemetry=True,
)
sqlalchemy.event.listen(engine, 'before_cursor_execute', listener, retval=True)
engine.execute(...) # comment will be added before execution