|
| 1 | +# Copyright 2025, OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +import mysql.connector |
| 18 | + |
| 19 | +from opentelemetry.instrumentation.mysql import MySQLInstrumentor |
| 20 | +from opentelemetry.test.test_base import TestBase |
| 21 | + |
| 22 | +MYSQL_USER = os.getenv("MYSQL_USER", "testuser") |
| 23 | +MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD", "testpassword") |
| 24 | +MYSQL_HOST = os.getenv("MYSQL_HOST", "localhost") |
| 25 | +MYSQL_PORT = int(os.getenv("MYSQL_PORT", "3306")) |
| 26 | +MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME", "opentelemetry-tests") |
| 27 | + |
| 28 | +class TestFunctionalMySqlCommenter(TestBase): |
| 29 | + def setUp(self): |
| 30 | + super().setUp() |
| 31 | + self._tracer = self.tracer_provider.get_tracer(__name__) |
| 32 | + MySQLInstrumentor().instrument(enable_commenter=True) |
| 33 | + self._connection = mysql.connector.connect( |
| 34 | + user=MYSQL_USER, |
| 35 | + password=MYSQL_PASSWORD, |
| 36 | + host=MYSQL_HOST, |
| 37 | + port=MYSQL_PORT, |
| 38 | + database=MYSQL_DB_NAME, |
| 39 | + ) |
| 40 | + self._cursor = self._connection.cursor() |
| 41 | + |
| 42 | + def tearDown(self): |
| 43 | + self._cursor.close() |
| 44 | + self._connection.close() |
| 45 | + MySQLInstrumentor().uninstrument() |
| 46 | + super().tearDown() |
| 47 | + |
| 48 | + def test_commenter_enabled(self): |
| 49 | + self._cursor.execute("SELECT 1;") |
| 50 | + 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}'\*/;", |
| 53 | + ) |
0 commit comments