@@ -201,6 +201,79 @@ def test__instrument_enable_commenter_dbapi_kwargs(
201201 self .assertEqual (kwargs ["enable_commenter" ], True )
202202 self .assertEqual (kwargs ["commenter_options" ], {"foo" : True })
203203
204+ def test__instrument_with_dbapi_sqlcomment_enabled (
205+ self ,
206+ ):
207+ mock_cursor = mock .MagicMock ()
208+ mock_cursor .execute = mock .MagicMock ()
209+ mock_connection = mock .MagicMock ()
210+ mock_connection .cursor .return_value = mock_cursor
211+
212+ mock_connect_module = mock .Mock ()
213+ mock_connect_module .__name__ = "MySQLdb"
214+ mock_connect_module .threadsafety = "123"
215+ mock_connect_module .apilevel = "123"
216+ mock_connect_module .paramstyle = "test"
217+ mock_connect_module ._mysql .get_client_info = mock .Mock (
218+ return_value = "foobaz"
219+ )
220+
221+ mock_connect_module .connect = mock .Mock (return_value = mock_connection )
222+
223+ with mock .patch (
224+ "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
225+ mock_connect_module ,
226+ ), mock .patch (
227+ "opentelemetry.instrumentation.dbapi.util_version" ,
228+ return_value = "foobar" ,
229+ ):
230+ MySQLClientInstrumentor ()._instrument (
231+ enable_commenter = True ,
232+ commenter_options = {"foo" : True },
233+ )
234+ cnx = mock_connect_module .connect (database = "test" )
235+ cursor = cnx .cursor ()
236+ cursor .execute ("Select 1;" )
237+ self .assertRegex (
238+ mock_cursor .execute .call_args [0 ][0 ],
239+ r"Select 1 /\*db_driver='MySQLdb%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
240+ )
241+
242+ def test__instrument_with_dbapi_sqlcomment_not_enabled_default (
243+ self ,
244+ ):
245+ mock_cursor = mock .MagicMock ()
246+ mock_cursor .execute = mock .MagicMock ()
247+ mock_connection = mock .MagicMock ()
248+ mock_connection .cursor .return_value = mock_cursor
249+
250+ mock_connect_module = mock .Mock ()
251+ mock_connect_module .__name__ = "MySQLdb"
252+ mock_connect_module .threadsafety = "123"
253+ mock_connect_module .apilevel = "123"
254+ mock_connect_module .paramstyle = "test"
255+ mock_connect_module ._mysql .get_client_info = mock .Mock (
256+ return_value = "foobaz"
257+ )
258+
259+ mock_connect_module .connect = mock .Mock (return_value = mock_connection )
260+
261+ with mock .patch (
262+ "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
263+ mock_connect_module ,
264+ ), mock .patch (
265+ "opentelemetry.instrumentation.dbapi.util_version" ,
266+ return_value = "foobar" ,
267+ ):
268+ MySQLClientInstrumentor ()._instrument ()
269+ cnx = mock_connect_module .connect (database = "test" )
270+ cursor = cnx .cursor ()
271+ cursor .execute ("Select 1;" )
272+ self .assertRegex (
273+ mock_cursor .execute .call_args [0 ][0 ],
274+ r"Select 1;" ,
275+ )
276+
204277 @mock .patch ("MySQLdb.connect" )
205278 # pylint: disable=unused-argument
206279 def test_uninstrument_connection (self , mock_connect ):
0 commit comments