Skip to content

Commit f56f055

Browse files
fix: use Literal type for comment_position and simplify sqlcommenter logic
1 parent eb1bc58 commit f56f055

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

  • instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi
  • opentelemetry-instrumentation/src/opentelemetry/instrumentation

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
import functools
161161
import logging
162162
import re
163-
from typing import Any, Awaitable, Callable, Generic, TypeVar
163+
from typing import Any, Awaitable, Callable, Generic, Literal, TypeVar
164164

165165
from wrapt import wrap_function_wrapper
166166

@@ -199,6 +199,8 @@
199199
"MySQLdb": "mysqlclient",
200200
}
201201

202+
CommentPositionT = Literal["start", "end"]
203+
202204
_logger = logging.getLogger(__name__)
203205

204206
ConnectionT = TypeVar("ConnectionT")
@@ -216,7 +218,7 @@ def trace_integration( # pylint: disable=too-many-positional-arguments
216218
db_api_integration_factory: type[DatabaseApiIntegration] | None = None,
217219
enable_attribute_commenter: bool = False,
218220
commenter_options: dict[str, Any] | None = None,
219-
comment_position: str = "end",
221+
comment_position: CommentPositionT = "end",
220222
):
221223
"""Integrate with DB API library.
222224
https://www.python.org/dev/peps/pep-0249/
@@ -267,7 +269,7 @@ def wrap_connect( # pylint: disable=too-many-positional-arguments
267269
db_api_integration_factory: type[DatabaseApiIntegration] | None = None,
268270
commenter_options: dict[str, Any] | None = None,
269271
enable_attribute_commenter: bool = False,
270-
comment_position: str = "end",
272+
comment_position: CommentPositionT = "end",
271273
):
272274
"""Integrate with DB API library.
273275
https://www.python.org/dev/peps/pep-0249/
@@ -349,7 +351,7 @@ def instrument_connection( # pylint: disable=too-many-positional-arguments
349351
connect_module: Callable[..., Any] | None = None,
350352
enable_attribute_commenter: bool = False,
351353
db_api_integration_factory: type[DatabaseApiIntegration] | None = None,
352-
comment_position: str = "end",
354+
comment_position: CommentPositionT = "end",
353355
) -> TracedConnectionProxy[ConnectionT]:
354356
"""Enable instrumentation in a database connection.
355357
@@ -431,7 +433,7 @@ def __init__( # pylint: disable=too-many-positional-arguments
431433
commenter_options: dict[str, Any] | None = None,
432434
connect_module: Callable[..., Any] | None = None,
433435
enable_attribute_commenter: bool = False,
434-
comment_position: str = "end",
436+
comment_position: CommentPositionT = "end",
435437
):
436438
if connection_attributes is None:
437439
self.connection_attributes = {

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ def _add_sql_comment(sql, comment_position="end", **meta) -> str:
1212
meta.update(**_add_framework_tags())
1313
comment = _generate_sql_comment(**meta)
1414
sql = sql.strip()
15+
if sql.endswith(";"):
16+
sql = sql[:-1]
17+
end = ";"
18+
else:
19+
end = ""
1520
if comment_position == "start":
16-
if sql.endswith(";"):
17-
sql = comment + " " + sql[:-1] + ";"
18-
else:
19-
sql = comment + " " + sql
21+
sql = comment + " " + sql + end
2022
else:
21-
if sql.endswith(";"):
22-
sql = sql[:-1] + comment + ";"
23-
else:
24-
sql = sql + comment
23+
sql = sql + comment + end
2524
return sql
2625

2726

0 commit comments

Comments
 (0)