|
25 | 25 | MYSQL_PORT = int(os.getenv("MYSQL_PORT", "3306")) |
26 | 26 | MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME", "opentelemetry-tests") |
27 | 27 |
|
| 28 | + |
28 | 29 | class TestFunctionalMySqlCommenter(TestBase): |
29 | | - def setUp(self): |
30 | | - super().setUp() |
31 | | - self._tracer = self.tracer_provider.get_tracer(__name__) |
| 30 | + def test_commenter_enabled_direct_reference(self): |
32 | 31 | MySQLInstrumentor().instrument(enable_commenter=True) |
33 | | - self._connection = mysql.connector.connect( |
| 32 | + cnx = mysql.connector.connect( |
34 | 33 | user=MYSQL_USER, |
35 | 34 | password=MYSQL_PASSWORD, |
36 | 35 | host=MYSQL_HOST, |
37 | 36 | port=MYSQL_PORT, |
38 | 37 | database=MYSQL_DB_NAME, |
39 | 38 | ) |
40 | | - self._cursor = self._connection.cursor() |
| 39 | + cursor = cnx.cursor() |
41 | 40 |
|
42 | | - def tearDown(self): |
43 | | - self._cursor.close() |
44 | | - self._connection.close() |
| 41 | + cursor.execute("SELECT 1;") |
| 42 | + self.assertRegex( |
| 43 | + cursor.statement, |
| 44 | + r"SELECT 1 /\*db_driver='mysql\.connector[^']*',dbapi_level='\d\.\d',dbapi_threadsafety=\d,driver_paramstyle='[^']*',mysql_client_version='[^']*',traceparent='[^']*'\*/;", |
| 45 | + ) |
| 46 | + self.assertRegex( |
| 47 | + cursor.statement, r"mysql_client_version='(?!unknown)[^']+" |
| 48 | + ) |
| 49 | + |
| 50 | + cursor.close() |
| 51 | + cnx.close() |
45 | 52 | MySQLInstrumentor().uninstrument() |
46 | | - super().tearDown() |
47 | 53 |
|
48 | | - def test_commenter_enabled(self): |
49 | | - self._cursor.execute("SELECT 1;") |
| 54 | + def test_commenter_enabled_connection_proxy(self): |
| 55 | + cnx = mysql.connector.connect( |
| 56 | + user=MYSQL_USER, |
| 57 | + password=MYSQL_PASSWORD, |
| 58 | + host=MYSQL_HOST, |
| 59 | + port=MYSQL_PORT, |
| 60 | + database=MYSQL_DB_NAME, |
| 61 | + ) |
| 62 | + instrumented_cnx = MySQLInstrumentor().instrument_connection( |
| 63 | + connection=cnx, |
| 64 | + enable_commenter=True, |
| 65 | + ) |
| 66 | + cursor = instrumented_cnx.cursor() |
| 67 | + |
| 68 | + cursor.execute("SELECT 1;") |
| 69 | + self.assertRegex( |
| 70 | + cursor.statement, |
| 71 | + r"SELECT 1 /\*db_driver='mysql\.connector[^']*',dbapi_level='\d\.\d',dbapi_threadsafety=\d,driver_paramstyle='[^']*',mysql_client_version='[^']*',traceparent='[^']*'\*/;", |
| 72 | + ) |
50 | 73 | self.assertRegex( |
51 | | - self._cursor._query.query.decode("ascii"), |
52 | | - r"SELECT 1 /\*db_driver='mysql.connector(.*)',dbapi_level='\d.\d',dbapi_threadsafety=\d,driver_paramstyle=(.*),mysql_client_version=\d*,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;", |
| 74 | + cursor.statement, r"mysql_client_version='(?!unknown)[^']+" |
53 | 75 | ) |
| 76 | + |
| 77 | + cursor.close() |
| 78 | + MySQLInstrumentor().uninstrument_connection(instrumented_cnx) |
| 79 | + cnx.close() |
0 commit comments