【问题标题】:OpenTelemetry is not tracing SQL Statements while using cursor_factory as NamedTupleCursorOpenTelemetry 在使用 cursor_factory 作为 NamedTupleCursor 时不跟踪 SQL 语句
【发布时间】:2022-08-19 22:11:04
【问题描述】:

请看下面的代码。我正在使用 opentelemetry 进行跟踪。用于 PostgreSQL 跟踪的 Psycopg2Instrumentor。此处仅跟踪“show server_version” SQL 语句。但是execute方法中的SQL语句没有被跟踪。我认为这是因为使用了 NamedTupleCursor cursor_factory。如果我删除 NamedTupleCursor,它会跟踪主要的 SQL 语句。你能帮我在不删除 NamedTupleCursor 的情况下跟踪主要的 SQL 语句吗?

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```
  • 不确定这将如何产生影响,因为游标类只会影响您在 Python 中检索的数据的呈现方式。发送到服务器的查询将是相同的。我会直接查看 Postgres 日志并查看实际访问服务器的内容。
  • Psycopg2Instrumentor 不跟踪 SQL 语句,因为它扩展了 NamedTupleCursor。 @psycopg2 人请在这里提供帮助。
  • 是的,@AdrianKlaver 完全相同的问题。请让我知道解决方案。
  • 解决方案是将您的评论添加到问题中,看看您是否可以让项目修复代码。

标签: python psycopg2 jaeger open-telemetry distributed-tracing


【解决方案1】:

下面是使用 Psycopg2Instrumentor 进行 PostgreSQL 跟踪的代码 sn-p。在游标参数中传递 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()

感谢线程 (Psycopg2Instrumentor doesn't work for cursors with non-default cursor_factory) 和 @RaguramGopi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多