From 8f6af8172b06f29ac9ce50db330f349d9b3327e9 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 3 Aug 2020 17:48:44 -0700 Subject: [PATCH] Rename db framework packages from "ext" to "instrumentation" (#966) --- .../CHANGELOG.md | 12 ++ .../README.rst | 20 +++ .../setup.cfg | 57 +++++++++ .../setup.py | 31 +++++ .../instrumentation/pymysql/__init__.py | 114 +++++++++++++++++ .../instrumentation/pymysql/version.py | 15 +++ .../tests/__init__.py | 0 .../tests/test_pymysql_integration.py | 116 ++++++++++++++++++ 8 files changed, 365 insertions(+) create mode 100644 instrumentation/opentelemetry-instrumentation-pymysql/CHANGELOG.md create mode 100644 instrumentation/opentelemetry-instrumentation-pymysql/README.rst create mode 100644 instrumentation/opentelemetry-instrumentation-pymysql/setup.cfg create mode 100644 instrumentation/opentelemetry-instrumentation-pymysql/setup.py create mode 100644 instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/__init__.py create mode 100644 instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/version.py create mode 100644 instrumentation/opentelemetry-instrumentation-pymysql/tests/__init__.py create mode 100644 instrumentation/opentelemetry-instrumentation-pymysql/tests/test_pymysql_integration.py diff --git a/instrumentation/opentelemetry-instrumentation-pymysql/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-pymysql/CHANGELOG.md new file mode 100644 index 000000000..f9b59fcf7 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-pymysql/CHANGELOG.md @@ -0,0 +1,12 @@ +# Changelog + +## Unreleased + +- Change package name to opentelemetry-instrumentation-pymysql + ([#999](https://github.com/open-telemetry/opentelemetry-python/pull/999)) + +## 0.7b1 + +Released 2020-05-12 + +- Initial release \ No newline at end of file diff --git a/instrumentation/opentelemetry-instrumentation-pymysql/README.rst b/instrumentation/opentelemetry-instrumentation-pymysql/README.rst new file mode 100644 index 000000000..0b566d2a9 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-pymysql/README.rst @@ -0,0 +1,20 @@ +OpenTelemetry PyMySQL Instrumentation +===================================== + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-pymysql.svg + :target: https://pypi.org/project/opentelemetry-instrumentation-pymysql/ + +Installation +------------ + +:: + + pip install opentelemetry-instrumentation-pymysql + + +References +---------- +* `OpenTelemetry PyMySQL Instrumentation `_ +* `OpenTelemetry Project `_ diff --git a/instrumentation/opentelemetry-instrumentation-pymysql/setup.cfg b/instrumentation/opentelemetry-instrumentation-pymysql/setup.cfg new file mode 100644 index 000000000..b00276c9d --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-pymysql/setup.cfg @@ -0,0 +1,57 @@ +# 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. +# +[metadata] +name = opentelemetry-instrumentation-pymysql +description = OpenTelemetry PyMySQL instrumentation +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/tree/master/instrumentation/opentelemetry-instrumentation-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 + Programming Language :: Python :: 3.8 + +[options] +python_requires = >=3.4 +package_dir= + =src +packages=find_namespace: +install_requires = + opentelemetry-api == 0.12.dev0 + opentelemetry-instrumentation-dbapi == 0.12.dev0 + opentelemetry-instrumentation == 0.12.dev0 + PyMySQL ~= 0.9.3 + +[options.extras_require] +test = + opentelemetry-test == 0.12.dev0 + +[options.packages.find] +where = src + +[options.entry_points] +opentelemetry_instrumentor = + pymysql = opentelemetry.instrumentation.pymysql:PyMySQLInstrumentor diff --git a/instrumentation/opentelemetry-instrumentation-pymysql/setup.py b/instrumentation/opentelemetry-instrumentation-pymysql/setup.py new file mode 100644 index 000000000..74cd948fe --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-pymysql/setup.py @@ -0,0 +1,31 @@ +# 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 setuptools + +BASE_DIR = os.path.dirname(__file__) +VERSION_FILENAME = os.path.join( + BASE_DIR, + "src", + "opentelemetry", + "instrumentation", + "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/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/__init__.py b/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/__init__.py new file mode 100644 index 000000000..5d299505d --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/__init__.py @@ -0,0 +1,114 @@ +# 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. + +""" +The integration with PyMySQL supports the `PyMySQL`_ library and can be enabled +by using ``PyMySQLInstrumentor``. + +.. _PyMySQL: https://pypi.org/project/PyMySQL/ + +Usage +----- + +.. code:: python + + import pymysql + from opentelemetry import trace + from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor + from opentelemetry.sdk.trace import TracerProvider + + trace.set_tracer_provider(TracerProvider()) + + PyMySQLInstrumentor().instrument() + + cnx = pymysql.connect(database="MySQL_Database") + cursor = cnx.cursor() + cursor.execute("INSERT INTO test (testField) VALUES (123)" + cnx.commit() + cursor.close() + cnx.close() + +API +--- +""" + +import pymysql + +from opentelemetry.instrumentation import dbapi +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.pymysql.version import __version__ + + +class PyMySQLInstrumentor(BaseInstrumentor): + _CONNECTION_ATTRIBUTES = { + "database": "db", + "port": "port", + "host": "host", + "user": "user", + } + + _DATABASE_COMPONENT = "mysql" + _DATABASE_TYPE = "sql" + + def _instrument(self, **kwargs): + """Integrate with the PyMySQL library. + https://github.com/PyMySQL/PyMySQL/ + """ + tracer_provider = kwargs.get("tracer_provider") + + dbapi.wrap_connect( + __name__, + pymysql, + "connect", + self._DATABASE_COMPONENT, + self._DATABASE_TYPE, + self._CONNECTION_ATTRIBUTES, + version=__version__, + tracer_provider=tracer_provider, + ) + + def _uninstrument(self, **kwargs): + """"Disable PyMySQL instrumentation""" + dbapi.unwrap_connect(pymysql, "connect") + + # pylint:disable=no-self-use + def instrument_connection(self, connection): + """Enable instrumentation in a PyMySQL connection. + + Args: + connection: The connection to instrument. + + Returns: + An instrumented connection. + """ + + return dbapi.instrument_connection( + __name__, + connection, + self._DATABASE_COMPONENT, + self._DATABASE_TYPE, + self._CONNECTION_ATTRIBUTES, + version=__version__, + ) + + def uninstrument_connection(self, connection): + """Disable instrumentation in a PyMySQL connection. + + Args: + connection: The connection to uninstrument. + + Returns: + An uninstrumented connection. + """ + return dbapi.uninstrument_connection(connection) diff --git a/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/version.py b/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/version.py new file mode 100644 index 000000000..780a92b6a --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/version.py @@ -0,0 +1,15 @@ +# 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. + +__version__ = "0.12.dev0" diff --git a/instrumentation/opentelemetry-instrumentation-pymysql/tests/__init__.py b/instrumentation/opentelemetry-instrumentation-pymysql/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/instrumentation/opentelemetry-instrumentation-pymysql/tests/test_pymysql_integration.py b/instrumentation/opentelemetry-instrumentation-pymysql/tests/test_pymysql_integration.py new file mode 100644 index 000000000..35c9f4d32 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-pymysql/tests/test_pymysql_integration.py @@ -0,0 +1,116 @@ +# 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 unittest import mock + +import pymysql + +import opentelemetry.instrumentation.pymysql +from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor +from opentelemetry.sdk import resources +from opentelemetry.test.test_base import TestBase + + +class TestPyMysqlIntegration(TestBase): + def tearDown(self): + super().tearDown() + with self.disable_logging(): + PyMySQLInstrumentor().uninstrument() + + @mock.patch("pymysql.connect") + # pylint: disable=unused-argument + def test_instrumentor(self, mock_connect): + PyMySQLInstrumentor().instrument() + + cnx = pymysql.connect(database="test") + cursor = cnx.cursor() + query = "SELECT * FROM test" + cursor.execute(query) + + 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.instrumentation.pymysql + ) + + # check that no spans are generated after uninstrument + PyMySQLInstrumentor().uninstrument() + + cnx = pymysql.connect(database="test") + cursor = cnx.cursor() + query = "SELECT * FROM test" + cursor.execute(query) + + spans_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans_list), 1) + + @mock.patch("pymysql.connect") + # pylint: disable=unused-argument + def test_custom_tracer_provider(self, mock_connect): + resource = resources.Resource.create({}) + result = self.create_tracer_provider(resource=resource) + tracer_provider, exporter = result + + PyMySQLInstrumentor().instrument(tracer_provider=tracer_provider) + + cnx = pymysql.connect(database="test") + cursor = cnx.cursor() + query = "SELECT * FROM test" + cursor.execute(query) + + spans_list = exporter.get_finished_spans() + self.assertEqual(len(spans_list), 1) + span = spans_list[0] + + self.assertIs(span.resource, resource) + + @mock.patch("pymysql.connect") + # pylint: disable=unused-argument + def test_instrument_connection(self, mock_connect): + cnx = pymysql.connect(database="test") + query = "SELECT * FROM test" + cursor = cnx.cursor() + cursor.execute(query) + + spans_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans_list), 0) + + cnx = PyMySQLInstrumentor().instrument_connection(cnx) + cursor = cnx.cursor() + cursor.execute(query) + + spans_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans_list), 1) + + @mock.patch("pymysql.connect") + # pylint: disable=unused-argument + def test_uninstrument_connection(self, mock_connect): + PyMySQLInstrumentor().instrument() + cnx = pymysql.connect(database="test") + query = "SELECT * FROM test" + cursor = cnx.cursor() + cursor.execute(query) + + spans_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans_list), 1) + + cnx = PyMySQLInstrumentor().uninstrument_connection(cnx) + cursor = cnx.cursor() + cursor.execute(query) + + spans_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans_list), 1)