open-telemetry / opentelemetry-python-contrib

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

Add sqlcommenting support to MySQL driver instrumentors #2902

Open tammy-baylis-swi opened 2 weeks ago

tammy-baylis-swi commented 2 weeks ago

What problem do you want to solve?

The community instrumentors for psycopg2 and psycopg provide sqlcommenting at the database "driver" level so that context is added to PostgreSQL query statements for the actual requests to database servers to select/insert/etc.

Example: 2024-10-11 19:54:22 UTC,4/1731,0,LOG,00000,"statement: SELECT * FROM city WHERE id = '1819' /*db_driver='psycopg2%3A2.9.9',dbapi_level='2.0',dbapi_threadsafety=2,driver_paramstyle='pyformat',libpq_version=150008,traceparent='00-<TRACE_ID>-<SPAN_ID>-01'*/",,,,,,,,,"","client backend",,0

For MySQL, sqlcommenting must be done at a higher level of abstraction through instrumentation of the ORM sqlalchemy while using a MySQL database driver, which can also be instrumented (mysql-connector, mysqlclient, pymysql). The result is different from driver-level sqlcommenting because the <SPAN_ID> in the traceparent KV of the comment is for the span from sqlalchemy, not the span ID of the database driver.

It would be great to have consistent sqlcommenting whether using MySQL or PostgreSQL.

Describe the solution you'd like

I'd like to implement sqlcommenting support for the existing mysql-connector, mysqlclient, pymysql instrumentors.

Describe alternatives you've considered

No response

Additional Context

No response

Would you like to implement a fix?

Yes

tammy-baylis-swi commented 2 weeks ago

This does overlap with open issue https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1387