Add mysql-connector instrumentor support for sqlcommenting #3023
+362
−54
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Updates mysql-connector instrumentor to support sqlcommenting.
To leverage existing code and reduce duplication, this updates DB-API integration instrumentor with new abstract classes
BaseTracedConnectionProxy
andBaseTracedCursorProxy
, while keeping uses byaiopg
,psycopg2
,psycopg
instrumentors the same.To perform checks specific to mysql-connector, its instrumentor has new custom subclasses and methods that extend from DB-API integration, sort of like what
psycopg
instrumentor does. This is to enable_commenter at the db client cursor level (instead of DB-API level) and support mixes of cursor types instrumented. See below for More background.Fixes #2902
Replaces other PR #2943
More about db client cursors and sqlcommenting
If an app-side, mysql-connector cursor is set with
prepared=True
, mysql-connector will perform native prepared statement execution (see docs here). If sqlcommenting is enabled with mysql-connector, native prepared statement execution would result in inaccuracy and a notable performance penalty. In the example MySQL general log below, we see multiple pairs ofPrepare
+Execute
with the same statement after performing a parameterized query 3 times whenprepared=True
:Adding the same sqlcomment to both
Prepare
andExecute
statements would break the 1:1 correlation between sqlcomment and span. It would also take more resources to append sqlcomment to both.If queries are submitted as single
Query
statements (whenprepared=False
), then the 1:1 correlation is maintained:Therefore, new helper
is_mysql_connector_cursor_prepared
checks and disables sqlcommenting only for that cursor ifprepared=True
. Other cursors will still be traced.Currently, native prepared statement execution of MySQL statements is only a mysql-connector feature, and not in mysqlclient nor PyMySQL.
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Unit tests
tox -e py312-test-instrumentation-dbapi
tox -e py312-test-instrumentation-mysql-0
tox -e py312-test-instrumentation-mysql-1
Manual testing
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.