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

PyMySQL Integration #504

Merged
merged 17 commits into from
Apr 24, 2020
Merged
10 changes: 10 additions & 0 deletions docs/ext/pymysql/pymysql.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. include:: ../../../ext/opentelemetry-ext-pymysql/README.rst


Choose a reason for hiding this comment

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

Currently we're not including that readme in these files anymore.

Module contents
---------------

.. automodule:: opentelemetry.ext.pymysql
:members:
:undoc-members:
:show-inheritance:
4 changes: 2 additions & 2 deletions ext/opentelemetry-ext-dbapi/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Usage
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
# Ex: mysql.connector
trace_integration(tracer_provider(), mysql.connector, "connect", "mysql", "sql")
lzchen marked this conversation as resolved.
Show resolved Hide resolved
trace_integration(tracer, mysql.connector, "connect", "mysql", "sql")
# Ex: pyodbc
trace_integration(tracer_provider(), pyodbc, "Connection", "odbc", "sql")
trace_integration(tracer, pyodbc, "Connection", "odbc", "sql")


References
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def get_connection_attributes(self, connection):
self.name = self.database_component
self.database = self.connection_props.get("database", "")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.database = self.connection_props.get("database", "")
self.database = self.connection_props.get("database", b"")

if self.database:
# PyMySQL encodes names with utf-8
try:
self.database = self.database.decode()
lzchen marked this conversation as resolved.
Show resolved Hide resolved
except AttributeError:
pass
self.name += "." + self.database
user = self.connection_props.get("user")
if user is not None:
Expand Down
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-mysql/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ package_dir=
packages=find_namespace:
install_requires =
opentelemetry-api >= 0.6.dev0
opentelemetry-ext-dbapi >= 0.6.dev0
mysql-connector-python ~= 8.0
wrapt >= 1.0.0, < 2.0.0

[options.packages.find]
where = src
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-psycopg2/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Usage
import psycopg2
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.trace.ext.psycopg2 import trace_integration
from opentelemetry.ext.psycopg2 import trace_integration

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
Expand Down
3 changes: 3 additions & 0 deletions ext/opentelemetry-ext-pymysql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changelog

## Unreleased
33 changes: 33 additions & 0 deletions ext/opentelemetry-ext-pymysql/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
OpenTelemetry PyMySQL integration
===============================
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
===============================
=================================


The integration with PyMySQL supports the `PyMySQL`_ library and is specified
to ``trace_integration`` using ``'PyMySQL'``.

.. _PyMySQL: https://pypi.org/project/PyMySQL/

Usage
-----

.. code:: python

import pymysql
from opentelemetry import trace
from opentelemetry.ext.pymysql import trace_integration
from opentelemetry.sdk.trace import TracerProvider

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
trace_integration(tracer)
cnx = pymysql.connect(database='MySQL_Database')
cursor = cnx.cursor()
cursor.execute("INSERT INTO test (testField) VALUES (123)"
cnx.commit()
cursor.close()
cnx.close()


References
----------

* `OpenTelemetry Project <https://opentelemetry.io/>`_
47 changes: 47 additions & 0 deletions ext/opentelemetry-ext-pymysql/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2020, OpenTelemetry Authors
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved
#
# 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-pymysql
description = OpenTelemetry PyMySQL 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-pymysql
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved
platforms = any
license = Apache-2.0
classifiers =
Development Status :: 4 - Beta
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
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved

[options]
python_requires = >=3.4
package_dir=
=src
packages=find_namespace:
install_requires =
opentelemetry-api >= 0.6.dev0
opentelemetry-ext-dbapi >= 0.6.dev0
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved
PyMySQL ~= 0.9.3

[options.packages.find]
where = src
26 changes: 26 additions & 0 deletions ext/opentelemetry-ext-pymysql/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2020, OpenTelemetry Authors
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved
#
# 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", "pymysql", "version.py"
)
PACKAGE_INFO = {}
with open(VERSION_FILENAME) as f:
exec(f.read(), PACKAGE_INFO)

setuptools.setup(version=PACKAGE_INFO["__version__"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2020, 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-pymysql package allows tracing MySQL queries made by the
PyMySQL library.
"""

import pymysql

from opentelemetry.ext.dbapi import trace_integration as db_integration
from opentelemetry.trace import Tracer


def trace_integration(tracer: Tracer):
"""Integrate with the PyMySQL library.
https://github.com/PyMySQL/PyMySQL/
"""
connection_attributes = {
"database": "db",
"port": "port",
"host": "host",
"user": "user",
}
db_integration(
tracer, pymysql, "connect", "mysql", "sql", connection_attributes
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2020, 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.6.dev0"
Empty file.
43 changes: 43 additions & 0 deletions ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2019, OpenTelemetry Authors
lzchen marked this conversation as resolved.
Show resolved Hide resolved
#
# 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 unittest
from unittest import mock

import pymysql

from opentelemetry import trace as trace_api
from opentelemetry.ext.pymysql import trace_integration


class TestPyMysqlIntegration(unittest.TestCase):
def test_trace_integration(self):
tracer = trace_api.DefaultTracer()
span = mock.create_autospec(trace_api.Span, spec_set=True)
start_current_span_patcher = mock.patch.object(
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved
tracer,
"start_as_current_span",
autospec=True,
spec_set=True,
return_value=span,
)
start_as_current_span = start_current_span_patcher.start()
lzchen marked this conversation as resolved.
Show resolved Hide resolved

with mock.patch("pymysql.connect"):
trace_integration(tracer)
cnx = pymysql.connect(database="test")
cursor = cnx.cursor()
query = "SELECT * FROM test"
cursor.execute(query)
self.assertTrue(start_as_current_span.called)
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ envlist =
py3{4,5,6,7,8}-test-ext-pymongo
pypy3-test-ext-pymongo

; opentelemetry-ext-pymysql
py3{4,5,6,7,8}-test-ext-pymysql
pypy3-test-ext-pymysql

; opentelemetry-ext-wsgi
py3{4,5,6,7,8}-test-ext-wsgi
pypy3-test-ext-wsgi
Expand Down Expand Up @@ -109,6 +113,7 @@ changedir =
test-ext-prometheus: ext/opentelemetry-ext-prometheus/tests
test-ext-pymongo: ext/opentelemetry-ext-pymongo/tests
test-ext-psycopg2: ext/opentelemetry-ext-psycopg2/tests
test-ext-pymysql: ext/opentelemetry-ext-pymysql/tests
test-ext-wsgi: ext/opentelemetry-ext-wsgi/tests
test-ext-zipkin: ext/opentelemetry-ext-zipkin/tests
test-ext-flask: ext/opentelemetry-ext-flask/tests
Expand Down Expand Up @@ -150,6 +155,8 @@ commands_pre =
pymongo: pip install {toxinidir}/ext/opentelemetry-ext-pymongo
psycopg2: pip install {toxinidir}/ext/opentelemetry-ext-dbapi
psycopg2: pip install {toxinidir}/ext/opentelemetry-ext-psycopg2
pymysql: pip install {toxinidir}/ext/opentelemetry-ext-dbapi
pymysql: pip install {toxinidir}/ext/opentelemetry-ext-pymysql
http-requests: pip install {toxinidir}/ext/opentelemetry-ext-http-requests
jaeger: pip install {toxinidir}/opentelemetry-sdk
jaeger: pip install {toxinidir}/ext/opentelemetry-ext-jaeger
Expand Down Expand Up @@ -208,6 +215,7 @@ deps =
thrift
pymongo
flask
pymysql
mysql-connector-python
wrapt
psycopg2-binary
Expand Down