Skip to content

Commit

Permalink
bug_fix(1477): type handling in _add_sql_comment
Browse files Browse the repository at this point in the history
  • Loading branch information
aryabharat committed Jan 9, 2025
1 parent 59ecd4d commit b75a977
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ";"
Expand Down
15 changes: 15 additions & 0 deletions opentelemetry-instrumentation/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit b75a977

Please sign in to comment.