48
48
import pymysql
49
49
50
50
from opentelemetry .auto_instrumentation .instrumentor import BaseInstrumentor
51
- from opentelemetry .ext . dbapi import unwrap_connect , wrap_connect
51
+ from opentelemetry .ext import dbapi
52
52
from opentelemetry .ext .pymysql .version import __version__
53
53
from opentelemetry .trace import TracerProvider , get_tracer
54
54
55
55
56
56
class PyMySQLInstrumentor (BaseInstrumentor ):
57
+ _CONNECTION_ATTRIBUTES = {
58
+ "database" : "db" ,
59
+ "port" : "port" ,
60
+ "host" : "host" ,
61
+ "user" : "user" ,
62
+ }
63
+
64
+ _DATABASE_COMPONENT = "mysql"
65
+ _DATABASE_TYPE = "sql"
66
+
57
67
def _instrument (self , ** kwargs ):
58
68
"""Integrate with the PyMySQL library.
59
69
https://github.com/PyMySQL/PyMySQL/
@@ -62,15 +72,40 @@ def _instrument(self, **kwargs):
62
72
63
73
tracer = get_tracer (__name__ , __version__ , tracer_provider )
64
74
65
- connection_attributes = {
66
- "database" : "db" ,
67
- "port" : "port" ,
68
- "host" : "host" ,
69
- "user" : "user" ,
70
- }
71
- wrap_connect (
72
- tracer , pymysql , "connect" , "mysql" , "sql" , connection_attributes
75
+ dbapi .wrap_connect (
76
+ tracer ,
77
+ pymysql ,
78
+ "connect" ,
79
+ self ._DATABASE_COMPONENT ,
80
+ self ._DATABASE_TYPE ,
81
+ self ._CONNECTION_ATTRIBUTES ,
73
82
)
74
83
75
84
def _uninstrument (self , ** kwargs ):
76
- unwrap_connect (pymysql , "connect" )
85
+ """"Disable PyMySQL instrumentation"""
86
+ dbapi .unwrap_connect (pymysql , "connect" )
87
+
88
+ # pylint:disable=no-self-use
89
+ def instrument_connection (self , connection ):
90
+ """Enable instrumentation in a PyMySQL connection.
91
+
92
+ Args:
93
+ connection: The connection to instrument.
94
+ """
95
+ tracer = get_tracer (__name__ , __version__ )
96
+
97
+ return dbapi .instrument_connection (
98
+ tracer ,
99
+ connection ,
100
+ self ._DATABASE_COMPONENT ,
101
+ self ._DATABASE_TYPE ,
102
+ self ._CONNECTION_ATTRIBUTES ,
103
+ )
104
+
105
+ def uninstrument_connection (self , connection ):
106
+ """Disable instrumentation in a PyMySQL connection.
107
+
108
+ Args:
109
+ connection: The connection to uninstrument.
110
+ """
111
+ return dbapi .uninstrument_connection (connection )
0 commit comments