-
Notifications
You must be signed in to change notification settings - Fork 705
Adding DB API integration + MySQL connector integration #264
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
2c2f2ba
Adding MySQL Connector integration
hectorhdzg 47824f2
Fix lint issues
hectorhdzg b5d81b8
Addressing comments
hectorhdzg f9f03a8
Fixing issue with typings
hectorhdzg 10f3f53
Using json to store query params
hectorhdzg e6158c8
Using tuple query params in tests instead of dict
hectorhdzg 315e583
Format issue
hectorhdzg cb8a835
Adding ext.dbapi package
hectorhdzg 857eed6
Updating readme
hectorhdzg aefc210
Adding more unit tests
hectorhdzg ebee38c
Merge branch 'master' into newMaster
hectorhdzg 9cfc808
Addressing comments
hectorhdzg 9997b46
Merge branch 'master' into newMaster
hectorhdzg d47adcd
Adding check for cursor method
hectorhdzg ce8e261
Addressing comments
hectorhdzg d7a1d07
Merge branch 'master' into newMaster
hectorhdzg 03a25b4
Fix format issue
hectorhdzg 4b0e20f
Refactor span creation so it can be used by other
hectorhdzg 7a66815
Refactored integration for easier reuse by
hectorhdzg 7952e63
Merge branch 'master' into newMaster
hectorhdzg 28108a1
Updating to 0.4.dev0
hectorhdzg 93468c0
Merge branch 'newMaster' of https://github.com/hectorhdzg/opentelemet…
hectorhdzg 3f0c082
Fixing invalid syntax in parameters
hectorhdzg d480718
Ignore lint abstract-method
hectorhdzg 83d95e7
Addressing comments
hectorhdzg f5fa462
Merge branch 'master' into newMaster
hectorhdzg 0a37e3f
Fixing tests
hectorhdzg 26e4983
Addressing feedback
hectorhdzg 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 hidden or 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,26 @@ | ||
OpenTelemetry Database API integration | ||
================================= | ||
|
||
The trace integration with Database API supports libraries following the specification. | ||
|
||
.. PEP 249 -- Python Database API Specification v2.0: https://www.python.org/dev/peps/pep-0249/ | ||
|
||
Usage | ||
----- | ||
|
||
.. code:: python | ||
|
||
import wrapt | ||
import mysql.connector | ||
from opentelemetry.trace import tracer | ||
from opentelemetry.ext.dbapi import trace_integration | ||
|
||
|
||
# Ex: mysql.connector | ||
trace_integration(tracer(), mysql.connector, "connect", "mysql") | ||
|
||
|
||
References | ||
---------- | ||
|
||
* `OpenTelemetry Project <https://opentelemetry.io/>`_ |
This file contains hidden or 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,46 @@ | ||
# Copyright 2019, 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. | ||
# | ||
[metadata] | ||
name = opentelemetry-ext-dbapi | ||
description = OpenTelemetry Database API integration | ||
long_description = file: README.rst | ||
long_description_content_type = text/x-rst | ||
author = OpenTelemetry Authors | ||
author_email = [email protected] | ||
url = https://github.com/open-telemetry/opentelemetry-python/ext/opentelemetry-ext-dbapi | ||
platforms = any | ||
license = Apache-2.0 | ||
classifiers = | ||
Development Status :: 3 - Alpha | ||
Intended Audience :: Developers | ||
License :: OSI Approved :: Apache Software License | ||
Programming Language :: Python | ||
Programming Language :: Python :: 3 | ||
Programming Language :: Python :: 3.4 | ||
Programming Language :: Python :: 3.5 | ||
Programming Language :: Python :: 3.6 | ||
Programming Language :: Python :: 3.7 | ||
|
||
[options] | ||
python_requires = >=3.4 | ||
package_dir= | ||
=src | ||
packages=find_namespace: | ||
install_requires = | ||
opentelemetry-api >= 0.3.dev0 | ||
wrapt >= 1.0.0, < 2.0.0 | ||
|
||
[options.packages.find] | ||
where = src |
This file contains hidden or 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,26 @@ | ||
# Copyright 2019, 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. | ||
import os | ||
|
||
import setuptools | ||
|
||
BASE_DIR = os.path.dirname(__file__) | ||
VERSION_FILENAME = os.path.join( | ||
BASE_DIR, "src", "opentelemetry", "ext", "dbapi", "version.py" | ||
) | ||
PACKAGE_INFO = {} | ||
with open(VERSION_FILENAME) as f: | ||
exec(f.read(), PACKAGE_INFO) | ||
|
||
setuptools.setup(version=PACKAGE_INFO["__version__"]) |
140 changes: 140 additions & 0 deletions
140
ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py
This file contains hidden or 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,140 @@ | ||
# Copyright 2019, 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. | ||
|
||
""" | ||
The opentelemetry-ext-dbapi package allows tracing queries made by the | ||
ibraries following Ptyhon Database API specification: | ||
https://www.python.org/dev/peps/pep-0249/ | ||
""" | ||
|
||
import typing | ||
|
||
import wrapt | ||
|
||
from opentelemetry.trace import SpanKind, Tracer | ||
from opentelemetry.trace.status import Status, StatusCanonicalCode | ||
|
||
QUERY_WRAP_METHODS = ["execute", "executemany", "callproc"] | ||
hectorhdzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def trace_integration( | ||
tracer: Tracer, | ||
connect_module: typing.Callable[..., any], | ||
connect_method_name: str, | ||
database_component: str, | ||
): | ||
"""Integrate with DB API library. | ||
https://www.python.org/dev/peps/pep-0249/ | ||
""" | ||
# pylint: disable=unused-argument | ||
def wrap_connect( | ||
wrapped: typing.Callable[..., any], | ||
instance: typing.Any, | ||
args: typing.Tuple[any, any], | ||
kwargs: typing.Dict[any, any], | ||
): | ||
db_integration = DatabaseApiIntegration(tracer, database_component) | ||
return db_integration.wrapped_connection(wrapped, args, kwargs) | ||
|
||
wrapt.wrap_function_wrapper( | ||
connect_module, connect_method_name, wrap_connect | ||
) | ||
|
||
|
||
class DatabaseApiIntegration: | ||
def __init__( | ||
self, tracer: Tracer, database_component: str, database_type="sql" | ||
): | ||
if tracer is None: | ||
raise ValueError("The tracer is not provided.") | ||
self._tracer = tracer | ||
self._database_component = database_component | ||
self._database_type = database_type | ||
self._connection_props = {} | ||
|
||
def wrapped_connection( | ||
self, | ||
wrapped: typing.Callable[..., any], | ||
args: typing.Tuple[any, any], | ||
kwargs: typing.Dict[any, any], | ||
): | ||
"""Patch connection cursor to add tracing. | ||
""" | ||
connection = wrapped(*args, **kwargs) | ||
self._connection_props = { | ||
hectorhdzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"database": connection.database, | ||
"port": connection.server_port, | ||
"host": connection.server_host, | ||
"user": connection.user, | ||
} | ||
|
||
wrapt.wrap_function_wrapper(connection, "cursor", self.wrapped_cursor) | ||
hectorhdzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return connection | ||
|
||
# pylint: disable=unused-argument | ||
def wrapped_cursor( | ||
self, | ||
wrapped: typing.Callable[..., any], | ||
instance: typing.Any, | ||
args: typing.Tuple[any, any], | ||
kwargs: typing.Dict[any, any], | ||
): | ||
"""Patch cursor execute, executemany and callproc methods. | ||
""" | ||
cursor = wrapped(*args, **kwargs) | ||
for func in QUERY_WRAP_METHODS: | ||
wrapt.wrap_function_wrapper(cursor, func, self.add_span) | ||
hectorhdzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return cursor | ||
|
||
# pylint: disable=unused-argument | ||
def add_span( | ||
self, | ||
wrapped: typing.Callable[..., any], | ||
instance: typing.Any, | ||
args: typing.Tuple[any, any], | ||
kwargs: typing.Dict[any, any], | ||
): | ||
name = self._database_component | ||
database = self._connection_props.get("database", "") | ||
if database: | ||
name += "." + database | ||
query = args[0] if args else "" | ||
# Query with parameters | ||
if len(args) > 1: | ||
query += " params=" + str(args[1]) | ||
hectorhdzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
with self._tracer.start_as_current_span( | ||
name, kind=SpanKind.CLIENT | ||
) as span: | ||
span.set_attribute("component", self._database_component) | ||
span.set_attribute("db.type", self._database_type) | ||
span.set_attribute("db.instance", database) | ||
span.set_attribute("db.statement", query) | ||
span.set_attribute( | ||
"db.user", self._connection_props.get("user", "") | ||
) | ||
span.set_attribute( | ||
"peer.hostname", self._connection_props.get("host", "") | ||
) | ||
port = self._connection_props.get("port") | ||
if port is not None: | ||
span.set_attribute("peer.port", port) | ||
hectorhdzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
try: | ||
result = wrapped(*args, **kwargs) | ||
span.set_status(Status(StatusCanonicalCode.OK)) | ||
return result | ||
except Exception as ex: # pylint: disable=broad-except | ||
span.set_status(Status(StatusCanonicalCode.UNKNOWN, str(ex))) | ||
hectorhdzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise ex |
15 changes: 15 additions & 0 deletions
15
ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/version.py
This file contains hidden or 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,15 @@ | ||
# Copyright 2019, 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. | ||
|
||
__version__ = "0.3.dev0" |
Empty file.
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.
Uh oh!
There was an error while loading. Please reload this page.