Skip to content

Commit ba3b51a

Browse files
committed
bug_fix(1477): type handling in _add_sql_comment
1 parent 86a7f6b commit ba3b51a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Diff for: opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def _add_sql_comment(sql, **meta) -> str:
2222
"""
2323
meta.update(**_add_framework_tags())
2424
comment = _generate_sql_comment(**meta)
25+
# converting to str to handle any type errors
26+
sql = str(sql)
2527
sql = sql.rstrip()
2628
if sql[-1] == ";":
2729
sql = sql[:-1] + comment + ";"

Diff for: opentelemetry-instrumentation/tests/test_utils.py

+15
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@ def test_add_sql_comments_without_comments(self):
208208

209209
self.assertEqual(commented_sql_without_semicolon, "Select 1")
210210

211+
def test_psycopg2_sql_composable(self):
212+
"""Test handling of psycopg2.sql.Composable input"""
213+
# Mock psycopg2.sql.Composable object
214+
class MockComposable:
215+
def __str__(self):
216+
return "SELECT * FROM table_name"
217+
218+
sql_query = MockComposable()
219+
comments = {"trace_id": "abc123"}
220+
221+
result = _add_sql_comment(sql_query, **comments)
222+
expected = "SELECT * FROM table_name /*trace_id='abc123'*/"
223+
224+
self.assertEqual(result, expected)
225+
211226
def test_is_instrumentation_enabled_by_default(self):
212227
self.assertTrue(is_instrumentation_enabled())
213228
self.assertTrue(is_http_instrumentation_enabled())

0 commit comments

Comments
 (0)