|
23 | 23 |
|
24 | 24 |
|
25 | 25 | class TestMySQLClientIntegration(TestBase):
|
| 26 | + # pylint: disable=invalid-name |
26 | 27 | def tearDown(self):
|
27 | 28 | super().tearDown()
|
28 | 29 | with self.disable_logging():
|
@@ -96,6 +97,256 @@ def test_instrument_connection(self, mock_connect):
|
96 | 97 | spans_list = self.memory_exporter.get_finished_spans()
|
97 | 98 | self.assertEqual(len(spans_list), 1)
|
98 | 99 |
|
| 100 | + @mock.patch("opentelemetry.instrumentation.dbapi.instrument_connection") |
| 101 | + @mock.patch("MySQLdb.connect") |
| 102 | + # pylint: disable=unused-argument |
| 103 | + def test_instrument_connection_enable_commenter_dbapi_kwargs( |
| 104 | + self, |
| 105 | + mock_connect, |
| 106 | + mock_instrument_connection, |
| 107 | + ): |
| 108 | + cnx = MySQLdb.connect(database="test") |
| 109 | + cnx = MySQLClientInstrumentor().instrument_connection( |
| 110 | + cnx, |
| 111 | + enable_commenter=True, |
| 112 | + commenter_options={"foo": True}, |
| 113 | + ) |
| 114 | + cursor = cnx.cursor() |
| 115 | + cursor.execute("Select 1;") |
| 116 | + kwargs = mock_instrument_connection.call_args[1] |
| 117 | + self.assertEqual(kwargs["enable_commenter"], True) |
| 118 | + self.assertEqual(kwargs["commenter_options"], {"foo": True}) |
| 119 | + |
| 120 | + def test_instrument_connection_with_dbapi_sqlcomment_enabled(self): |
| 121 | + mock_connect_module = mock.MagicMock( |
| 122 | + __name__="MySQLdb", |
| 123 | + threadsafety="123", |
| 124 | + apilevel="123", |
| 125 | + paramstyle="test", |
| 126 | + ) |
| 127 | + mock_connect_module._mysql.get_client_info.return_value = "foobaz" |
| 128 | + mock_cursor = mock_connect_module.connect().cursor() |
| 129 | + mock_connection = mock.MagicMock() |
| 130 | + mock_connection.cursor.return_value = mock_cursor |
| 131 | + |
| 132 | + with mock.patch( |
| 133 | + "opentelemetry.instrumentation.mysqlclient.MySQLdb", |
| 134 | + mock_connect_module, |
| 135 | + ), mock.patch( |
| 136 | + "opentelemetry.instrumentation.dbapi.util_version", |
| 137 | + return_value="foobar", |
| 138 | + ): |
| 139 | + cnx_proxy = MySQLClientInstrumentor().instrument_connection( |
| 140 | + mock_connection, |
| 141 | + enable_commenter=True, |
| 142 | + ) |
| 143 | + cnx_proxy.cursor().execute("Select 1;") |
| 144 | + |
| 145 | + spans_list = self.memory_exporter.get_finished_spans() |
| 146 | + span = spans_list[0] |
| 147 | + span_id = format(span.get_span_context().span_id, "016x") |
| 148 | + trace_id = format(span.get_span_context().trace_id, "032x") |
| 149 | + self.assertEqual( |
| 150 | + mock_cursor.execute.call_args[0][0], |
| 151 | + f"Select 1 /*db_driver='MySQLdb%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;", |
| 152 | + ) |
| 153 | + |
| 154 | + def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options( |
| 155 | + self, |
| 156 | + ): |
| 157 | + mock_connect_module = mock.MagicMock( |
| 158 | + __name__="MySQLdb", |
| 159 | + threadsafety="123", |
| 160 | + apilevel="123", |
| 161 | + paramstyle="test", |
| 162 | + ) |
| 163 | + mock_connect_module._mysql.get_client_info.return_value = "foobaz" |
| 164 | + mock_cursor = mock_connect_module.connect().cursor() |
| 165 | + mock_connection = mock.MagicMock() |
| 166 | + mock_connection.cursor.return_value = mock_cursor |
| 167 | + |
| 168 | + with mock.patch( |
| 169 | + "opentelemetry.instrumentation.mysqlclient.MySQLdb", |
| 170 | + mock_connect_module, |
| 171 | + ), mock.patch( |
| 172 | + "opentelemetry.instrumentation.dbapi.util_version", |
| 173 | + return_value="foobar", |
| 174 | + ): |
| 175 | + cnx_proxy = MySQLClientInstrumentor().instrument_connection( |
| 176 | + mock_connection, |
| 177 | + enable_commenter=True, |
| 178 | + commenter_options={ |
| 179 | + "dbapi_level": False, |
| 180 | + "dbapi_threadsafety": True, |
| 181 | + "driver_paramstyle": False, |
| 182 | + }, |
| 183 | + ) |
| 184 | + cnx_proxy.cursor().execute("Select 1;") |
| 185 | + |
| 186 | + spans_list = self.memory_exporter.get_finished_spans() |
| 187 | + span = spans_list[0] |
| 188 | + span_id = format(span.get_span_context().span_id, "016x") |
| 189 | + trace_id = format(span.get_span_context().trace_id, "032x") |
| 190 | + self.assertEqual( |
| 191 | + mock_cursor.execute.call_args[0][0], |
| 192 | + f"Select 1 /*db_driver='MySQLdb%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;", |
| 193 | + ) |
| 194 | + |
| 195 | + def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default( |
| 196 | + self, |
| 197 | + ): |
| 198 | + mock_connect_module = mock.MagicMock( |
| 199 | + __name__="MySQLdb", |
| 200 | + threadsafety="123", |
| 201 | + apilevel="123", |
| 202 | + paramstyle="test", |
| 203 | + ) |
| 204 | + mock_connect_module._mysql.get_client_info.return_value = "foobaz" |
| 205 | + mock_cursor = mock_connect_module.connect().cursor() |
| 206 | + mock_connection = mock.MagicMock() |
| 207 | + mock_connection.cursor.return_value = mock_cursor |
| 208 | + |
| 209 | + with mock.patch( |
| 210 | + "opentelemetry.instrumentation.mysqlclient.MySQLdb", |
| 211 | + mock_connect_module, |
| 212 | + ), mock.patch( |
| 213 | + "opentelemetry.instrumentation.dbapi.util_version", |
| 214 | + return_value="foobar", |
| 215 | + ): |
| 216 | + cnx_proxy = MySQLClientInstrumentor().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 | + |
| 225 | + @mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect") |
| 226 | + @mock.patch("MySQLdb.connect") |
| 227 | + # pylint: disable=unused-argument |
| 228 | + def test_instrument_enable_commenter_dbapi_kwargs( |
| 229 | + self, |
| 230 | + mock_connect, |
| 231 | + mock_wrap_connect, |
| 232 | + ): |
| 233 | + MySQLClientInstrumentor()._instrument( |
| 234 | + enable_commenter=True, |
| 235 | + commenter_options={"foo": True}, |
| 236 | + ) |
| 237 | + kwargs = mock_wrap_connect.call_args[1] |
| 238 | + self.assertEqual(kwargs["enable_commenter"], True) |
| 239 | + self.assertEqual(kwargs["commenter_options"], {"foo": True}) |
| 240 | + |
| 241 | + def test_instrument_with_dbapi_sqlcomment_enabled( |
| 242 | + self, |
| 243 | + ): |
| 244 | + mock_connect_module = mock.MagicMock( |
| 245 | + __name__="MySQLdb", |
| 246 | + threadsafety="123", |
| 247 | + apilevel="123", |
| 248 | + paramstyle="test", |
| 249 | + ) |
| 250 | + mock_connect_module._mysql.get_client_info.return_value = "foobaz" |
| 251 | + mock_cursor = mock_connect_module.connect().cursor() |
| 252 | + mock_connection = mock.MagicMock() |
| 253 | + mock_connection.cursor.return_value = mock_cursor |
| 254 | + |
| 255 | + with mock.patch( |
| 256 | + "opentelemetry.instrumentation.mysqlclient.MySQLdb", |
| 257 | + mock_connect_module, |
| 258 | + ), mock.patch( |
| 259 | + "opentelemetry.instrumentation.dbapi.util_version", |
| 260 | + return_value="foobar", |
| 261 | + ): |
| 262 | + MySQLClientInstrumentor()._instrument( |
| 263 | + enable_commenter=True, |
| 264 | + ) |
| 265 | + cnx = mock_connect_module.connect(database="test") |
| 266 | + cursor = cnx.cursor() |
| 267 | + cursor.execute("Select 1;") |
| 268 | + |
| 269 | + spans_list = self.memory_exporter.get_finished_spans() |
| 270 | + span = spans_list[0] |
| 271 | + span_id = format(span.get_span_context().span_id, "016x") |
| 272 | + trace_id = format(span.get_span_context().trace_id, "032x") |
| 273 | + self.assertEqual( |
| 274 | + mock_cursor.execute.call_args[0][0], |
| 275 | + f"Select 1 /*db_driver='MySQLdb%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;", |
| 276 | + ) |
| 277 | + |
| 278 | + def test_instrument_with_dbapi_sqlcomment_enabled_with_options( |
| 279 | + self, |
| 280 | + ): |
| 281 | + mock_connect_module = mock.MagicMock( |
| 282 | + __name__="MySQLdb", |
| 283 | + threadsafety="123", |
| 284 | + apilevel="123", |
| 285 | + paramstyle="test", |
| 286 | + ) |
| 287 | + mock_connect_module._mysql.get_client_info.return_value = "foobaz" |
| 288 | + mock_cursor = mock_connect_module.connect().cursor() |
| 289 | + mock_connection = mock.MagicMock() |
| 290 | + mock_connection.cursor.return_value = mock_cursor |
| 291 | + |
| 292 | + with mock.patch( |
| 293 | + "opentelemetry.instrumentation.mysqlclient.MySQLdb", |
| 294 | + mock_connect_module, |
| 295 | + ), mock.patch( |
| 296 | + "opentelemetry.instrumentation.dbapi.util_version", |
| 297 | + return_value="foobar", |
| 298 | + ): |
| 299 | + MySQLClientInstrumentor()._instrument( |
| 300 | + enable_commenter=True, |
| 301 | + commenter_options={ |
| 302 | + "dbapi_level": False, |
| 303 | + "dbapi_threadsafety": True, |
| 304 | + "driver_paramstyle": False, |
| 305 | + }, |
| 306 | + ) |
| 307 | + cnx = mock_connect_module.connect(database="test") |
| 308 | + cursor = cnx.cursor() |
| 309 | + cursor.execute("Select 1;") |
| 310 | + |
| 311 | + spans_list = self.memory_exporter.get_finished_spans() |
| 312 | + span = spans_list[0] |
| 313 | + span_id = format(span.get_span_context().span_id, "016x") |
| 314 | + trace_id = format(span.get_span_context().trace_id, "032x") |
| 315 | + self.assertEqual( |
| 316 | + mock_cursor.execute.call_args[0][0], |
| 317 | + f"Select 1 /*db_driver='MySQLdb%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;", |
| 318 | + ) |
| 319 | + |
| 320 | + def test_instrument_with_dbapi_sqlcomment_not_enabled_default( |
| 321 | + self, |
| 322 | + ): |
| 323 | + mock_connect_module = mock.MagicMock( |
| 324 | + __name__="MySQLdb", |
| 325 | + threadsafety="123", |
| 326 | + apilevel="123", |
| 327 | + paramstyle="test", |
| 328 | + ) |
| 329 | + mock_connect_module._mysql.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.mysqlclient.MySQLdb", |
| 336 | + mock_connect_module, |
| 337 | + ), mock.patch( |
| 338 | + "opentelemetry.instrumentation.dbapi.util_version", |
| 339 | + return_value="foobar", |
| 340 | + ): |
| 341 | + MySQLClientInstrumentor()._instrument() |
| 342 | + cnx = mock_connect_module.connect(database="test") |
| 343 | + cursor = cnx.cursor() |
| 344 | + cursor.execute("Select 1;") |
| 345 | + self.assertEqual( |
| 346 | + mock_cursor.execute.call_args[0][0], |
| 347 | + "Select 1;", |
| 348 | + ) |
| 349 | + |
99 | 350 | @mock.patch("MySQLdb.connect")
|
100 | 351 | # pylint: disable=unused-argument
|
101 | 352 | def test_uninstrument_connection(self, mock_connect):
|
|
0 commit comments