-
Notifications
You must be signed in to change notification settings - Fork 614
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
Open
tammy-baylis-swi
wants to merge
24
commits into
open-telemetry:main
Choose a base branch
from
tammy-baylis-swi:mysqlconnector-sqlcomment-v4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add mysql-connector instrumentor support for sqlcommenting #3023
tammy-baylis-swi
wants to merge
24
commits into
open-telemetry:main
from
tammy-baylis-swi:mysqlconnector-sqlcomment-v4
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 tasks
tammy-baylis-swi
changed the title
Add mysql-connector instrumentor support for sqlcommenting
WIP/PoC Add mysql-connector instrumentor support for sqlcommenting
Nov 21, 2024
tammy-baylis-swi
changed the title
WIP/PoC Add mysql-connector instrumentor support for sqlcommenting
Add mysql-connector instrumentor support for sqlcommenting
Nov 21, 2024
Comment on lines
+215
to
+218
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!
tammy-baylis-swi
force-pushed
the
mysqlconnector-sqlcomment-v4
branch
from
November 28, 2024 01:19
cb05dfb
to
957636a
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 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; 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.