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