From b75a9773281aaab57021e101b02d1dea4fba71a3 Mon Sep 17 00:00:00 2001 From: bharatarya Date: Tue, 17 Dec 2024 22:45:09 +0530 Subject: [PATCH 1/2] bug_fix(1477): type handling in _add_sql_comment --- .../instrumentation/sqlcommenter_utils.py | 2 ++ opentelemetry-instrumentation/tests/test_utils.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py index 1eeefbf206..f3a217fd10 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py @@ -22,6 +22,8 @@ def _add_sql_comment(sql, **meta) -> str: """ meta.update(**_add_framework_tags()) comment = _generate_sql_comment(**meta) + # converting to str to handle any type errors + sql = str(sql) sql = sql.rstrip() if sql[-1] == ";": sql = sql[:-1] + comment + ";" diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index 5ddd45d692..e61a0fa774 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -208,6 +208,21 @@ def test_add_sql_comments_without_comments(self): self.assertEqual(commented_sql_without_semicolon, "Select 1") + def test_psycopg2_sql_composable(self): + """Test handling of psycopg2.sql.Composable input""" + # Mock psycopg2.sql.Composable object + class MockComposable: + def __str__(self): + return "SELECT * FROM table_name" + + sql_query = MockComposable() + comments = {"trace_id": "abc123"} + + result = _add_sql_comment(sql_query, **comments) + expected = "SELECT * FROM table_name /*trace_id='abc123'*/" + + self.assertEqual(result, expected) + def test_is_instrumentation_enabled_by_default(self): self.assertTrue(is_instrumentation_enabled()) self.assertTrue(is_http_instrumentation_enabled()) From 03561c80fc291d97833dcf55d0cb4d13f11010cb Mon Sep 17 00:00:00 2001 From: bharatarya Date: Tue, 17 Dec 2024 22:45:09 +0530 Subject: [PATCH 2/2] bug_fix(1477): type handling in _add_sql_comment --- .../instrumentation/sqlcommenter_utils.py | 2 ++ opentelemetry-instrumentation/tests/test_utils.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py index 1eeefbf206..f3a217fd10 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py @@ -22,6 +22,8 @@ def _add_sql_comment(sql, **meta) -> str: """ meta.update(**_add_framework_tags()) comment = _generate_sql_comment(**meta) + # converting to str to handle any type errors + sql = str(sql) sql = sql.rstrip() if sql[-1] == ";": sql = sql[:-1] + comment + ";" diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index 5ddd45d692..e61a0fa774 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -208,6 +208,21 @@ def test_add_sql_comments_without_comments(self): self.assertEqual(commented_sql_without_semicolon, "Select 1") + def test_psycopg2_sql_composable(self): + """Test handling of psycopg2.sql.Composable input""" + # Mock psycopg2.sql.Composable object + class MockComposable: + def __str__(self): + return "SELECT * FROM table_name" + + sql_query = MockComposable() + comments = {"trace_id": "abc123"} + + result = _add_sql_comment(sql_query, **comments) + expected = "SELECT * FROM table_name /*trace_id='abc123'*/" + + self.assertEqual(result, expected) + def test_is_instrumentation_enabled_by_default(self): self.assertTrue(is_instrumentation_enabled()) self.assertTrue(is_http_instrumentation_enabled())