From de2fda9e3eca85701b5a7f66ca1a3756cd46de11 Mon Sep 17 00:00:00 2001 From: Leighton Date: Tue, 17 Mar 2020 13:58:21 -0700 Subject: [PATCH 01/14] pymy --- docs/ext/pymysql/pymysql.rst | 10 ++++ ext/opentelemetry-ext-dbapi/README.rst | 4 +- .../src/opentelemetry/ext/dbapi/__init__.py | 5 ++ ext/opentelemetry-ext-mysql/setup.cfg | 2 +- ext/opentelemetry-ext-psycopg2/README.rst | 2 +- ext/opentelemetry-ext-pymysql/CHANGELOG.md | 3 ++ ext/opentelemetry-ext-pymysql/README.rst | 33 +++++++++++++ ext/opentelemetry-ext-pymysql/setup.cfg | 47 +++++++++++++++++++ ext/opentelemetry-ext-pymysql/setup.py | 26 ++++++++++ .../src/opentelemetry/ext/pymysql/__init__.py | 43 +++++++++++++++++ .../src/opentelemetry/ext/pymysql/version.py | 15 ++++++ .../tests/__init__.py | 0 .../tests/test_pymysql_integration.py | 43 +++++++++++++++++ 13 files changed, 229 insertions(+), 4 deletions(-) create mode 100644 docs/ext/pymysql/pymysql.rst create mode 100644 ext/opentelemetry-ext-pymysql/CHANGELOG.md create mode 100644 ext/opentelemetry-ext-pymysql/README.rst create mode 100644 ext/opentelemetry-ext-pymysql/setup.cfg create mode 100644 ext/opentelemetry-ext-pymysql/setup.py create mode 100644 ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py create mode 100644 ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py create mode 100644 ext/opentelemetry-ext-pymysql/tests/__init__.py create mode 100644 ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py diff --git a/docs/ext/pymysql/pymysql.rst b/docs/ext/pymysql/pymysql.rst new file mode 100644 index 00000000000..81e2963b9e4 --- /dev/null +++ b/docs/ext/pymysql/pymysql.rst @@ -0,0 +1,10 @@ +.. include:: ../../../ext/opentelemetry-ext-pymysql/README.rst + + +Module contents +--------------- + +.. automodule:: opentelemetry.ext.pymysql + :members: + :undoc-members: + :show-inheritance: diff --git a/ext/opentelemetry-ext-dbapi/README.rst b/ext/opentelemetry-ext-dbapi/README.rst index b3f91511c38..bac97bc3b6f 100644 --- a/ext/opentelemetry-ext-dbapi/README.rst +++ b/ext/opentelemetry-ext-dbapi/README.rst @@ -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") + 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 diff --git a/ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py b/ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py index 88e9d3a0b12..5c9348b1a8c 100644 --- a/ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py +++ b/ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py @@ -125,6 +125,11 @@ def get_connection_attributes(self, connection): self.name = self.database_component self.database = self.connection_props.get("database", "") if self.database: + # PyMySQL encodes names with utf-8 + try: + self.database = self.database.decode() + except AttributeError: + pass self.name += "." + self.database user = self.connection_props.get("user") if user is not None: diff --git a/ext/opentelemetry-ext-mysql/setup.cfg b/ext/opentelemetry-ext-mysql/setup.cfg index 2d69dadd0c0..f77555a9981 100644 --- a/ext/opentelemetry-ext-mysql/setup.cfg +++ b/ext/opentelemetry-ext-mysql/setup.cfg @@ -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 diff --git a/ext/opentelemetry-ext-psycopg2/README.rst b/ext/opentelemetry-ext-psycopg2/README.rst index 00222a40136..c6265b69325 100644 --- a/ext/opentelemetry-ext-psycopg2/README.rst +++ b/ext/opentelemetry-ext-psycopg2/README.rst @@ -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__) diff --git a/ext/opentelemetry-ext-pymysql/CHANGELOG.md b/ext/opentelemetry-ext-pymysql/CHANGELOG.md new file mode 100644 index 00000000000..1512c421622 --- /dev/null +++ b/ext/opentelemetry-ext-pymysql/CHANGELOG.md @@ -0,0 +1,3 @@ +# Changelog + +## Unreleased diff --git a/ext/opentelemetry-ext-pymysql/README.rst b/ext/opentelemetry-ext-pymysql/README.rst new file mode 100644 index 00000000000..ab35ace4fe0 --- /dev/null +++ b/ext/opentelemetry-ext-pymysql/README.rst @@ -0,0 +1,33 @@ +OpenTelemetry PyMySQL integration +=============================== + +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 `_ diff --git a/ext/opentelemetry-ext-pymysql/setup.cfg b/ext/opentelemetry-ext-pymysql/setup.cfg new file mode 100644 index 00000000000..a7b20616fd5 --- /dev/null +++ b/ext/opentelemetry-ext-pymysql/setup.cfg @@ -0,0 +1,47 @@ +# 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. +# +[metadata] +name = opentelemetry-ext-mysql +description = OpenTelemetry PyMySQL integration +long_description = file: README.rst +long_description_content_type = text/x-rst +author = OpenTelemetry Authors +author_email = cncf-opentelemetry-contributors@lists.cncf.io +url = https://github.com/open-telemetry/opentelemetry-python/ext/opentelemetry-ext-pymysql +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 + +[options] +python_requires = >=3.4 +package_dir= + =src +packages=find_namespace: +install_requires = + opentelemetry-api >= 0.6.dev0 + opentelemetry-ext-dbapi >= 0.6.dev0 + PyMySQL ~= 0.9.3 + +[options.packages.find] +where = src diff --git a/ext/opentelemetry-ext-pymysql/setup.py b/ext/opentelemetry-ext-pymysql/setup.py new file mode 100644 index 00000000000..943842e3535 --- /dev/null +++ b/ext/opentelemetry-ext-pymysql/setup.py @@ -0,0 +1,26 @@ +# 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. +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__"]) diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py new file mode 100644 index 00000000000..07a7bc30972 --- /dev/null +++ b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py @@ -0,0 +1,43 @@ +# 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-mysql package allows tracing MySQL queries made by the +MySQL Connector/Python 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, + ) diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py new file mode 100644 index 00000000000..373ae92cb85 --- /dev/null +++ b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py @@ -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" diff --git a/ext/opentelemetry-ext-pymysql/tests/__init__.py b/ext/opentelemetry-ext-pymysql/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py new file mode 100644 index 00000000000..489b162775d --- /dev/null +++ b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py @@ -0,0 +1,43 @@ +# 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 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( + tracer, + "start_as_current_span", + autospec=True, + spec_set=True, + return_value=span, + ) + start_as_current_span = start_current_span_patcher.start() + + 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) From 789eaeda9286e30df1717f8a59a388b4863b999c Mon Sep 17 00:00:00 2001 From: Leighton Date: Tue, 17 Mar 2020 14:17:37 -0700 Subject: [PATCH 02/14] fix lint --- .../src/opentelemetry/ext/pymysql/__init__.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py index 07a7bc30972..d37858abf79 100644 --- a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py +++ b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py @@ -34,10 +34,5 @@ def trace_integration(tracer: Tracer): "user": "user", } db_integration( - tracer, - pymysql, - "connect", - "mysql", - "sql", - connection_attributes, + tracer, pymysql, "connect", "mysql", "sql", connection_attributes ) From 69af52d8073bfe32dde360c12d22a4b0fb51b512 Mon Sep 17 00:00:00 2001 From: Leighton Date: Tue, 17 Mar 2020 14:37:00 -0700 Subject: [PATCH 03/14] tox --- tox.ini | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tox.ini b/tox.ini index 52c82eba075..121a3c2c573 100644 --- a/tox.ini +++ b/tox.ini @@ -59,6 +59,10 @@ envlist = py3{4,5,6,7,8}-test-ext-pymongo pypy3-test-ext-pymongo + ; opentelemetry-ext-mysql + 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 @@ -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 @@ -150,6 +155,7 @@ 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-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 From 7d25e82c1b29adb1c8793d6a149cea5c4e72fe34 Mon Sep 17 00:00:00 2001 From: Leighton Date: Tue, 17 Mar 2020 14:56:20 -0700 Subject: [PATCH 04/14] dbapi --- tox.ini | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 121a3c2c573..6fb66b5cb63 100644 --- a/tox.ini +++ b/tox.ini @@ -59,7 +59,7 @@ envlist = py3{4,5,6,7,8}-test-ext-pymongo pypy3-test-ext-pymongo - ; opentelemetry-ext-mysql + ; opentelemetry-ext-pymysql py3{4,5,6,7,8}-test-ext-pymysql pypy3-test-ext-pymysql @@ -155,6 +155,7 @@ 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 From 0037579da9794ba0c49368fd60d43cb451ec095a Mon Sep 17 00:00:00 2001 From: Leighton Date: Tue, 17 Mar 2020 15:30:49 -0700 Subject: [PATCH 05/14] Fix import --- ext/opentelemetry-ext-pymysql/README.rst | 2 +- .../src/opentelemetry/ext/{pymysql => pymysql_}/__init__.py | 4 ++-- .../src/opentelemetry/ext/{pymysql => pymysql_}/version.py | 0 .../tests/test_pymysql_integration.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/{pymysql => pymysql_}/__init__.py (90%) rename ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/{pymysql => pymysql_}/version.py (100%) diff --git a/ext/opentelemetry-ext-pymysql/README.rst b/ext/opentelemetry-ext-pymysql/README.rst index ab35ace4fe0..4d5097c04fc 100644 --- a/ext/opentelemetry-ext-pymysql/README.rst +++ b/ext/opentelemetry-ext-pymysql/README.rst @@ -13,7 +13,7 @@ Usage import pymysql from opentelemetry import trace - from opentelemetry.ext.pymysql import trace_integration + from opentelemetry.ext.pymysql_ import trace_integration from opentelemetry.sdk.trace import TracerProvider trace.set_tracer_provider(TracerProvider()) diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql_/__init__.py similarity index 90% rename from ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py rename to ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql_/__init__.py index d37858abf79..4dd4840170d 100644 --- a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py +++ b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql_/__init__.py @@ -13,8 +13,8 @@ # limitations under the License. """ -The opentelemetry-ext-mysql package allows tracing MySQL queries made by the -MySQL Connector/Python library. +The opentelemetry-ext-pymysql package allows tracing MySQL queries made by the +PyMySQL library. """ import pymysql diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql_/version.py similarity index 100% rename from ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py rename to ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql_/version.py diff --git a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py index 489b162775d..4dd7bcb0b03 100644 --- a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py +++ b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py @@ -18,7 +18,7 @@ import pymysql from opentelemetry import trace as trace_api -from opentelemetry.ext.pymysql import trace_integration +from opentelemetry.ext.pymysql_ import trace_integration class TestPyMysqlIntegration(unittest.TestCase): From 71c47546d72a0c165d9fc29072fa0d42abea5145 Mon Sep 17 00:00:00 2001 From: Leighton Date: Tue, 17 Mar 2020 16:07:24 -0700 Subject: [PATCH 06/14] add path --- ext/opentelemetry-ext-pymysql/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-pymysql/setup.py b/ext/opentelemetry-ext-pymysql/setup.py index 943842e3535..1427bb1064c 100644 --- a/ext/opentelemetry-ext-pymysql/setup.py +++ b/ext/opentelemetry-ext-pymysql/setup.py @@ -17,7 +17,7 @@ BASE_DIR = os.path.dirname(__file__) VERSION_FILENAME = os.path.join( - BASE_DIR, "src", "opentelemetry", "ext", "pymysql", "version.py" + BASE_DIR, "src", "opentelemetry", "ext", "pymysql_", "version.py" ) PACKAGE_INFO = {} with open(VERSION_FILENAME) as f: From 225bb4c272e06d2f5ebe786d0590106c26d22f9e Mon Sep 17 00:00:00 2001 From: Leighton Date: Tue, 17 Mar 2020 16:40:52 -0700 Subject: [PATCH 07/14] fix name --- ext/opentelemetry-ext-pymysql/README.rst | 2 +- ext/opentelemetry-ext-pymysql/setup.cfg | 2 +- ext/opentelemetry-ext-pymysql/setup.py | 2 +- .../src/opentelemetry/ext/{pymysql_ => pymysql}/__init__.py | 0 .../src/opentelemetry/ext/{pymysql_ => pymysql}/version.py | 0 ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py | 2 +- tox.ini | 1 + 7 files changed, 5 insertions(+), 4 deletions(-) rename ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/{pymysql_ => pymysql}/__init__.py (100%) rename ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/{pymysql_ => pymysql}/version.py (100%) diff --git a/ext/opentelemetry-ext-pymysql/README.rst b/ext/opentelemetry-ext-pymysql/README.rst index 4d5097c04fc..ab35ace4fe0 100644 --- a/ext/opentelemetry-ext-pymysql/README.rst +++ b/ext/opentelemetry-ext-pymysql/README.rst @@ -13,7 +13,7 @@ Usage import pymysql from opentelemetry import trace - from opentelemetry.ext.pymysql_ import trace_integration + from opentelemetry.ext.pymysql import trace_integration from opentelemetry.sdk.trace import TracerProvider trace.set_tracer_provider(TracerProvider()) diff --git a/ext/opentelemetry-ext-pymysql/setup.cfg b/ext/opentelemetry-ext-pymysql/setup.cfg index a7b20616fd5..36f919236d3 100644 --- a/ext/opentelemetry-ext-pymysql/setup.cfg +++ b/ext/opentelemetry-ext-pymysql/setup.cfg @@ -13,7 +13,7 @@ # limitations under the License. # [metadata] -name = opentelemetry-ext-mysql +name = opentelemetry-ext-pymysql description = OpenTelemetry PyMySQL integration long_description = file: README.rst long_description_content_type = text/x-rst diff --git a/ext/opentelemetry-ext-pymysql/setup.py b/ext/opentelemetry-ext-pymysql/setup.py index 1427bb1064c..943842e3535 100644 --- a/ext/opentelemetry-ext-pymysql/setup.py +++ b/ext/opentelemetry-ext-pymysql/setup.py @@ -17,7 +17,7 @@ BASE_DIR = os.path.dirname(__file__) VERSION_FILENAME = os.path.join( - BASE_DIR, "src", "opentelemetry", "ext", "pymysql_", "version.py" + BASE_DIR, "src", "opentelemetry", "ext", "pymysql", "version.py" ) PACKAGE_INFO = {} with open(VERSION_FILENAME) as f: diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql_/__init__.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py similarity index 100% rename from ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql_/__init__.py rename to ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql_/version.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py similarity index 100% rename from ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql_/version.py rename to ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py diff --git a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py index 4dd7bcb0b03..489b162775d 100644 --- a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py +++ b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py @@ -18,7 +18,7 @@ import pymysql from opentelemetry import trace as trace_api -from opentelemetry.ext.pymysql_ import trace_integration +from opentelemetry.ext.pymysql import trace_integration class TestPyMysqlIntegration(unittest.TestCase): diff --git a/tox.ini b/tox.ini index 6fb66b5cb63..2780c96fd27 100644 --- a/tox.ini +++ b/tox.ini @@ -215,6 +215,7 @@ deps = thrift pymongo flask + pymysql mysql-connector-python wrapt psycopg2-binary From 6e2f49f9106239986179ba8540d60ccfdb9122c7 Mon Sep 17 00:00:00 2001 From: Leighton Date: Fri, 20 Mar 2020 11:17:00 -0700 Subject: [PATCH 08/14] address comments --- ext/opentelemetry-ext-dbapi/README.rst | 2 +- .../src/opentelemetry/ext/dbapi/__init__.py | 6 ++---- .../tests/test_pymysql_integration.py | 4 +++- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/opentelemetry-ext-dbapi/README.rst b/ext/opentelemetry-ext-dbapi/README.rst index bac97bc3b6f..9784232c3f2 100644 --- a/ext/opentelemetry-ext-dbapi/README.rst +++ b/ext/opentelemetry-ext-dbapi/README.rst @@ -13,9 +13,9 @@ Usage import mysql.connector import pyodbc + from opentelemetry import trace from opentelemetry.ext.dbapi import trace_integration from opentelemetry.sdk.trace import TracerProvider - from opentelemetry.trace import tracer_provider trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) diff --git a/ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py b/ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py index 5c9348b1a8c..01857be1de7 100644 --- a/ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py +++ b/ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py @@ -126,10 +126,8 @@ def get_connection_attributes(self, connection): self.database = self.connection_props.get("database", "") if self.database: # PyMySQL encodes names with utf-8 - try: - self.database = self.database.decode() - except AttributeError: - pass + if hasattr(self.database, "decode"): + self.database = self.database.decode(errors="ignore") self.name += "." + self.database user = self.connection_props.get("user") if user is not None: diff --git a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py index 489b162775d..01ca3532895 100644 --- a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py +++ b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py @@ -1,4 +1,4 @@ -# Copyright 2019, OpenTelemetry Authors +# 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. @@ -41,3 +41,5 @@ def test_trace_integration(self): query = "SELECT * FROM test" cursor.execute(query) self.assertTrue(start_as_current_span.called) + + start_current_span_patcher.stop() From a7b82c3d6925f9303638a578f2614f2448c0d944 Mon Sep 17 00:00:00 2001 From: Leighton Date: Mon, 13 Apr 2020 14:41:01 -0700 Subject: [PATCH 09/14] functional tests --- .../tests/pymysql/test_pymysql_functional.py | 108 ++++++++++++++++++ ext/opentelemetry-ext-pymysql/README.rst | 6 +- .../src/opentelemetry/ext/pymysql/__init__.py | 2 +- .../src/opentelemetry/ext/pymysql/version.py | 2 +- .../tests/test_pymysql_integration.py | 2 +- tox.ini | 4 +- 6 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py diff --git a/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py b/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py new file mode 100644 index 00000000000..3bf0933588a --- /dev/null +++ b/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py @@ -0,0 +1,108 @@ +# 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. + +import os +import time +import unittest + +import pymysql as pymy + +from opentelemetry import trace as trace_api +from opentelemetry.ext.pymysql import trace_integration +from opentelemetry.sdk.trace import Tracer, TracerProvider +from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor +from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( + InMemorySpanExporter, +) + +MYSQL_USER = os.getenv("MYSQL_USER ", "testuser") +MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD ", "testpassword") +MYSQL_HOST = os.getenv("MYSQL_HOST ", "localhost") +MYSQL_PORT = int(os.getenv("MYSQL_PORT ", "3306")) +MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME ", "opentelemetry-tests") + + +class TestFunctionalPyMysql(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls._connection = None + cls._cursor = None + cls._tracer_provider = TracerProvider() + cls._tracer = Tracer(cls._tracer_provider, None) + cls._span_exporter = InMemorySpanExporter() + cls._span_processor = SimpleExportSpanProcessor(cls._span_exporter) + cls._tracer_provider.add_span_processor(cls._span_processor) + trace_integration(cls._tracer) + cls._connection = pymy.connect( + user=MYSQL_USER, + password=MYSQL_PASSWORD, + host=MYSQL_HOST, + port=MYSQL_PORT, + database=MYSQL_DB_NAME, + ) + cls._cursor = cls._connection.cursor() + + @classmethod + def tearDownClass(cls): + if cls._connection: + cls._connection.close() + + def setUp(self): + self._span_exporter.clear() + + def validate_spans(self): + spans = self._span_exporter.get_finished_spans() + self.assertEqual(len(spans), 2) + for span in spans: + if span.name == "rootSpan": + root_span = span + else: + db_span = span + self.assertIsInstance(span.start_time, int) + self.assertIsInstance(span.end_time, int) + self.assertIsNotNone(root_span) + self.assertIsNotNone(db_span) + self.assertEqual(root_span.name, "rootSpan") + self.assertEqual(db_span.name, "pymysql.opentelemetry-tests") + self.assertIsNotNone(db_span.parent) + self.assertEqual(db_span.parent.name, root_span.name) + self.assertIs(db_span.kind, trace_api.SpanKind.CLIENT) + self.assertEqual(db_span.attributes["db.instance"], MYSQL_DB_NAME) + self.assertEqual(db_span.attributes["net.peer.name"], MYSQL_HOST) + self.assertEqual(db_span.attributes["net.peer.port"], MYSQL_PORT) + + def test_execute(self): + """Should create a child span for execute + """ + with self._tracer.start_as_current_span("rootSpan"): + self._cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") + self.validate_spans() + + def test_executemany(self): + """Should create a child span for executemany + """ + with self._tracer.start_as_current_span("rootSpan"): + data = ["1", "2", "3"] + stmt = "INSERT INTO test (id) VALUES (%s)" + self._cursor.executemany(stmt, data) + self.validate_spans() + + def test_callproc(self): + """Should create a child span for callproc + """ + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( + Exception + ): + self._cursor.callproc("test", ()) + self.validate_spans() diff --git a/ext/opentelemetry-ext-pymysql/README.rst b/ext/opentelemetry-ext-pymysql/README.rst index 75eeb3f576e..3cf845366b9 100644 --- a/ext/opentelemetry-ext-pymysql/README.rst +++ b/ext/opentelemetry-ext-pymysql/README.rst @@ -1,11 +1,15 @@ OpenTelemetry PyMySQL integration -=============================== +================================= |pypi| .. |pypi| image:: https://badge.fury.io/py/opentelemetry-ext-pymysql.svg :target: https://pypi.org/project/opentelemetry-ext-pymysql/ +Integration with PyMySQL that supports the PyMySQL library and is +specified to trace_integration using 'PyMySQL'. + + Installation ------------ diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py index 87afd4677da..3f81dab753a 100644 --- a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py +++ b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2020, OpenTelemetry Authors +# 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. diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py index fefa5ee9175..86c61362ab5 100644 --- a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py +++ b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py @@ -1,4 +1,4 @@ -# Copyright 2020, OpenTelemetry Authors +# 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. diff --git a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py index 01ca3532895..fa6fc140ecf 100644 --- a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py +++ b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py @@ -1,4 +1,4 @@ -# Copyright 2020, OpenTelemetry Authors +# 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. diff --git a/tox.ini b/tox.ini index 09e9e343b7f..2ec1221ec47 100644 --- a/tox.ini +++ b/tox.ini @@ -269,6 +269,7 @@ deps = docker-compose >= 1.25.2 mysql-connector-python ~= 8.0 pymongo ~= 3.1 + pymysql ~= 0.9.3 psycopg2 ~= 2.8.4 changedir = @@ -280,7 +281,8 @@ commands_pre = -e {toxinidir}/ext/opentelemetry-ext-dbapi \ -e {toxinidir}/ext/opentelemetry-ext-mysql \ -e {toxinidir}/ext/opentelemetry-ext-psycopg2 \ - -e {toxinidir}/ext/opentelemetry-ext-pymongo + -e {toxinidir}/ext/opentelemetry-ext-pymongo \ + -e {toxinidir}/ext/opentelemetry-ext-pymysql docker-compose up -d python check_availability.py commands = From 5d16a3a0f7bca372ccc00328bef9cd56e1925173 Mon Sep 17 00:00:00 2001 From: Leighton Date: Mon, 13 Apr 2020 15:57:11 -0700 Subject: [PATCH 10/14] fix test --- .../tests/pymysql/test_pymysql_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py b/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py index 3bf0933588a..2eb36c63f7f 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py @@ -74,7 +74,7 @@ def validate_spans(self): self.assertIsNotNone(root_span) self.assertIsNotNone(db_span) self.assertEqual(root_span.name, "rootSpan") - self.assertEqual(db_span.name, "pymysql.opentelemetry-tests") + self.assertEqual(db_span.name, "mysql.opentelemetry-tests") self.assertIsNotNone(db_span.parent) self.assertEqual(db_span.parent.name, root_span.name) self.assertIs(db_span.kind, trace_api.SpanKind.CLIENT) From f6acff9eadaa4bb3d044e7848c4a00650dd87393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Thu, 23 Apr 2020 09:02:15 -0500 Subject: [PATCH 11/14] Apply suggestions from code review Committing some of my suggestions that are obvious. --- ext/opentelemetry-ext-mysql/setup.cfg | 2 +- ext/opentelemetry-ext-pymysql/setup.cfg | 9 +++++---- ext/opentelemetry-ext-pymysql/setup.py | 2 +- .../src/opentelemetry/ext/pymysql/__init__.py | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ext/opentelemetry-ext-mysql/setup.cfg b/ext/opentelemetry-ext-mysql/setup.cfg index 59dbcf5e0ac..abc9dcb4359 100644 --- a/ext/opentelemetry-ext-mysql/setup.cfg +++ b/ext/opentelemetry-ext-mysql/setup.cfg @@ -41,7 +41,7 @@ package_dir= packages=find_namespace: install_requires = opentelemetry-api == 0.7.dev0 - opentelemetry-ext-dbapi >= 0.7.dev0 + opentelemetry-ext-dbapi == 0.7.dev0 mysql-connector-python ~= 8.0 [options.packages.find] diff --git a/ext/opentelemetry-ext-pymysql/setup.cfg b/ext/opentelemetry-ext-pymysql/setup.cfg index 36f919236d3..9f44953a402 100644 --- a/ext/opentelemetry-ext-pymysql/setup.cfg +++ b/ext/opentelemetry-ext-pymysql/setup.cfg @@ -1,4 +1,4 @@ -# Copyright 2020, OpenTelemetry Authors +# 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. @@ -19,7 +19,7 @@ long_description = file: README.rst long_description_content_type = text/x-rst author = OpenTelemetry Authors author_email = cncf-opentelemetry-contributors@lists.cncf.io -url = https://github.com/open-telemetry/opentelemetry-python/ext/opentelemetry-ext-pymysql +url = https://github.com/open-telemetry/opentelemetry-python/tree/master/ext/opentelemetry-ext-pymysql platforms = any license = Apache-2.0 classifiers = @@ -32,6 +32,7 @@ classifiers = Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 [options] python_requires = >=3.4 @@ -39,8 +40,8 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api >= 0.6.dev0 - opentelemetry-ext-dbapi >= 0.6.dev0 + opentelemetry-api == 0.7.dev0 + opentelemetry-ext-dbapi == 0.7.dev0 PyMySQL ~= 0.9.3 [options.packages.find] diff --git a/ext/opentelemetry-ext-pymysql/setup.py b/ext/opentelemetry-ext-pymysql/setup.py index 943842e3535..a3f057b310b 100644 --- a/ext/opentelemetry-ext-pymysql/setup.py +++ b/ext/opentelemetry-ext-pymysql/setup.py @@ -1,4 +1,4 @@ -# Copyright 2020, OpenTelemetry Authors +# 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. diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py index 3f81dab753a..4ff6b15b985 100644 --- a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py +++ b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py @@ -31,7 +31,7 @@ trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) trace_integration(tracer) - cnx = pymysql.connect(database='MySQL_Database') + cnx = pymysql.connect(database="MySQL_Database") cursor = cnx.cursor() cursor.execute("INSERT INTO test (testField) VALUES (123)" cnx.commit() From 6e6be347e697f53b99c5ce099ef6ccfb606aacea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Thu, 23 Apr 2020 09:17:58 -0500 Subject: [PATCH 12/14] undo change in python version for lint --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index eb637bc25b1..e0c4314bc07 100644 --- a/tox.ini +++ b/tox.ini @@ -209,7 +209,7 @@ commands = [testenv:lint] -basepython: python3.7 +basepython: python3.8 recreate = True deps = -c dev-requirements.txt From 0b0132c649c304366c7bd82f497b663e9b656e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Thu, 23 Apr 2020 09:50:34 -0500 Subject: [PATCH 13/14] ext/pymysql: Take TracerProvider instead of tracer --- .../tests/pymysql/test_pymysql_functional.py | 2 +- .../src/opentelemetry/ext/pymysql/__init__.py | 14 +++++++--- .../tests/test_pymysql_integration.py | 27 +++++++------------ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py b/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py index 2eb36c63f7f..e4ee65af30f 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py +++ b/ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py @@ -43,7 +43,7 @@ def setUpClass(cls): cls._span_exporter = InMemorySpanExporter() cls._span_processor = SimpleExportSpanProcessor(cls._span_exporter) cls._tracer_provider.add_span_processor(cls._span_processor) - trace_integration(cls._tracer) + trace_integration(cls._tracer_provider) cls._connection = pymy.connect( user=MYSQL_USER, password=MYSQL_PASSWORD, diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py index 4ff6b15b985..e6d85327426 100644 --- a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py +++ b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py @@ -42,22 +42,28 @@ --- """ +import typing + import pymysql -from opentelemetry.ext.dbapi import trace_integration as db_integration -from opentelemetry.trace import Tracer +from opentelemetry.ext.dbapi import wrap_connect +from opentelemetry.ext.pymysql.version import __version__ +from opentelemetry.trace import TracerProvider, get_tracer -def trace_integration(tracer: Tracer): +def trace_integration(tracer_provider: typing.Optional[TracerProvider] = None): """Integrate with the PyMySQL library. https://github.com/PyMySQL/PyMySQL/ """ + + tracer = get_tracer(__name__, __version__, tracer_provider) + connection_attributes = { "database": "db", "port": "port", "host": "host", "user": "user", } - db_integration( + wrap_connect( tracer, pymysql, "connect", "mysql", "sql", connection_attributes ) diff --git a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py index fa6fc140ecf..c452c31ec6d 100644 --- a/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py +++ b/ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py @@ -12,34 +12,27 @@ # 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 +import opentelemetry.ext.pymysql from opentelemetry.ext.pymysql import trace_integration +from opentelemetry.test.test_base import TestBase -class TestPyMysqlIntegration(unittest.TestCase): +class TestPyMysqlIntegration(TestBase): 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( - tracer, - "start_as_current_span", - autospec=True, - spec_set=True, - return_value=span, - ) - start_as_current_span = start_current_span_patcher.start() - with mock.patch("pymysql.connect"): - trace_integration(tracer) + trace_integration() cnx = pymysql.connect(database="test") cursor = cnx.cursor() query = "SELECT * FROM test" cursor.execute(query) - self.assertTrue(start_as_current_span.called) - start_current_span_patcher.stop() + spans_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans_list), 1) + span = spans_list[0] + + # Check version and name in span's instrumentation info + self.check_span_instrumentation_info(span, opentelemetry.ext.pymysql) From a2bf7142a5291ad9f3fb98e29607e92df98a2e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Thu, 23 Apr 2020 10:13:09 -0500 Subject: [PATCH 14/14] quick fixes for documentation --- docs/ext/pymysql/pymysql.rst | 7 ++----- .../src/opentelemetry/ext/pymysql/__init__.py | 3 +-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/docs/ext/pymysql/pymysql.rst b/docs/ext/pymysql/pymysql.rst index 81e2963b9e4..23dca80c4f2 100644 --- a/docs/ext/pymysql/pymysql.rst +++ b/docs/ext/pymysql/pymysql.rst @@ -1,8 +1,5 @@ -.. include:: ../../../ext/opentelemetry-ext-pymysql/README.rst - - -Module contents ---------------- +OpenTelemetry PyMySQL Integration +================================= .. automodule:: opentelemetry.ext.pymysql :members: diff --git a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py index e6d85327426..0f06578e0bc 100644 --- a/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py +++ b/ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py @@ -29,8 +29,7 @@ from opentelemetry.sdk.trace import TracerProvider trace.set_tracer_provider(TracerProvider()) - tracer = trace.get_tracer(__name__) - trace_integration(tracer) + trace_integration() cnx = pymysql.connect(database="MySQL_Database") cursor = cnx.cursor() cursor.execute("INSERT INTO test (testField) VALUES (123)"