@@ -64,7 +64,7 @@ def test_instrumentor(self, mock_connect):
6464
6565 @mock .patch ("opentelemetry.instrumentation.dbapi.wrap_connect" )
6666 @mock .patch ("mysql.connector.connect" )
67- def test_instrumentor_sqlcomment_enabled (
67+ def test_instrumentor_sqlcomment_enabled_dbapi_kwargs (
6868 self ,
6969 mock_connect ,
7070 mock_wrap_connect ,
@@ -77,6 +77,118 @@ def test_instrumentor_sqlcomment_enabled(
7777 self .assertEqual (kwargs ["enable_commenter" ], True )
7878 self .assertEqual (kwargs ["commenter_options" ], {"foo" : True })
7979
80+ def test_instrument_with_dbapi_sqlcomment_enabled (
81+ self ,
82+ ):
83+ mock_connect_module = mock .MagicMock (
84+ __name__ = "mysql.connector" ,
85+ __version__ = "foobar" ,
86+ threadsafety = "123" ,
87+ apilevel = "123" ,
88+ paramstyle = "test" ,
89+ )
90+ mock_cursor = mock_connect_module .connect ().cursor ()
91+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
92+ mock_connection = mock .MagicMock ()
93+ mock_connection .cursor .return_value = mock_cursor
94+
95+ with mock .patch (
96+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
97+ mock_connect_module ,
98+ ), mock .patch (
99+ "opentelemetry.instrumentation.dbapi.util_version" ,
100+ return_value = "foobar" ,
101+ ):
102+ MySQLInstrumentor ()._instrument (
103+ enable_commenter = True ,
104+ )
105+ cnx = mock_connect_module .connect (database = "test" )
106+ cursor = cnx .cursor ()
107+ cursor .execute ("Select 1;" )
108+
109+ spans_list = self .memory_exporter .get_finished_spans ()
110+ span = spans_list [0 ]
111+ span_id = format (span .get_span_context ().span_id , "016x" )
112+ trace_id = format (span .get_span_context ().trace_id , "032x" )
113+ self .assertEqual (
114+ mock_cursor .execute .call_args [0 ][0 ],
115+ f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{ trace_id } -{ span_id } -01'*/;" ,
116+ )
117+
118+ def test_instrument_with_dbapi_sqlcomment_enabled_with_options (
119+ self ,
120+ ):
121+ mock_connect_module = mock .MagicMock (
122+ __name__ = "mysql.connector" ,
123+ __version__ = "foobar" ,
124+ threadsafety = "123" ,
125+ apilevel = "123" ,
126+ paramstyle = "test" ,
127+ )
128+ mock_cursor = mock_connect_module .connect ().cursor ()
129+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
130+ mock_connection = mock .MagicMock ()
131+ mock_connection .cursor .return_value = mock_cursor
132+
133+ with mock .patch (
134+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
135+ mock_connect_module ,
136+ ), mock .patch (
137+ "opentelemetry.instrumentation.dbapi.util_version" ,
138+ return_value = "foobar" ,
139+ ):
140+ MySQLInstrumentor ()._instrument (
141+ enable_commenter = True ,
142+ commenter_options = {
143+ "dbapi_level" : False ,
144+ "dbapi_threadsafety" : True ,
145+ "driver_paramstyle" : False ,
146+ },
147+ )
148+ cnx = mock_connect_module .connect (database = "test" )
149+ cursor = cnx .cursor ()
150+ cursor .execute ("Select 1;" )
151+
152+ spans_list = self .memory_exporter .get_finished_spans ()
153+ span = spans_list [0 ]
154+ span_id = format (span .get_span_context ().span_id , "016x" )
155+ trace_id = format (span .get_span_context ().trace_id , "032x" )
156+ self .assertEqual (
157+ mock_cursor .execute .call_args [0 ][0 ],
158+ f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{ trace_id } -{ span_id } -01'*/;" ,
159+ )
160+
161+ def test_instrument_with_dbapi_sqlcomment_not_enabled_default (
162+ self ,
163+ ):
164+ mock_connect_module = mock .MagicMock (
165+ __name__ = "mysql.connector" ,
166+ __version__ = "foobar" ,
167+ threadsafety = "123" ,
168+ apilevel = "123" ,
169+ paramstyle = "test" ,
170+ )
171+ mock_cursor = mock_connect_module .connect ().cursor ()
172+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
173+ mock_connection = mock .MagicMock ()
174+ mock_connection .cursor .return_value = mock_cursor
175+
176+ with mock .patch (
177+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
178+ mock_connect_module ,
179+ ), mock .patch (
180+ "opentelemetry.instrumentation.dbapi.util_version" ,
181+ return_value = "foobar" ,
182+ ):
183+ MySQLInstrumentor ()._instrument ()
184+ cnx = mock_connect_module .connect (database = "test" )
185+ cursor = cnx .cursor ()
186+ cursor .execute ("Select 1;" )
187+ self .assertEqual (
188+ mock_cursor .execute .call_args [0 ][0 ],
189+ "Select 1;" ,
190+ )
191+
80192 @mock .patch ("mysql.connector.connect" )
81193 # pylint: disable=unused-argument
82194 def test_custom_tracer_provider (self , mock_connect ):
@@ -120,7 +232,7 @@ def test_instrument_connection_no_op_tracer_provider(self, mock_connect):
120232 @mock .patch ("opentelemetry.instrumentation.mysql.DatabaseApiIntegration" )
121233 @mock .patch ("mysql.connector.connect" )
122234 # pylint: disable=unused-argument
123- def test_instrument_connection_enable_commenter (
235+ def test_instrument_connection_enable_commenter_dbapi_kwargs (
124236 self ,
125237 mock_connect ,
126238 mock_mysql_dbapi ,
@@ -137,6 +249,114 @@ def test_instrument_connection_enable_commenter(
137249 self .assertEqual (kwargs ["enable_commenter" ], True )
138250 self .assertEqual (kwargs ["commenter_options" ], {"foo" : True })
139251
252+ def test_instrument_connection_with_dbapi_sqlcomment_enabled (self ):
253+ mock_connect_module = mock .MagicMock (
254+ __name__ = "mysql.connector" ,
255+ __version__ = "foobar" ,
256+ threadsafety = "123" ,
257+ apilevel = "123" ,
258+ paramstyle = "test" ,
259+ )
260+ mock_cursor = mock_connect_module .connect ().cursor ()
261+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
262+ mock_connection = mock .MagicMock ()
263+ mock_connection .cursor .return_value = mock_cursor
264+
265+ with mock .patch (
266+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
267+ mock_connect_module ,
268+ ), mock .patch (
269+ "opentelemetry.instrumentation.dbapi.util_version" ,
270+ return_value = "foobar" ,
271+ ):
272+ cnx_proxy = MySQLInstrumentor ().instrument_connection (
273+ mock_connection ,
274+ enable_commenter = True ,
275+ )
276+ cnx_proxy .cursor ().execute ("Select 1;" )
277+
278+ spans_list = self .memory_exporter .get_finished_spans ()
279+ span = spans_list [0 ]
280+ span_id = format (span .get_span_context ().span_id , "016x" )
281+ trace_id = format (span .get_span_context ().trace_id , "032x" )
282+ self .assertEqual (
283+ mock_cursor .execute .call_args [0 ][0 ],
284+ f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{ trace_id } -{ span_id } -01'*/;" ,
285+ )
286+
287+ def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options (
288+ self ,
289+ ):
290+ mock_connect_module = mock .MagicMock (
291+ __name__ = "mysql.connector" ,
292+ __version__ = "foobar" ,
293+ threadsafety = "123" ,
294+ apilevel = "123" ,
295+ paramstyle = "test" ,
296+ )
297+ mock_cursor = mock_connect_module .connect ().cursor ()
298+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
299+ mock_connection = mock .MagicMock ()
300+ mock_connection .cursor .return_value = mock_cursor
301+
302+ with mock .patch (
303+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
304+ mock_connect_module ,
305+ ), mock .patch (
306+ "opentelemetry.instrumentation.dbapi.util_version" ,
307+ return_value = "foobar" ,
308+ ):
309+ cnx_proxy = MySQLInstrumentor ().instrument_connection (
310+ mock_connection ,
311+ enable_commenter = True ,
312+ commenter_options = {
313+ "dbapi_level" : False ,
314+ "dbapi_threadsafety" : True ,
315+ "driver_paramstyle" : False ,
316+ },
317+ )
318+ cnx_proxy .cursor ().execute ("Select 1;" )
319+
320+ spans_list = self .memory_exporter .get_finished_spans ()
321+ span = spans_list [0 ]
322+ span_id = format (span .get_span_context ().span_id , "016x" )
323+ trace_id = format (span .get_span_context ().trace_id , "032x" )
324+ self .assertEqual (
325+ mock_cursor .execute .call_args [0 ][0 ],
326+ f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{ trace_id } -{ span_id } -01'*/;" ,
327+ )
328+
329+ def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default (
330+ self ,
331+ ):
332+ mock_connect_module = mock .MagicMock (
333+ __name__ = "mysql.connector" ,
334+ __version__ = "foobar" ,
335+ threadsafety = "123" ,
336+ apilevel = "123" ,
337+ paramstyle = "test" ,
338+ )
339+ mock_cursor = mock_connect_module .connect ().cursor ()
340+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
341+ mock_connection = mock .MagicMock ()
342+ mock_connection .cursor .return_value = mock_cursor
343+
344+ with mock .patch (
345+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
346+ mock_connect_module ,
347+ ), mock .patch (
348+ "opentelemetry.instrumentation.dbapi.util_version" ,
349+ return_value = "foobar" ,
350+ ):
351+ cnx_proxy = MySQLInstrumentor ().instrument_connection (
352+ mock_connection ,
353+ )
354+ cnx_proxy .cursor ().execute ("Select 1;" )
355+ self .assertEqual (
356+ mock_cursor .execute .call_args [0 ][0 ],
357+ "Select 1;" ,
358+ )
359+
140360 @mock .patch ("mysql.connector.connect" )
141361 # pylint: disable=unused-argument
142362 def test_uninstrument_connection (self , mock_connect ):
0 commit comments