Add DB-API instrumentor support for MySQL driver sqlcommenting (#2897)

This commit is contained in:
Tammy Baylis
2024-10-30 10:46:11 -07:00
committed by GitHub
parent f6b68d0c02
commit a940fc4a65
3 changed files with 275 additions and 17 deletions

View File

@ -24,6 +24,7 @@ from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.test_base import TestBase
# pylint: disable=too-many-public-methods
class TestDBApiIntegration(TestBase):
def setUp(self):
super().setUp()
@ -252,6 +253,7 @@ class TestDBApiIntegration(TestBase):
def test_executemany_comment(self):
connect_module = mock.MagicMock()
connect_module.__name__ = "test"
connect_module.__version__ = mock.MagicMock()
connect_module.__libpq_version__ = 123
connect_module.apilevel = 123
@ -260,7 +262,7 @@ class TestDBApiIntegration(TestBase):
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"testcomponent",
"postgresql",
enable_commenter=True,
commenter_options={"db_driver": False, "dbapi_level": False},
connect_module=connect_module,
@ -275,8 +277,38 @@ class TestDBApiIntegration(TestBase):
r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)
def test_executemany_comment_non_pep_249_compliant(self):
class MockConnectModule:
def __getattr__(self, name):
if name == "__name__":
return "test"
if name == "__version__":
return mock.MagicMock()
if name == "__libpq_version__":
return 123
raise AttributeError("attribute missing")
connect_module = MockConnectModule()
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"postgresql",
enable_commenter=True,
connect_module=connect_module,
commenter_options={"db_driver": False},
)
mock_connection = db_integration.wrapped_connection(
mock_connect, {}, {}
)
cursor = mock_connection.cursor()
cursor.executemany("Select 1;")
self.assertRegex(
cursor.query,
r"Select 1 /\*dbapi_level='1.0',dbapi_threadsafety='unknown',driver_paramstyle='unknown',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)
def test_compatible_build_version_psycopg_psycopg2_libpq(self):
connect_module = mock.MagicMock()
connect_module.__name__ = "test"
connect_module.__version__ = mock.MagicMock()
connect_module.pq = mock.MagicMock()
connect_module.pq.__build_version__ = 123
@ -286,7 +318,7 @@ class TestDBApiIntegration(TestBase):
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"testcomponent",
"postgresql",
enable_commenter=True,
commenter_options={"db_driver": False, "dbapi_level": False},
connect_module=connect_module,
@ -301,8 +333,150 @@ class TestDBApiIntegration(TestBase):
r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)
def test_executemany_psycopg2_integration_comment(self):
connect_module = mock.MagicMock()
connect_module.__name__ = "psycopg2"
connect_module.__version__ = "1.2.3"
connect_module.__libpq_version__ = 123
connect_module.apilevel = 123
connect_module.threadsafety = 123
connect_module.paramstyle = "test"
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"postgresql",
enable_commenter=True,
commenter_options={"db_driver": True, "dbapi_level": False},
connect_module=connect_module,
)
mock_connection = db_integration.wrapped_connection(
mock_connect, {}, {}
)
cursor = mock_connection.cursor()
cursor.executemany("Select 1;")
self.assertRegex(
cursor.query,
r"Select 1 /\*db_driver='psycopg2%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)
def test_executemany_psycopg_integration_comment(self):
connect_module = mock.MagicMock()
connect_module.__name__ = "psycopg"
connect_module.__version__ = "1.2.3"
connect_module.pq = mock.MagicMock()
connect_module.pq.__build_version__ = 123
connect_module.apilevel = 123
connect_module.threadsafety = 123
connect_module.paramstyle = "test"
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"postgresql",
enable_commenter=True,
commenter_options={"db_driver": True, "dbapi_level": False},
connect_module=connect_module,
)
mock_connection = db_integration.wrapped_connection(
mock_connect, {}, {}
)
cursor = mock_connection.cursor()
cursor.executemany("Select 1;")
self.assertRegex(
cursor.query,
r"Select 1 /\*db_driver='psycopg%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)
def test_executemany_mysqlconnector_integration_comment(self):
connect_module = mock.MagicMock()
connect_module.__name__ = "mysql.connector"
connect_module.__version__ = "1.2.3"
connect_module.apilevel = 123
connect_module.threadsafety = 123
connect_module.paramstyle = "test"
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"mysql",
enable_commenter=True,
commenter_options={"db_driver": True, "dbapi_level": False},
connect_module=connect_module,
)
mock_connection = db_integration.wrapped_connection(
mock_connect, {}, {}
)
cursor = mock_connection.cursor()
cursor.executemany("Select 1;")
self.assertRegex(
cursor.query,
r"Select 1 /\*db_driver='mysql.connector%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',mysql_client_version='1.2.3',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)
@mock.patch("opentelemetry.instrumentation.dbapi.util_version")
def test_executemany_mysqlclient_integration_comment(
self,
mock_dbapi_util_version,
):
mock_dbapi_util_version.return_value = "1.2.3"
connect_module = mock.MagicMock()
connect_module.__name__ = "MySQLdb"
connect_module.__version__ = "1.2.3"
connect_module.apilevel = 123
connect_module.threadsafety = 123
connect_module.paramstyle = "test"
connect_module._mysql = mock.MagicMock()
connect_module._mysql.get_client_info = mock.MagicMock(
return_value="123"
)
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"mysql",
enable_commenter=True,
commenter_options={"db_driver": True, "dbapi_level": False},
connect_module=connect_module,
)
mock_connection = db_integration.wrapped_connection(
mock_connect, {}, {}
)
cursor = mock_connection.cursor()
cursor.executemany("Select 1;")
self.assertRegex(
cursor.query,
r"Select 1 /\*db_driver='MySQLdb%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',mysql_client_version='123',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)
def test_executemany_pymysql_integration_comment(self):
connect_module = mock.MagicMock()
connect_module.__name__ = "pymysql"
connect_module.__version__ = "1.2.3"
connect_module.apilevel = 123
connect_module.threadsafety = 123
connect_module.paramstyle = "test"
connect_module.get_client_info = mock.MagicMock(return_value="123")
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"mysql",
enable_commenter=True,
commenter_options={"db_driver": True, "dbapi_level": False},
connect_module=connect_module,
)
mock_connection = db_integration.wrapped_connection(
mock_connect, {}, {}
)
cursor = mock_connection.cursor()
cursor.executemany("Select 1;")
self.assertRegex(
cursor.query,
r"Select 1 /\*db_driver='pymysql%%3A1.2.3',dbapi_threadsafety=123,driver_paramstyle='test',mysql_client_version='123',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)
def test_executemany_flask_integration_comment(self):
connect_module = mock.MagicMock()
connect_module.__name__ = "test"
connect_module.__version__ = mock.MagicMock()
connect_module.__libpq_version__ = 123
connect_module.apilevel = 123
@ -311,7 +485,7 @@ class TestDBApiIntegration(TestBase):
db_integration = dbapi.DatabaseApiIntegration(
"testname",
"testcomponent",
"postgresql",
enable_commenter=True,
commenter_options={"db_driver": False, "dbapi_level": False},
connect_module=connect_module,
@ -332,6 +506,11 @@ class TestDBApiIntegration(TestBase):
r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',flask=1,libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
)
clear_context = context.set_value(
"SQLCOMMENTER_ORM_TAGS_AND_VALUES", {}, current_context
)
context.attach(clear_context)
def test_callproc(self):
db_integration = dbapi.DatabaseApiIntegration(
"testname", "testcomponent"
@ -415,6 +594,12 @@ class MockCursor:
def __init__(self) -> None:
self.query = ""
self.params = None
# Mock mysql.connector modules and method
self._cnx = mock.MagicMock()
self._cnx._cmysql = mock.MagicMock()
self._cnx._cmysql.get_client_info = mock.MagicMock(
return_value="1.2.3"
)
# pylint: disable=unused-argument, no-self-use
def execute(self, query, params=None, throw_exception=False):