Skip to content
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

Adding DB API integration + MySQL connector integration #264

Merged
merged 28 commits into from
Jan 5, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2c2f2ba
Adding MySQL Connector integration
hectorhdzg Nov 4, 2019
47824f2
Fix lint issues
hectorhdzg Nov 4, 2019
b5d81b8
Addressing comments
hectorhdzg Nov 5, 2019
f9f03a8
Fixing issue with typings
hectorhdzg Nov 5, 2019
10f3f53
Using json to store query params
hectorhdzg Nov 6, 2019
e6158c8
Using tuple query params in tests instead of dict
hectorhdzg Nov 6, 2019
315e583
Format issue
hectorhdzg Nov 6, 2019
cb8a835
Adding ext.dbapi package
hectorhdzg Nov 11, 2019
857eed6
Updating readme
hectorhdzg Nov 11, 2019
aefc210
Adding more unit tests
hectorhdzg Nov 14, 2019
ebee38c
Merge branch 'master' into newMaster
hectorhdzg Nov 14, 2019
9cfc808
Addressing comments
hectorhdzg Nov 20, 2019
9997b46
Merge branch 'master' into newMaster
hectorhdzg Nov 20, 2019
d47adcd
Adding check for cursor method
hectorhdzg Nov 20, 2019
ce8e261
Addressing comments
hectorhdzg Dec 9, 2019
d7a1d07
Merge branch 'master' into newMaster
hectorhdzg Dec 9, 2019
03a25b4
Fix format issue
hectorhdzg Dec 9, 2019
4b0e20f
Refactor span creation so it can be used by other
hectorhdzg Dec 10, 2019
7a66815
Refactored integration for easier reuse by
hectorhdzg Dec 11, 2019
7952e63
Merge branch 'master' into newMaster
hectorhdzg Dec 11, 2019
28108a1
Updating to 0.4.dev0
hectorhdzg Dec 11, 2019
93468c0
Merge branch 'newMaster' of https://github.com/hectorhdzg/opentelemet…
hectorhdzg Dec 11, 2019
3f0c082
Fixing invalid syntax in parameters
hectorhdzg Dec 11, 2019
d480718
Ignore lint abstract-method
hectorhdzg Dec 12, 2019
83d95e7
Addressing comments
hectorhdzg Jan 2, 2020
f5fa462
Merge branch 'master' into newMaster
hectorhdzg Jan 2, 2020
0a37e3f
Fixing tests
hectorhdzg Jan 2, 2020
26e4983
Addressing feedback
hectorhdzg Jan 3, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ext/opentelemetry-ext-dbapi/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 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/>`_
46 changes: 46 additions & 0 deletions ext/opentelemetry-ext-dbapi/setup.cfg
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.4.dev0
wrapt >= 1.0.0, < 2.0.0

[options.packages.find]
where = src
26 changes: 26 additions & 0 deletions ext/opentelemetry-ext-dbapi/setup.py
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__"])
219 changes: 219 additions & 0 deletions ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# 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 logging
import typing

import wrapt

from opentelemetry.trace import SpanKind, Tracer
from opentelemetry.trace.status import Status, StatusCanonicalCode

logger = logging.getLogger(__name__)


def trace_integration(
tracer: Tracer,
connect_module: typing.Callable[..., any],
connect_method_name: str,
database_component: str,
database_type: str = "",
connection_attributes: typing.Dict = None,
):
"""Integrate with DB API library.
https://www.python.org/dev/peps/pep-0249/
Args:
tracer: The :class:`Tracer` to use.
connect_module: Module name where connect method is available.
connect_method_name: The connect method name.
database_component: Database driver name or database name "JDBI", "jdbc", "odbc", "postgreSQL".
database_type: The Database type. For any SQL database, "sql".
connection_attributes: Attribute names for database, port, host and user in Connection object.
"""

# 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,
database_type=database_type,
connection_attributes=connection_attributes,
)
return db_integration.wrapped_connection(wrapped, args, kwargs)

try:
wrapt.wrap_function_wrapper(
connect_module, connect_method_name, wrap_connect
)
except Exception as ex: # pylint: disable=broad-except
logger.warning("Failed to integrate with DB API. %s", str(ex))


class DatabaseApiIntegration:
# pylint: disable=unused-argument
def __init__(
self,
tracer: Tracer,
database_component: str,
database_type: str = "sql",
connection_attributes=None,
):
if tracer is None:
raise ValueError("The tracer is not provided.")
self.connection_attributes = connection_attributes
if self.connection_attributes is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify: is it possible that these connection_attributes don't reflect the actual connection established, since this is wrapping the connect method and different args can be passed there?

should there be some documentation added about what the attribute names should be, in that case? I could imagine people using slightly different names for the attributes which could lead to quite a mismatch in key names.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the attributes names depends entirely on the dbapi library being used, if we support more popular libraries the less likely people will make mistakes, I agree having more documentation is always good so I added more details

self.connection_attributes = {
"database": "database",
"port": "port",
"host": "host",
"user": "user",
}
self.tracer = tracer
self.database_component = database_component
self.database_type = database_type
self.connection_props = {}
self.name = ""
self.database = ""

def wrapped_connection(
self,
connect_method: typing.Callable[..., any],
args: typing.Tuple[any, any],
kwargs: typing.Dict[any, any],
):
"""Add object proxy to connection object.
"""
connection = connect_method(*args, **kwargs)

for key, value in self.connection_attributes.items():
attribute = getattr(connection, value, None)
if attribute:
self.connection_props[key] = attribute
traced_connection = TracedConnection(connection, self)
return traced_connection


# pylint: disable=abstract-method
class TracedConnection(wrapt.ObjectProxy):

# pylint: disable=unused-argument
def __init__(
self,
connection,
db_api_integration: DatabaseApiIntegration,
*args,
**kwargs
):
wrapt.ObjectProxy.__init__(self, connection)
self._db_api_integration = db_api_integration

self._db_api_integration.name = (
self._db_api_integration.database_component
)
self._db_api_integration.database = self._db_api_integration.connection_props.get(
"database", ""
)
if self._db_api_integration.database:
self._db_api_integration.name += (
"." + self._db_api_integration.database
)

def cursor(self, *args, **kwargs):
return TracedCursor(
self.__wrapped__.cursor(*args, **kwargs), self._db_api_integration
)


# pylint: disable=abstract-method
class TracedCursor(wrapt.ObjectProxy):

# pylint: disable=unused-argument
def __init__(
self,
cursor,
db_api_integration: DatabaseApiIntegration,
*args,
**kwargs
):
wrapt.ObjectProxy.__init__(self, cursor)
self._db_api_integration = db_api_integration

def execute(self, *args, **kwargs):
return self._traced_execution(
self.__wrapped__.execute, *args, **kwargs
)

def executemany(self, *args, **kwargs):
return self._traced_execution(
self.__wrapped__.executemany, *args, **kwargs
)

def callproc(self, *args, **kwargs):
return self._traced_execution(
self.__wrapped__.callproc, *args, **kwargs
)

def _traced_execution(
self,
query_method: typing.Callable[..., any],
*args: typing.Tuple[any, any],
**kwargs: typing.Dict[any, any]
):

statement = args[0] if args else ""
with self._db_api_integration.tracer.start_as_current_span(
self._db_api_integration.name, kind=SpanKind.CLIENT
) as span:
span.set_attribute(
"component", self._db_api_integration.database_component
)
span.set_attribute(
"db.type", self._db_api_integration.database_type
)
span.set_attribute(
"db.instance", self._db_api_integration.database
)
span.set_attribute("db.statement", statement)

user = self._db_api_integration.connection_props.get("user")
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
if user is not None:
span.set_attribute("db.user", user)
host = self._db_api_integration.connection_props.get("host")
if host is not None:
span.set_attribute("net.peer.name", host)
port = self._db_api_integration.connection_props.get("port")
if port is not None:
span.set_attribute("net.peer.port", port)

if len(args) > 1:
span.set_attribute("db.statement.parameters", str(args[1]))

try:
result = query_method(*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 ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/version.py
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.4.dev0"
Empty file.
Loading