mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-30 13:43:03 +08:00
Rename db framework packages from "ext" to "instrumentation" (#966)
This commit is contained in:
@ -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
|
@ -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 <https://opentelemetry-python.readthedocs.io/en/latest/instrumentation/pymysql/pymysql.html>`_
|
||||||
|
* `OpenTelemetry Project <https://opentelemetry.io/>`_
|
@ -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
|
@ -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__"])
|
@ -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)
|
@ -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"
|
@ -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)
|
Reference in New Issue
Block a user