open-telemetry / opentelemetry-python-contrib

OpenTelemetry instrumentation for Python modules
https://opentelemetry.io
Apache License 2.0
739 stars 612 forks source link

Add DB-API integration optional connect_module to instrument_connection #3028

Closed tammy-baylis-swi closed 1 day ago

tammy-baylis-swi commented 2 days ago

What problem do you want to solve?

Generally there are two ways to instrument a Python client-side database driver client framework: instrument() or instrument_connection(). The former seems to be the most-advertised in the docs (e.g. psycopg2, mysqlclient docs) and is also how some distros set up instrumentation (e.g. load_instrumentor). Users can still call instrument_connection() in-code and I'd like to improve this method.

DB-API integration's wrap_connect is used by multiple instrumentors. It accepts connect_module to optionally use for wrapping/unwrapping and populating sqlcomment when opted in.

DB-API integration's instrumentation_connection is called by the instrumentors for mysql-connector, mysqlclient, PyMySQL, and sqlite3. (psycopg2 does it own thing.) Currently this does not accept connect_module. This is needed to better support sqlcommenting by those MySQL driver instrumentors (see also https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2902). Otherwise, sqlcomment will populate with db_driver='unknown%%3Aunknown' -- it doesn't currently because not yet implemented on main.

Describe the solution you'd like

Get DB-API instrument_connection to accept optional connect_module

Describe alternatives you've considered

No response

Additional Context

Relates to https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2902

Would you like to implement a fix?

Yes