Skip to content

Commit

Permalink
Add mysql sqlcomment integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi committed Nov 28, 2024
1 parent 1cf2238 commit cb9a475
Showing 1 changed file with 222 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_instrumentor(self, mock_connect):

@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
@mock.patch("mysql.connector.connect")
def test_instrumentor_sqlcomment_enabled(
def test_instrumentor_sqlcomment_enabled_dbapi_kwargs(
self,
mock_connect,
mock_wrap_connect,
Expand All @@ -77,6 +77,118 @@ def test_instrumentor_sqlcomment_enabled(
self.assertEqual(kwargs["enable_commenter"], True)
self.assertEqual(kwargs["commenter_options"], {"foo": True})

def test_instrument_with_dbapi_sqlcomment_enabled(
self,
):
mock_connect_module = mock.MagicMock(
__name__="mysql.connector",
__version__="foobar",
threadsafety="123",
apilevel="123",
paramstyle="test",
)
mock_cursor = mock_connect_module.connect().cursor()
mock_cursor._cnx._cmysql.get_client_info.return_value = "foobaz"
mock_connection = mock.MagicMock()
mock_connection.cursor.return_value = mock_cursor

with mock.patch(
"opentelemetry.instrumentation.mysql.mysql.connector",
mock_connect_module,
), mock.patch(
"opentelemetry.instrumentation.dbapi.util_version",
return_value="foobar",
):
MySQLInstrumentor()._instrument(
enable_commenter=True,
)
cnx = mock_connect_module.connect(database="test")
cursor = cnx.cursor()
cursor.execute("Select 1;")

spans_list = self.memory_exporter.get_finished_spans()
span = spans_list[0]
span_id = format(span.get_span_context().span_id, "016x")
trace_id = format(span.get_span_context().trace_id, "032x")
self.assertEqual(
mock_cursor.execute.call_args[0][0],
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
)

def test_instrument_with_dbapi_sqlcomment_enabled_with_options(
self,
):
mock_connect_module = mock.MagicMock(
__name__="mysql.connector",
__version__="foobar",
threadsafety="123",
apilevel="123",
paramstyle="test",
)
mock_cursor = mock_connect_module.connect().cursor()
mock_cursor._cnx._cmysql.get_client_info.return_value = "foobaz"
mock_connection = mock.MagicMock()
mock_connection.cursor.return_value = mock_cursor

with mock.patch(
"opentelemetry.instrumentation.mysql.mysql.connector",
mock_connect_module,
), mock.patch(
"opentelemetry.instrumentation.dbapi.util_version",
return_value="foobar",
):
MySQLInstrumentor()._instrument(
enable_commenter=True,
commenter_options={
"dbapi_level": False,
"dbapi_threadsafety": True,
"driver_paramstyle": False,
},
)
cnx = mock_connect_module.connect(database="test")
cursor = cnx.cursor()
cursor.execute("Select 1;")

spans_list = self.memory_exporter.get_finished_spans()
span = spans_list[0]
span_id = format(span.get_span_context().span_id, "016x")
trace_id = format(span.get_span_context().trace_id, "032x")
self.assertEqual(
mock_cursor.execute.call_args[0][0],
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
)

def test_instrument_with_dbapi_sqlcomment_not_enabled_default(
self,
):
mock_connect_module = mock.MagicMock(
__name__="mysql.connector",
__version__="foobar",
threadsafety="123",
apilevel="123",
paramstyle="test",
)
mock_cursor = mock_connect_module.connect().cursor()
mock_cursor._cnx._cmysql.get_client_info.return_value = "foobaz"
mock_connection = mock.MagicMock()
mock_connection.cursor.return_value = mock_cursor

with mock.patch(
"opentelemetry.instrumentation.mysql.mysql.connector",
mock_connect_module,
), mock.patch(
"opentelemetry.instrumentation.dbapi.util_version",
return_value="foobar",
):
MySQLInstrumentor()._instrument()
cnx = mock_connect_module.connect(database="test")
cursor = cnx.cursor()
cursor.execute("Select 1;")
self.assertEqual(
mock_cursor.execute.call_args[0][0],
"Select 1;",
)

@mock.patch("mysql.connector.connect")
# pylint: disable=unused-argument
def test_custom_tracer_provider(self, mock_connect):
Expand Down Expand Up @@ -120,7 +232,7 @@ def test_instrument_connection_no_op_tracer_provider(self, mock_connect):
@mock.patch("opentelemetry.instrumentation.mysql.DatabaseApiIntegration")
@mock.patch("mysql.connector.connect")
# pylint: disable=unused-argument
def test_instrument_connection_enable_commenter(
def test_instrument_connection_enable_commenter_dbapi_kwargs(
self,
mock_connect,
mock_mysql_dbapi,
Expand All @@ -137,6 +249,114 @@ def test_instrument_connection_enable_commenter(
self.assertEqual(kwargs["enable_commenter"], True)
self.assertEqual(kwargs["commenter_options"], {"foo": True})

def test_instrument_connection_with_dbapi_sqlcomment_enabled(self):
mock_connect_module = mock.MagicMock(
__name__="mysql.connector",
__version__="foobar",
threadsafety="123",
apilevel="123",
paramstyle="test",
)
mock_cursor = mock_connect_module.connect().cursor()
mock_cursor._cnx._cmysql.get_client_info.return_value = "foobaz"
mock_connection = mock.MagicMock()
mock_connection.cursor.return_value = mock_cursor

with mock.patch(
"opentelemetry.instrumentation.mysql.mysql.connector",
mock_connect_module,
), mock.patch(
"opentelemetry.instrumentation.dbapi.util_version",
return_value="foobar",
):
cnx_proxy = MySQLInstrumentor().instrument_connection(
mock_connection,
enable_commenter=True,
)
cnx_proxy.cursor().execute("Select 1;")

spans_list = self.memory_exporter.get_finished_spans()
span = spans_list[0]
span_id = format(span.get_span_context().span_id, "016x")
trace_id = format(span.get_span_context().trace_id, "032x")
self.assertEqual(
mock_cursor.execute.call_args[0][0],
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_level='123',dbapi_threadsafety='123',driver_paramstyle='test',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
)

def test_instrument_connection_with_dbapi_sqlcomment_enabled_with_options(
self,
):
mock_connect_module = mock.MagicMock(
__name__="mysql.connector",
__version__="foobar",
threadsafety="123",
apilevel="123",
paramstyle="test",
)
mock_cursor = mock_connect_module.connect().cursor()
mock_cursor._cnx._cmysql.get_client_info.return_value = "foobaz"
mock_connection = mock.MagicMock()
mock_connection.cursor.return_value = mock_cursor

with mock.patch(
"opentelemetry.instrumentation.mysql.mysql.connector",
mock_connect_module,
), mock.patch(
"opentelemetry.instrumentation.dbapi.util_version",
return_value="foobar",
):
cnx_proxy = MySQLInstrumentor().instrument_connection(
mock_connection,
enable_commenter=True,
commenter_options={
"dbapi_level": False,
"dbapi_threadsafety": True,
"driver_paramstyle": False,
},
)
cnx_proxy.cursor().execute("Select 1;")

spans_list = self.memory_exporter.get_finished_spans()
span = spans_list[0]
span_id = format(span.get_span_context().span_id, "016x")
trace_id = format(span.get_span_context().trace_id, "032x")
self.assertEqual(
mock_cursor.execute.call_args[0][0],
f"Select 1 /*db_driver='mysql.connector%%3Afoobar',dbapi_threadsafety='123',mysql_client_version='foobaz',traceparent='00-{trace_id}-{span_id}-01'*/;",
)

def test_instrument_connection_with_dbapi_sqlcomment_not_enabled_default(
self,
):
mock_connect_module = mock.MagicMock(
__name__="mysql.connector",
__version__="foobar",
threadsafety="123",
apilevel="123",
paramstyle="test",
)
mock_cursor = mock_connect_module.connect().cursor()
mock_cursor._cnx._cmysql.get_client_info.return_value = "foobaz"
mock_connection = mock.MagicMock()
mock_connection.cursor.return_value = mock_cursor

with mock.patch(
"opentelemetry.instrumentation.mysql.mysql.connector",
mock_connect_module,
), mock.patch(
"opentelemetry.instrumentation.dbapi.util_version",
return_value="foobar",
):
cnx_proxy = MySQLInstrumentor().instrument_connection(
mock_connection,
)
cnx_proxy.cursor().execute("Select 1;")
self.assertEqual(
mock_cursor.execute.call_args[0][0],
"Select 1;",
)

@mock.patch("mysql.connector.connect")
# pylint: disable=unused-argument
def test_uninstrument_connection(self, mock_connect):
Expand Down

0 comments on commit cb9a475

Please sign in to comment.