Skip to content

Commit 44c9bb3

Browse files
author
Daniel Rogers
committed
Strip leading comments from SQL queries when generating the span name.
1 parent 5f85a5b commit 44c9bb3

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
1414
- `opentelemetry-instrumentation-fastapi` Add support for regular expression matching and sanitization of HTTP headers.
1515
([#1403](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1403))
16+
- `opentelemetry-instrumentation-dbapi` Strip leading comments from SQL queries when generating the span name.
17+
([#1434](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1434))
1618

1719
### Fixed
1820

instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import functools
4141
import logging
42+
import re
4243
import typing
4344

4445
import wrapt
@@ -368,6 +369,7 @@ def __init__(self, db_api_integration: DatabaseApiIntegration) -> None:
368369
else {}
369370
)
370371
self._connect_module = self._db_api_integration.connect_module
372+
self._leading_comment_remover = re.compile(r"^/\*.*?\*/")
371373

372374
def _populate_span(
373375
self,
@@ -397,7 +399,8 @@ def _populate_span(
397399

398400
def get_operation_name(self, cursor, args): # pylint: disable=no-self-use
399401
if args and isinstance(args[0], str):
400-
return args[0].split()[0]
402+
# Strip leading comments so we get the operation name.
403+
return self._leading_comment_remover.sub("", args[0]).split()[0]
401404
return ""
402405

403406
def get_statement(self, cursor, args): # pylint: disable=no-self-use

instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,17 @@ def test_span_name(self):
8888
query"""
8989
)
9090
cursor.execute("tab\tseparated query")
91+
cursor.execute("/* leading comment */ query")
92+
cursor.execute("/* leading comment */ query /* trailing comment */")
93+
cursor.execute("query /* trailing comment */")
9194
spans_list = self.memory_exporter.get_finished_spans()
92-
self.assertEqual(len(spans_list), 3)
95+
self.assertEqual(len(spans_list), 6)
9396
self.assertEqual(spans_list[0].name, "Test")
9497
self.assertEqual(spans_list[1].name, "multi")
9598
self.assertEqual(spans_list[2].name, "tab")
99+
self.assertEqual(spans_list[3].name, "query")
100+
self.assertEqual(spans_list[4].name, "query")
101+
self.assertEqual(spans_list[5].name, "query")
96102

97103
def test_span_succeeded_with_capture_of_statement_parameters(self):
98104
connection_props = {

instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ def get_operation_name(self, cursor, args):
216216
statement = statement.as_string(cursor)
217217

218218
if isinstance(statement, str):
219-
return statement.split()[0]
219+
# Strip leading comments so we get the operation name.
220+
return self._leading_comment_remover.sub("", statement).split()[0]
220221

221222
return ""
222223

instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_integration.py

+26
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ def test_instrumentor(self):
116116
spans_list = self.memory_exporter.get_finished_spans()
117117
self.assertEqual(len(spans_list), 1)
118118

119+
def test_span_name(self):
120+
Psycopg2Instrumentor().instrument()
121+
122+
cnx = psycopg2.connect(database="test")
123+
124+
cursor = cnx.cursor()
125+
126+
cursor.execute("Test query", ("param1Value", False))
127+
cursor.execute(
128+
"""multi
129+
line
130+
query"""
131+
)
132+
cursor.execute("tab\tseparated query")
133+
cursor.execute("/* leading comment */ query")
134+
cursor.execute("/* leading comment */ query /* trailing comment */")
135+
cursor.execute("query /* trailing comment */")
136+
spans_list = self.memory_exporter.get_finished_spans()
137+
self.assertEqual(len(spans_list), 6)
138+
self.assertEqual(spans_list[0].name, "Test")
139+
self.assertEqual(spans_list[1].name, "multi")
140+
self.assertEqual(spans_list[2].name, "tab")
141+
self.assertEqual(spans_list[3].name, "query")
142+
self.assertEqual(spans_list[4].name, "query")
143+
self.assertEqual(spans_list[5].name, "query")
144+
119145
# pylint: disable=unused-argument
120146
def test_not_recording(self):
121147
mock_tracer = mock.Mock()

0 commit comments

Comments
 (0)