请看下面的代码。我在用验光仪追踪。Psycopg2Instrumentor用于PostgreSQL跟踪。这里只跟踪"show server_version“SQL语句。但是execute方法中的SQL语句不会被跟踪。我想是因为使用了NamedTupleCursor cursor_factory。如果删除NamedTupleCursor,它将跟踪主SQL语句。您能帮助我跟踪主SQL语句而不删除NamedTupleCursor吗?
def self.get_connection():
#conn = create_connection()
with conn.cursor() as curs:
curs.execute("show server_version") ---> this sql statement is getting tracked
return conn
def execute()
with self.get_connection() as conn:
with conn.cursor(cursor_factory=NamedTupleCursor) as curs:
curs.execute("Sql statements"). ---> this sql statement is **not** getting tracked```发布于 2022-08-19 14:11:04
下面是使用Psycopg2Instrumentor进行PostgreSQL跟踪的代码片段。当在游标参数中传递cursor_factory时要更新的检测代码,而不是在连接中设置它。现在,下面的工作对我和跟踪被捕获。
import psycopg2
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
Psycopg2Instrumentor().instrument()
#add your cursor factory in connection method
cnx = psycopg2.connect(
host=host, database=DBname, user=user, password=password, cursor_factory=RealDictCursor)
#remove the cursor factory from cursor method
cursor = cnx.cursor()
cursor.execute("SELECT statement")
cursor.close()
cnx.close()感谢线程(工厂)和@RaguramGopi
https://stackoverflow.com/questions/71479085
复制相似问题