-
Notifications
You must be signed in to change notification settings - Fork 649
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
Closed
tammy-baylis-swi
wants to merge
27
commits into
open-telemetry:main
from
tammy-baylis-swi:mysqlconnector-sqlcomment-v4
Closed
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5e1e57a
DB-API integration: abstract classes BaseTracedConnection|CursorProxy
tammy-baylis-swi d242454
Add mysql-connector sqlcomment support
tammy-baylis-swi 089c90a
Add tests
tammy-baylis-swi bb1f010
Changelog
tammy-baylis-swi b3c3aae
Merge branch 'main' into mysqlconnector-sqlcomment-v4
tammy-baylis-swi 7fabfcf
Mv dbapi base class to proxy.py
tammy-baylis-swi 83f8e67
Add docstring
tammy-baylis-swi 569e811
Use class attr in Base to fix sqlite3 integration
tammy-baylis-swi 5acb2d0
Add BaseMeta helper for pypy
tammy-baylis-swi 4cbcfe7
Merge branch 'main' into mysqlconnector-sqlcomment-v4
tammy-baylis-swi 78c5130
Merge branch 'main' into mysqlconnector-sqlcomment-v4
tammy-baylis-swi 880c0f7
mysql instrument_connection passes connect_module
tammy-baylis-swi 94bf04a
Update comment
tammy-baylis-swi 19bfd7c
dbapi.instrument_connection optional kwargs db_api_integration_factor…
tammy-baylis-swi ee907e2
mysql uses updated dbapi.instrument_connection
tammy-baylis-swi 4e1b778
Simplify get_traced_connection_proxy and update comment
tammy-baylis-swi a0d3736
Rm is_prepared helper, add quick kwargs check
tammy-baylis-swi 957636a
Add dbapi proxy tests
tammy-baylis-swi 11d4f07
Add dbapi instr_cnx optional params to tests
tammy-baylis-swi 1cf2238
Mv test
tammy-baylis-swi cb9a475
Add mysql sqlcomment integration tests
tammy-baylis-swi b9a52e9
Add get_traced_cnx or cur_proxy tests
tammy-baylis-swi 762eea9
Merge branch 'main' into mysqlconnector-sqlcomment-v4
tammy-baylis-swi da13a71
Update doc
tammy-baylis-swi cc8ef8e
Merge branch 'main' into mysqlconnector-sqlcomment-v4
tammy-baylis-swi b39fd0c
Merge branch 'main' into mysqlconnector-sqlcomment-v4
tammy-baylis-swi b62bd54
Merge branch 'main' into mysqlconnector-sqlcomment-v4
tammy-baylis-swi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
100 changes: 100 additions & 0 deletions
100
...tion/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/proxy.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from abc import ABC, ABCMeta, abstractmethod | ||
|
||
import wrapt | ||
|
||
|
||
class BaseMeta(ABCMeta, type(wrapt.ObjectProxy)): | ||
"""Metaclass compatibility helper for PyPy for derived classes""" | ||
|
||
|
||
class BaseTracedConnectionProxy(ABC, wrapt.ObjectProxy, metaclass=BaseMeta): | ||
"""ABC for traced database client connection proxy. | ||
|
||
Child classes are used to wrap Connection objects and | ||
generate telemetry based on provided integration settings. | ||
|
||
Child classes must implement cursor(), which should return | ||
a traced database client cursor proxy depending on database | ||
driver needs. | ||
""" | ||
|
||
# pylint: disable=unused-argument | ||
def __init__(self, connection, *args, **kwargs): | ||
wrapt.ObjectProxy.__init__(self, connection) | ||
|
||
def __getattribute__(self, name): | ||
if object.__getattribute__(self, name): | ||
return object.__getattribute__(self, name) | ||
|
||
return object.__getattribute__( | ||
object.__getattribute__(self, "_connection"), name | ||
) | ||
|
||
@abstractmethod | ||
def cursor(self, *args, **kwargs): | ||
"""Returns instrumented database query cursor""" | ||
|
||
def __enter__(self): | ||
self.__wrapped__.__enter__() | ||
return self | ||
|
||
def __exit__(self, *args, **kwargs): | ||
self.__wrapped__.__exit__(*args, **kwargs) | ||
|
||
|
||
# pylint: disable=abstract-method | ||
class BaseTracedCursorProxy(ABC, wrapt.ObjectProxy, metaclass=BaseMeta): | ||
"""ABC for traced database client cursor proxy. | ||
|
||
Child classes are used to wrap Cursor objects and | ||
generate telemetry based on provided integration settings. | ||
|
||
Child classes must implement __init__(), which should set | ||
class variable _cursor_tracer as a CursorTracer depending | ||
on database driver needs. | ||
""" | ||
|
||
_cursor_tracer = None | ||
|
||
# pylint: disable=unused-argument | ||
@abstractmethod | ||
def __init__(self, cursor, *args, **kwargs): | ||
"""Wrap db client cursor for tracing""" | ||
wrapt.ObjectProxy.__init__(self, cursor) | ||
|
||
def callproc(self, *args, **kwargs): | ||
return self._cursor_tracer.traced_execution( | ||
self.__wrapped__, self.__wrapped__.callproc, *args, **kwargs | ||
) | ||
|
||
def execute(self, *args, **kwargs): | ||
return self._cursor_tracer.traced_execution( | ||
self.__wrapped__, self.__wrapped__.execute, *args, **kwargs | ||
) | ||
|
||
def executemany(self, *args, **kwargs): | ||
return self._cursor_tracer.traced_execution( | ||
self.__wrapped__, self.__wrapped__.executemany, *args, **kwargs | ||
) | ||
|
||
def __enter__(self): | ||
self.__wrapped__.__enter__() | ||
return self | ||
|
||
def __exit__(self, *args, **kwargs): | ||
self.__wrapped__.__exit__(*args, **kwargs) |
Oops, something went wrong.
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.
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 customizeTracedConnectionProxy.cursor
to check user-specified cursor params. Open to other ideas too!