@@ -99,7 +99,7 @@ def test_instrument_connection(self, mock_connect):
9999 @mock .patch ("opentelemetry.instrumentation.dbapi.instrument_connection" )
100100 @mock .patch ("MySQLdb.connect" )
101101 # pylint: disable=unused-argument
102- def test_instrument_connection_enable_commenter (
102+ def test_instrument_connection_enable_commenter_dbapi_kwargs (
103103 self ,
104104 mock_connect ,
105105 mock_instrument_connection ,
@@ -111,15 +111,84 @@ def test_instrument_connection_enable_commenter(
111111 commenter_options = {"foo" : True },
112112 )
113113 cursor = cnx .cursor ()
114- cursor .execute ("SELECT * FROM test " )
114+ cursor .execute ("Select 1; " )
115115 kwargs = mock_instrument_connection .call_args [1 ]
116116 self .assertEqual (kwargs ["enable_commenter" ], True )
117117 self .assertEqual (kwargs ["commenter_options" ], {"foo" : True })
118118
119+ def test_instrument_connection_with_dbapi_sqlcomment_enabled (self ):
120+ mock_cursor = mock .MagicMock ()
121+ mock_cursor .execute = mock .MagicMock ()
122+ mock_connection = mock .MagicMock ()
123+ mock_connection .cursor .return_value = mock_cursor
124+
125+ mock_connect_module = mock .MagicMock ()
126+ mock_connect_module .__name__ = "MySQLdb"
127+ mock_connect_module .threadsafety = "123"
128+ mock_connect_module .apilevel = "123"
129+ mock_connect_module .paramstyle = "test"
130+ mock_connect_module ._mysql .get_client_info = mock .Mock (
131+ return_value = "foobaz"
132+ )
133+ mock_connect_module .connect = mock .Mock (return_value = mock_connection )
134+
135+ with mock .patch (
136+ "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
137+ mock_connect_module ,
138+ ), mock .patch (
139+ "opentelemetry.instrumentation.dbapi.util_version" ,
140+ return_value = "foobar" ,
141+ ):
142+ cnx_proxy = MySQLClientInstrumentor ().instrument_connection (
143+ mock_connection ,
144+ enable_commenter = True ,
145+ commenter_options = {"foo" : True },
146+ )
147+ cnx_proxy .cursor ().execute ("Select 1;" )
148+ self .assertRegex (
149+ mock_cursor .execute .call_args [0 ][0 ],
150+ 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}'\*/;" ,
151+ )
152+
153+ def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default (
154+ self ,
155+ ):
156+ mock_cursor = mock .MagicMock ()
157+ mock_cursor .execute = mock .MagicMock ()
158+ mock_connection = mock .MagicMock ()
159+ mock_connection .cursor .return_value = mock_cursor
160+
161+ mock_connect_module = mock .MagicMock ()
162+ mock_connect_module .__name__ = "MySQLdb"
163+ mock_connect_module .threadsafety = "123"
164+ mock_connect_module .apilevel = "123"
165+ mock_connect_module .paramstyle = "test"
166+ mock_connect_module ._mysql .get_client_info = mock .Mock (
167+ return_value = "foobaz"
168+ )
169+
170+ mock_connect_module .connect = mock .Mock (return_value = mock_connection )
171+
172+ with mock .patch (
173+ "opentelemetry.instrumentation.mysqlclient.MySQLdb" ,
174+ mock_connect_module ,
175+ ), mock .patch (
176+ "opentelemetry.instrumentation.dbapi.util_version" ,
177+ return_value = "foobar" ,
178+ ):
179+ cnx_proxy = MySQLClientInstrumentor ().instrument_connection (
180+ mock_connection ,
181+ )
182+ cnx_proxy .cursor ().execute ("Select 1;" )
183+ self .assertRegex (
184+ mock_cursor .execute .call_args [0 ][0 ],
185+ r"Select 1;" ,
186+ )
187+
119188 @mock .patch ("opentelemetry.instrumentation.dbapi.wrap_connect" )
120189 @mock .patch ("MySQLdb.connect" )
121190 # pylint: disable=unused-argument
122- def test__instrument_enable_commenter (
191+ def test__instrument_enable_commenter_dbapi_kwargs (
123192 self ,
124193 mock_connect ,
125194 mock_wrap_connect ,
0 commit comments