-
Notifications
You must be signed in to change notification settings - Fork 636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mysql-connector instrumentor support for sqlcommenting #3023
Add mysql-connector instrumentor support for sqlcommenting #3023
Conversation
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the | ||
default one is used. | ||
get_cnx_proxy: Method to get traced connextion proxy. If none is passed the | ||
default one is used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding these optional kwargs to reduce code duplication. mysql instrumentor doesn't need to implement its own instrument_connection
but can still customize TracedConnectionProxy.cursor
to check user-specified cursor params. Open to other ideas too!
cb05dfb
to
957636a
Compare
I chatted with Leighton about this one and I'll re-submit later as multiple PRs. Closing this one. |
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 background
tl;dr: Injecting a unique sqlcomment into a MySQL statement, i.e. with
traceparent
, causes mysql-connector to treat it as new, which imposes new, extra overhead if the cursor is enabled for server-side prepared statements unless we skip it.db client cursors, prepared statements, 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). Here is an example of 'prepared' cursor setup and usage in an instrumented db client app (without sqlcommenting):The mysql-connector cursor will correctly optimize by only doing a
Prepare
phase for theexecute
marked with(2)
. Theexecute
and(3)
and(4)
will skipPrepare
and only doExecute
on the server. Here are the MySQL general logs that result:If we use mysql-connector cursor with
prepared=True
and we enable sqlcommenting, native prepared statement execution would introduce a new performance penalty unless we take precautions (see further down for Solution). Here is the same client app setup but withenable_commenter=True
:This time, the mysql-connector cursor will NOT correctly optimize and do more work because of the introduction of sqlcomment uniqueness! Every client-side
execute
((2)
,(3)
, and(4)
) results in aPrepare
+Execute
, plus aClose
in between! More processing, more caching, more memory use, and increased latency:This does not happen if mysql-connector cursor is set with the default
prepared=False
, without nor with sqlcommenting (logs shown respectively)Solution: the new mysql-connector instrumentor implementation of
TracedConnectionProxy.cursor()
is a precaution that checks and disables sqlcommenting ifprepared=True
, and only for that cursor. Tracing is still enabled because that's still correct (see also comment on original issue); we just don't do sqlcommenting or else performance penalty. Other cursors (i.e.prepared=False
) will still have both tracing and sqlcommenting performed. This check is in the mysql-connector instrumentor and not the DB-API integration because native prepared statement execution as described above is only a mysql-connector feature, and not in mysqlclient nor PyMySQL.Here is an example client app where instrumentation is equipped with new
TracedConnectionProxy.cursor
under the hood while user setsenable_commenter=True
, one mysql-connector cursorprepared=True
, then another cursor after withprepared=False
:Here is the resulting MySQL server general log. The server-side 1
Prepare
+ 3Execute
trigger is preserved by the prepared mysql-connector cursor, and we cannot support sqlcommenting. Meanwhile non-prepared cursors do support sqlcommenting to preserve as much expected functionality as possible: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
prepared=True
prepared=False
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.