Skip to content

Commit 5d89ace

Browse files
Add test case for cmysql unknown
1 parent d0508f3 commit 5d89ace

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/opentelemetry-docker-tests/tests/mysql/test_mysql_sqlcommenter.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,43 @@ def test_commenter_enabled_connection_proxy(self):
7979
cursor.close()
8080
MySQLInstrumentor().uninstrument_connection(instrumented_cnx)
8181
cnx.close()
82+
83+
def test_commenter_enabled_unknown_mysql_client_version(self):
84+
MySQLInstrumentor().instrument(enable_commenter=True)
85+
cnx = mysql.connector.connect(
86+
user=MYSQL_USER,
87+
password=MYSQL_PASSWORD,
88+
host=MYSQL_HOST,
89+
port=MYSQL_PORT,
90+
database=MYSQL_DB_NAME,
91+
)
92+
cursor = cnx.cursor()
93+
94+
# Temporarily remove _cmysql
95+
original_cmysql = None
96+
try:
97+
if hasattr(cursor._cnx, "_cmysql"):
98+
original_cmysql = cursor._cnx._cmysql
99+
delattr(cursor._cnx, "_cmysql")
100+
except AttributeError:
101+
pass
102+
103+
cursor.execute("SELECT 1;")
104+
cursor.fetchall()
105+
106+
self.assertRegex(
107+
cursor.statement,
108+
r"SELECT 1 /\*db_driver='mysql\.connector[^']*',dbapi_level='\d\.\d',dbapi_threadsafety=\d,driver_paramstyle='[^']*',mysql_client_version='unknown',traceparent='[^']*'\*/;",
109+
)
110+
self.assertIn("mysql_client_version='unknown'", cursor.statement)
111+
112+
# Restore _cmysql
113+
if original_cmysql is not None:
114+
try:
115+
cursor._cnx._cmysql = original_cmysql
116+
except AttributeError:
117+
pass
118+
119+
cursor.close()
120+
cnx.close()
121+
MySQLInstrumentor().uninstrument()

0 commit comments

Comments
 (0)