Skip to content

dbapi instrumentation does not work for MySQLdb/mysqlclient #1319

@alisonatwork

Description

@alisonatwork

Describe your environment

Running Python 3 on WSL. Latest OpenTelemetry.

$ uname -srv
Linux 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021
$ python --version
Python 3.10.6
$ pip freeze | egrep '(opentelem|mysql)'
mysqlclient==2.0.3
opentelemetry-api==1.12.0
opentelemetry-instrumentation==0.33b0
opentelemetry-instrumentation-dbapi==0.33b0
opentelemetry-instrumentation-wsgi==0.33b0
opentelemetry-sdk==1.12.0
opentelemetry-semantic-conventions==0.33b0
opentelemetry-util-http==0.33b0

Steps to reproduce

For some reason when wrapping the connect function of MySQLdb, the supplied connections come back not connected.

$ python
Python 3.10.6 (main, Aug  2 2022, 00:00:00) [GCC 12.1.1 20220507 (Red Hat 12.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb as mysql
>>> c = mysql.connect(host="AAA",password="BBB",user="CCC")
>>> c
<_mysql.connection open to 'AAA' at 0x555c85535c00>
>>> c.get_server_info()
'8.0.30'
>>> c.close()
>>> c
<_mysql.connection closed at 0x555c85535c00>
>>> from opentelemetry.instrumentation.dbapi import trace_integration
>>> trace_integration(mysql, "connect", "mysql")
>>> c = mysql.connect(host="AAA",password="BBB",user="CCC")
>>> c
<_mysql.connection closed at 0x555c857ab600>
>>> c.get_server_info()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MySQLdb._exceptions.OperationalError: (2006, '')

What is the expected behavior?

I would expect to get a usable connection back.

What is the actual behavior?

The connection comes back already closed. It's really weird because if you put some breakpoints you can see that the actual backing Connection object to MySQL is open, but the TracedConnectionProxy object somehow thinks that it is closed. So when you try to do anything with the connection it throws MySQLdb._exceptions.OperationalError: (2006, '') (database is gone). It seems like somehow there is a state in the connection object which the proxy object is not copying somehow, but I can't figure out what it is.

Additional context

MySQLdb is a MySQL client library that wraps around the C libmysqlclient, so perhaps it's something about going across languages that is causing strange things to happen here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions