@@ -103,29 +103,129 @@ def test_instrument_connection_no_op_tracer_provider(self, mock_connect):
103103 self .assertEqual (len (spans_list ), 0 )
104104
105105 @mock .patch ("opentelemetry.instrumentation.dbapi.instrument_connection" )
106- @mock .patch ("mysql.connector.connect " )
106+ @mock .patch ("mysql.connector" )
107107 # pylint: disable=unused-argument
108- def test_instrument_connection_enable_commenter (
108+ def test_instrument_connection_enable_commenter_dbapi_kwargs (
109109 self ,
110110 mock_connect ,
111111 mock_instrument_connection ,
112112 ):
113- cnx , query = connect_and_execute_query ( )
113+ cnx = mysql . connector . connect ( database = "test" )
114114 cnx = MySQLInstrumentor ().instrument_connection (
115115 cnx ,
116116 enable_commenter = True ,
117117 commenter_options = {"foo" : True },
118118 )
119119 cursor = cnx .cursor ()
120- cursor .execute (query )
120+ cursor .execute ("SELECT * FROM test" )
121121 kwargs = mock_instrument_connection .call_args [1 ]
122122 self .assertEqual (kwargs ["enable_commenter" ], True )
123123 self .assertEqual (kwargs ["commenter_options" ], {"foo" : True })
124124
125+ def test_instrument_connection_with_dbapi_sqlcomment_enabled (self ):
126+ mock_connect_module = mock .MagicMock (
127+ __name__ = "mysql.connector" ,
128+ __version__ = "foobar" ,
129+ threadsafety = "123" ,
130+ apilevel = "123" ,
131+ paramstyle = "test" ,
132+ )
133+ mock_cursor = mock_connect_module .connect ().cursor ()
134+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
135+ mock_connection = mock .MagicMock ()
136+ mock_connection .cursor .return_value = mock_cursor
137+
138+ with mock .patch (
139+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
140+ mock_connect_module ,
141+ ):
142+ cnx_proxy = MySQLInstrumentor ().instrument_connection (
143+ mock_connection ,
144+ enable_commenter = True ,
145+ )
146+ cnx_proxy .cursor ().execute ("Select 1;" )
147+
148+ spans_list = self .memory_exporter .get_finished_spans ()
149+ span = spans_list [0 ]
150+ span_id = format (span .get_span_context ().span_id , "016x" )
151+ trace_id = format (span .get_span_context ().trace_id , "032x" )
152+ self .assertEqual (
153+ mock_cursor .execute .call_args [0 ][0 ],
154+ 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'*/;" ,
155+ )
156+
157+ def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options (
158+ self ,
159+ ):
160+ mock_connect_module = mock .MagicMock (
161+ __name__ = "mysql.connector" ,
162+ __version__ = "foobar" ,
163+ threadsafety = "123" ,
164+ apilevel = "123" ,
165+ paramstyle = "test" ,
166+ )
167+ mock_cursor = mock_connect_module .connect ().cursor ()
168+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
169+ mock_connection = mock .MagicMock ()
170+ mock_connection .cursor .return_value = mock_cursor
171+
172+ with mock .patch (
173+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
174+ mock_connect_module ,
175+ ):
176+ cnx_proxy = MySQLInstrumentor ().instrument_connection (
177+ mock_connection ,
178+ enable_commenter = True ,
179+ commenter_options = {
180+ "dbapi_level" : False ,
181+ "dbapi_threadsafety" : True ,
182+ "driver_paramstyle" : False ,
183+ },
184+ )
185+ cnx_proxy .cursor ().execute ("Select 1;" )
186+
187+ spans_list = self .memory_exporter .get_finished_spans ()
188+ span = spans_list [0 ]
189+ span_id = format (span .get_span_context ().span_id , "016x" )
190+ trace_id = format (span .get_span_context ().trace_id , "032x" )
191+ self .assertEqual (
192+ mock_cursor .execute .call_args [0 ][0 ],
193+ f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{ trace_id } -{ span_id } -01'*/;" ,
194+ )
195+
196+ def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default (
197+ self ,
198+ ):
199+ mock_connect_module = mock .MagicMock (
200+ __name__ = "mysql.connector" ,
201+ __version__ = "foobar" ,
202+ threadsafety = "123" ,
203+ apilevel = "123" ,
204+ paramstyle = "test" ,
205+ )
206+ mock_cursor = mock_connect_module .connect ().cursor ()
207+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
208+ mock_cursor = mock_connect_module .connect ().cursor ()
209+ mock_connection = mock .MagicMock ()
210+ mock_connection .cursor .return_value = mock_cursor
211+
212+ with mock .patch (
213+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
214+ mock_connect_module ,
215+ ):
216+ cnx_proxy = MySQLInstrumentor ().instrument_connection (
217+ mock_connection ,
218+ )
219+ cnx_proxy .cursor ().execute ("Select 1;" )
220+ self .assertEqual (
221+ mock_cursor .execute .call_args [0 ][0 ],
222+ "Select 1;" ,
223+ )
224+
125225 @mock .patch ("opentelemetry.instrumentation.dbapi.wrap_connect" )
126- @mock .patch ("mysql.connector.connect " )
226+ @mock .patch ("mysql.connector" )
127227 # pylint: disable=unused-argument
128- def test__instrument_enable_commenter (
228+ def test_instrument_enable_commenter_dbapi_kwargs (
129229 self ,
130230 mock_connect ,
131231 mock_wrap_connect ,
@@ -138,6 +238,112 @@ def test__instrument_enable_commenter(
138238 self .assertEqual (kwargs ["enable_commenter" ], True )
139239 self .assertEqual (kwargs ["commenter_options" ], {"foo" : True })
140240
241+ def test_instrument_with_dbapi_sqlcomment_enabled (
242+ self ,
243+ ):
244+ mock_connect_module = mock .MagicMock (
245+ __name__ = "mysql.connector" ,
246+ __version__ = "foobar" ,
247+ threadsafety = "123" ,
248+ apilevel = "123" ,
249+ paramstyle = "test" ,
250+ )
251+ mock_cursor = mock_connect_module .connect ().cursor ()
252+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
253+ mock_cursor = mock_connect_module .connect ().cursor ()
254+ mock_connection = mock .MagicMock ()
255+ mock_connection .cursor .return_value = mock_cursor
256+
257+ with mock .patch (
258+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
259+ mock_connect_module ,
260+ ):
261+ MySQLInstrumentor ()._instrument (
262+ enable_commenter = True ,
263+ )
264+ cnx = mock_connect_module .connect (database = "test" )
265+ cursor = cnx .cursor ()
266+ cursor .execute ("Select 1;" )
267+
268+ spans_list = self .memory_exporter .get_finished_spans ()
269+ span = spans_list [0 ]
270+ span_id = format (span .get_span_context ().span_id , "016x" )
271+ trace_id = format (span .get_span_context ().trace_id , "032x" )
272+ self .assertEqual (
273+ mock_cursor .execute .call_args [0 ][0 ],
274+ 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'*/;" ,
275+ )
276+
277+ def test_instrument_with_dbapi_sqlcomment_enabled_with_options (
278+ self ,
279+ ):
280+ mock_connect_module = mock .MagicMock (
281+ __name__ = "mysql.connector" ,
282+ __version__ = "foobar" ,
283+ threadsafety = "123" ,
284+ apilevel = "123" ,
285+ paramstyle = "test" ,
286+ )
287+ mock_cursor = mock_connect_module .connect ().cursor ()
288+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
289+ mock_cursor = mock_connect_module .connect ().cursor ()
290+ mock_connection = mock .MagicMock ()
291+ mock_connection .cursor .return_value = mock_cursor
292+
293+ with mock .patch (
294+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
295+ mock_connect_module ,
296+ ):
297+ MySQLInstrumentor ()._instrument (
298+ enable_commenter = True ,
299+ commenter_options = {
300+ "dbapi_level" : False ,
301+ "dbapi_threadsafety" : True ,
302+ "driver_paramstyle" : False ,
303+ },
304+ )
305+ cnx = mock_connect_module .connect (database = "test" )
306+ cursor = cnx .cursor ()
307+ cursor .execute ("Select 1;" )
308+
309+ spans_list = self .memory_exporter .get_finished_spans ()
310+ span = spans_list [0 ]
311+ span_id = format (span .get_span_context ().span_id , "016x" )
312+ trace_id = format (span .get_span_context ().trace_id , "032x" )
313+ self .assertEqual (
314+ mock_cursor .execute .call_args [0 ][0 ],
315+ f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{ trace_id } -{ span_id } -01'*/;" ,
316+ )
317+
318+ def test_instrument_with_dbapi_sqlcomment_not_enabled_default (
319+ self ,
320+ ):
321+ mock_connect_module = mock .MagicMock (
322+ __name__ = "mysql.connector" ,
323+ __version__ = "foobar" ,
324+ threadsafety = "123" ,
325+ apilevel = "123" ,
326+ paramstyle = "test" ,
327+ )
328+ mock_cursor = mock_connect_module .connect ().cursor ()
329+ mock_cursor ._cnx ._cmysql .get_client_info .return_value = "foobaz"
330+ mock_cursor = mock_connect_module .connect ().cursor ()
331+ mock_connection = mock .MagicMock ()
332+ mock_connection .cursor .return_value = mock_cursor
333+
334+ with mock .patch (
335+ "opentelemetry.instrumentation.mysql.mysql.connector" ,
336+ mock_connect_module ,
337+ ):
338+ MySQLInstrumentor ()._instrument ()
339+ cnx = mock_connect_module .connect (database = "test" )
340+ cursor = cnx .cursor ()
341+ cursor .execute ("Select 1;" )
342+ self .assertEqual (
343+ mock_cursor .execute .call_args [0 ][0 ],
344+ "Select 1;" ,
345+ )
346+
141347 @mock .patch ("mysql.connector.connect" )
142348 # pylint: disable=unused-argument
143349 def test_uninstrument_connection (self , mock_connect ):
0 commit comments