Skip to content

Commit 3a54847

Browse files
ext/pymysql: Implement instrumentor interface
- Implement instrumentor interface to be usable by autoinstrumentation - Provide mechanishm to uninstrument
1 parent 2ab5e5b commit 3a54847

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

ext/opentelemetry-ext-docker-tests/tests/pymysql/test_pymysql_functional.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import pymysql as pymy
1818

1919
from opentelemetry import trace as trace_api
20-
from opentelemetry.ext.pymysql import trace_integration
20+
from opentelemetry.ext.pymysql import PymysqlInstrumentor
2121
from opentelemetry.test.test_base import TestBase
2222

2323
MYSQL_USER = os.getenv("MYSQL_USER ", "testuser")
@@ -34,7 +34,7 @@ def setUpClass(cls):
3434
cls._connection = None
3535
cls._cursor = None
3636
cls._tracer = cls.tracer_provider.get_tracer(__name__)
37-
trace_integration()
37+
PymysqlInstrumentor().instrument()
3838
cls._connection = pymy.connect(
3939
user=MYSQL_USER,
4040
password=MYSQL_PASSWORD,

ext/opentelemetry-ext-pymysql/setup.cfg

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ packages=find_namespace:
4242
install_requires =
4343
opentelemetry-api == 0.7.dev0
4444
opentelemetry-ext-dbapi == 0.7.dev0
45+
opentelemetry-auto-instrumentation == 0.7.dev0
4546
PyMySQL ~= 0.9.3
4647

4748
[options.extras_require]
@@ -50,3 +51,7 @@ test =
5051

5152
[options.packages.find]
5253
where = src
54+
55+
[options.entry_points]
56+
opentelemetry_instrumentor =
57+
pymysql = opentelemetry.ext.pymysql:PymysqlInstrumentor

ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,30 @@
4545

4646
import pymysql
4747

48-
from opentelemetry.ext.dbapi import wrap_connect
48+
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
49+
from opentelemetry.ext.dbapi import unwrap_connect, wrap_connect
4950
from opentelemetry.ext.pymysql.version import __version__
5051
from opentelemetry.trace import TracerProvider, get_tracer
5152

5253

53-
def trace_integration(tracer_provider: typing.Optional[TracerProvider] = None):
54-
"""Integrate with the PyMySQL library.
55-
https://github.com/PyMySQL/PyMySQL/
56-
"""
54+
class PymysqlInstrumentor(BaseInstrumentor):
55+
def _instrument(self, **kwargs):
56+
"""Integrate with the PyMySQL library.
57+
https://github.com/PyMySQL/PyMySQL/
58+
"""
59+
tracer_provider = kwargs.get("tracer_provider")
5760

58-
tracer = get_tracer(__name__, __version__, tracer_provider)
61+
tracer = get_tracer(__name__, __version__, tracer_provider)
5962

60-
connection_attributes = {
61-
"database": "db",
62-
"port": "port",
63-
"host": "host",
64-
"user": "user",
65-
}
66-
wrap_connect(
67-
tracer, pymysql, "connect", "mysql", "sql", connection_attributes
68-
)
63+
connection_attributes = {
64+
"database": "db",
65+
"port": "port",
66+
"host": "host",
67+
"user": "user",
68+
}
69+
wrap_connect(
70+
tracer, pymysql, "connect", "mysql", "sql", connection_attributes
71+
)
72+
73+
def _uninstrument(self, **kwargs):
74+
unwrap_connect(pymysql, "connect")

ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,35 @@
1717
import pymysql
1818

1919
import opentelemetry.ext.pymysql
20-
from opentelemetry.ext.pymysql import trace_integration
20+
from opentelemetry.ext.pymysql import PymysqlInstrumentor
2121
from opentelemetry.test.test_base import TestBase
2222

2323

2424
class TestPyMysqlIntegration(TestBase):
25-
def test_trace_integration(self):
26-
with mock.patch("pymysql.connect"):
27-
trace_integration()
28-
cnx = pymysql.connect(database="test")
29-
cursor = cnx.cursor()
30-
query = "SELECT * FROM test"
31-
cursor.execute(query)
25+
@mock.patch("pymysql.connect")
26+
# pylint: disable=unused-argument
27+
def test_instrumentor(self, mock_connect):
28+
PymysqlInstrumentor().instrument()
29+
30+
cnx = pymysql.connect(database="test")
31+
cursor = cnx.cursor()
32+
query = "SELECT * FROM test"
33+
cursor.execute(query)
3234

3335
spans_list = self.memory_exporter.get_finished_spans()
3436
self.assertEqual(len(spans_list), 1)
3537
span = spans_list[0]
3638

3739
# Check version and name in span's instrumentation info
3840
self.check_span_instrumentation_info(span, opentelemetry.ext.pymysql)
41+
42+
# check that no spans are generated ater uninstrument
43+
PymysqlInstrumentor().uninstrument()
44+
45+
cnx = pymysql.connect(database="test")
46+
cursor = cnx.cursor()
47+
query = "SELECT * FROM test"
48+
cursor.execute(query)
49+
50+
spans_list = self.memory_exporter.get_finished_spans()
51+
self.assertEqual(len(spans_list), 1)

tox.ini

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ commands_pre =
176176
psycopg2: pip install {toxinidir}/ext/opentelemetry-ext-dbapi
177177
psycopg2: pip install {toxinidir}/ext/opentelemetry-ext-psycopg2
178178

179+
pymysql: pip install {toxinidir}/opentelemetry-auto-instrumentation
179180
pymysql: pip install {toxinidir}/ext/opentelemetry-ext-dbapi
180181
pymysql: pip install {toxinidir}/ext/opentelemetry-ext-pymysql[test]
181182

@@ -286,6 +287,7 @@ changedir =
286287
commands_pre =
287288
pip install -e {toxinidir}/opentelemetry-api \
288289
-e {toxinidir}/opentelemetry-sdk \
290+
-e {toxinidir}/opentelemetry-auto-instrumentation \
289291
-e {toxinidir}/tests/util \
290292
-e {toxinidir}/ext/opentelemetry-ext-dbapi \
291293
-e {toxinidir}/ext/opentelemetry-ext-mysql \

0 commit comments

Comments
 (0)