Skip to content

Commit 3c88163

Browse files
selcukxrmx
andauthored
Handle empty strings in sqlcommenter (#3309)
* Handle empty strings Safer check for a trailing semicolon that could handle empty strings as well. * Updated CHANGELOG.md * Test for empty SQL strings in sqlcommenter --------- Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent f96d14c commit 3c88163

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4646
([#3249](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3249))
4747
- `opentelemetry-instrumentation-asyncpg` Fix fallback for empty queries.
4848
([#3253](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3253))
49+
- `opentelemetry-instrumentation` Fix a traceback in sqlcommenter when psycopg connection pooling is enabled.
50+
([#3309](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3309))
4951
- `opentelemetry-instrumentation-threading` Fix broken context typehints
5052
([#3322](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3322))
5153
- `opentelemetry-instrumentation-requests` always record span status code in duration metric

instrumentation/opentelemetry-instrumentation-django/tests/test_sqlcommenter.py

+28
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,31 @@ def test_multiple_connection_support(self, query_wrapper):
146146

147147
# check if query_wrapper is added to the context for 2 databases
148148
self.assertEqual(query_wrapper.call_count, 2)
149+
150+
@patch(
151+
"opentelemetry.instrumentation.django.middleware.sqlcommenter_middleware._get_opentelemetry_values"
152+
)
153+
def test_empty_sql(self, trace_capture):
154+
requests_mock = MagicMock()
155+
requests_mock.resolver_match.view_name = "view"
156+
requests_mock.resolver_match.route = "route"
157+
requests_mock.resolver_match.app_name = "app"
158+
159+
trace_capture.return_value = {
160+
"traceparent": "*traceparent='00-000000000000000000000000deadbeef-000000000000beef-00"
161+
}
162+
qw_instance = _QueryWrapper(requests_mock)
163+
execute_mock_obj = MagicMock()
164+
qw_instance(
165+
execute_mock_obj,
166+
"",
167+
MagicMock("test"),
168+
MagicMock("test1"),
169+
MagicMock(),
170+
)
171+
output_sql = execute_mock_obj.call_args[0][0]
172+
self.assertEqual(
173+
output_sql,
174+
" /*app_name='app',controller='view',route='route',traceparent='%%2Atraceparent%%3D%%2700-0000000"
175+
"00000000000000000deadbeef-000000000000beef-00'*/",
176+
)

opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _add_sql_comment(sql, **meta) -> str:
2323
meta.update(**_add_framework_tags())
2424
comment = _generate_sql_comment(**meta)
2525
sql = sql.rstrip()
26-
if sql[-1] == ";":
26+
if sql.endswith(";"):
2727
sql = sql[:-1] + comment + ";"
2828
else:
2929
sql = sql + comment

0 commit comments

Comments
 (0)