open-telemetry / open-telemetry/opentelemetry-python-contrib

dbapi instrumentation does not work for MySQLdb/mysqlclient

Open
#1,319 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug help wanted instrumentation
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the closed-connection behavior with MySQLdb and trace_integration, then inspect the TracedConnectionProxy behavior described in the report. Done means the wrapped connect call returns a usable connection and get_server_info() succeeds without the OperationalError.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.