通过元信息增强SQL语句,以获取框架和运行环境信息。
项目描述
sqlcommenter
Python模块,用于为您的SQL查询添加注释形式的元信息,适用于流行的项目。
本地安装
pip3 install --user google-cloud-sqlcommenter
如果您想记录OpenCensus跟踪上下文,只需安装它即可
pip3 install google-cloud-sqlcommenter[opencensus]
如果您想记录OpenTelemetry跟踪上下文(仅限Python 3),只需安装它即可
pip3 install google-cloud-sqlcommenter[opentelemetry]
用法
Django
将提供的Django中间件添加到您的Django项目设置中。在标准的请求→响应周期中执行的所有查询都将带有SQL注释。
MIDDLEWARE = [
'google.cloud.sqlcommenter.django.middleware.SqlCommenter',
...
]
当在Postgresql日志中查看时,会产生
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'*/
如果您想包括OpenCensus属性,必须将SQLCOMMENTER_WITH_OPENCENSUS
设置设置为True
。
如果您想包括OpenTelemetry属性,必须将SQLCOMMENTER_WITH_OPENTELEMETRY
设置设置为True
。
您不能同时使用 OpenTelemetry 和 OpenCensus,因为它们使用相同的属性。
SQLAlchemy
将提供的事件监听器附加到数据库引擎的 before_cursor_execute
事件,并设置 retval=True
。使用该引擎执行的所有查询都将带有 SQL 注释。
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
这将生成类似于在 Postgresql 上查看的后端日志。
2019-05-28 11:52:06.527 PDT [64087] LOG: statement: SELECT * FROM polls_question
/*db_driver='psycopg2',framework='sqlalchemy%3A1.3.4',
traceparent='00-5bd66ef5095369c7b0d1f8f4bd33716a-c532cb4098ac3dd2-01',
tracestate='congo%%3Dt61rcWkgMzE%%2Crojo%%3D00f067aa0ba902b7'*/
Psycopg2
使用提供的游标工厂生成数据库游标。使用此类游标执行的所有查询都将带有 SQL 注释。
import psycopg2
from google.cloud.sqlcommenter.psycopg2.extension import CommenterCursorFactory
cursor_factory = CommenterCursorFactory(
with_db_driver=True,
with_dbapi_level=True,
with_dbapi_threadsafety=True,
with_driver_paramstyle=True,
with_libpq_version=True,
# you may use one of opencensus or opentelemetry
with_opencensus=True,
with_opentelemetry=True,
)
conn = psycopg2.connect(..., cursor_factory=cursor_factory)
cursor = conn.cursor()
cursor.execute(...) # comment will be added before execution
这将生成类似于在 Postgresql 上查看的后端日志。
2019-05-28 02:33:25.287 PDT [57302] LOG: statement: SELECT * FROM
polls_question /*db_driver='psycopg2%%3A2.8.2%%20%%28dt%%20dec%%20pq3%%20ext%%20lo64%%29',
dbapi_level='2.0',dbapi_threadsafety=2,driver_paramstyle='pyformat',
libpq_version=100001,traceparent='00-5bd66ef5095369c7b0d1f8f4bd33716a-c532cb4098ac3dd2-01',
tracestate='congo%%3Dt61rcWkgMzE%%2Crojo%%3D00f067aa0ba902b7'*/
选项
在 Django 中,每个选项都通过将它们转换为大写并添加前缀 SQLCOMMENTER_
转换为 Django 设置。例如,with_framework
由 django 设置 SQLCOMMENTER_WITH_FRAMEWORK
控制。
选项 | 默认包含? | Django | SQLAlchemy | psycopg2 | 注释 |
---|---|---|---|---|---|
with_framework |
:heavy_check_mark | Django 版本 | Flask 版本 | Flask 版本 | |
with_controller |
:heavy_check_mark | Django 视图 | Flask 端点 | Flask 端点 | |
with_route |
:heavy_check_mark | Django 路由 | Flask 路由 | Flask 路由 | |
with_app_name |
Django 应用名称 | ||||
with_opencensus |
W3C TraceContext.Traceparent,W3C TraceContext.Tracestate | W3C TraceContext.Traceparent,W3C TraceContext.Tracestate | W3C TraceContext.Traceparent,W3C TraceContext.Tracestate | [1][3] | |
with_opentelemetry |
W3C TraceContext.Traceparent,W3C TraceContext.Tracestate | W3C TraceContext.Traceparent,W3C TraceContext.Tracestate | W3C TraceContext.Traceparent,W3C TraceContext.Tracestate | [2][3] | |
with_db_driver |
Django 数据库引擎 | SQLAlchemy 数据库驱动程序 | psycopg2 版本 | ||
with_db_framework |
SQLAlchemy 版本 | ||||
with_dbapi_threadsafety |
psycopg2 线程安全 | ||||
with_dbapi_level |
psycopg2 API 级别 | ||||
with_libpq_version |
psycopg2 libpq 版本 | ||||
with_driver_paramstyle |
psycopg2 参数风格 |
[1] opencensus
为了使 opencensus
正确工作,请注意,必须将 Python 的 OpenCensus 安装在 Python 环境中。
[2] opentelemetry
为了使 opentelemetry
正确工作,请注意,必须将 Python 的 OpenTelemetry 安装在 Python 环境中。
[3] traceparent/tracestate
由于 W3C TraceContext 的 traceparent
和 tracestate
对于每个请求来说都非常短暂,包括这些属性可能会对查询缓存产生负面影响。
项目详情
下载文件
下载适合您的平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。
源分布
构建分布
google-cloud-sqlcommenter-2.0.0.tar.gz 的散列
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 748618e8d666e536e41d0e09ec6b019a90c7ec045ca3974a581b7de012fa855b |
|
MD5 | f84383c5e8ff5571a4c4ac9fb7085a34 |
|
BLAKE2b-256 | 5fbe04f8b2613707b9d458036c19a7fd7783fda4c855aeca8101404ba74ccd61 |
google_cloud_sqlcommenter-2.0.0-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ce0d665c01dab489c349cd415c48c908353e051e13185790a2a6dfae07e5f9d6 |
|
MD5 | 3ec7d38535f8ee307e5adfba53cde9bf |
|
BLAKE2b-256 | 3bdc3c03cff1746e7bf02c35e4d05f3fd6466604024c8bf44f36de8ae81768e4 |