Skip to content

DB-API instrumentation errors if mysql-connector connection wrapper used for queries with sqlcomment enabled #3730

@tammy-baylis-swi

Description

@tammy-baylis-swi

Describe your environment

OS: Ubuntu
Python version: Python 3.11
Package version: opentelemetry-instrumentation-dbapi==0.57b0

What happened?

The DB-API instrumentation errors if mysql-connector connection wrapper is used to make queries with sqlcomment enabled.

Exception while generating sql comment: 'CMySQLCursor' object has no attribute '_cnx'
Traceback (most recent call last):
  File "/.pyenv/versions/sqlcommenter-example-py311/lib/python3.11/site-packages/opentelemetry/instrumentation/dbapi/__init__.py", line 500, in _update_args_with_added_sql_comment
    self._capture_mysql_version(cursor)
  File "/.pyenv/versions/sqlcommenter-example-py311/lib/python3.11/site-packages/opentelemetry/instrumentation/dbapi/__init__.py", line 482, in _capture_mysql_version
    cursor._cnx._cmysql.get_client_info()
    ^^^^^^^^^^^
AttributeError: 'CMySQLCursor' object has no attribute '_cnx'

Steps to Reproduce

from mysql.connector import connect
from opentelemetry.instrumentation.mysql import MySQLInstrumentor

cnx = connect(
    host="localhost",
    port=3366,
    user="books",
    password="books123",
    database="books",
)

# Returns wrapped connection to generate traces.
# Seems required for mysql-connector instrumentation specifically to work.
cnx = MySQLInstrumentor().instrument_connection(
    connection=cnx,
    enable_commenter=True,
    enable_attribute_commenter=True,
)

cursor = cnx.cursor()
statement = "SELECT * FROM authors WHERE id = %s"

for cid in range(1, 4):
    cursor.execute(statement, (cid,))
    rows = cursor.fetchall()

Expected Result

No stacktrace, normal telemetry generation.

Actual Result

Stacktrace happens and spans don't get created.

Additional context

No response

Would you like to implement a fix?

Yes

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions