Skip to content

Commit f88ddcc

Browse files
fix: restore comment_position, pylint disable, CommentPositionT after conflict resolution
1 parent f360254 commit f88ddcc

2 files changed

Lines changed: 24 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: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
import functools
172172
import logging
173173
import re
174-
from typing import Any, Awaitable, Callable, Generic, TypeVar
174+
from typing import Any, Awaitable, Callable, Generic, Literal, TypeVar
175175

176176
from wrapt import wrap_function_wrapper
177177

@@ -210,13 +210,15 @@
210210
"MySQLdb": "mysqlclient",
211211
}
212212

213+
CommentPositionT = Literal["start", "end"]
214+
213215
_logger = logging.getLogger(__name__)
214216

215217
ConnectionT = TypeVar("ConnectionT")
216218
CursorT = TypeVar("CursorT")
217219

218220

219-
def trace_integration(
221+
def trace_integration( # pylint: disable=too-many-positional-arguments
220222
connect_module: Callable[..., Any],
221223
connect_method_name: str,
222224
database_system: str,
@@ -227,7 +229,7 @@ def trace_integration(
227229
db_api_integration_factory: type[DatabaseApiIntegration] | None = None,
228230
enable_attribute_commenter: bool = False,
229231
commenter_options: dict[str, Any] | None = None,
230-
comment_position: str = "end",
232+
comment_position: CommentPositionT = "end",
231233
):
232234
"""Integrate with DB API library.
233235
https://www.python.org/dev/peps/pep-0249/
@@ -265,7 +267,7 @@ def trace_integration(
265267
)
266268

267269

268-
def wrap_connect(
270+
def wrap_connect( # pylint: disable=too-many-positional-arguments
269271
name: str,
270272
connect_module: Callable[..., Any],
271273
connect_method_name: str,
@@ -278,7 +280,7 @@ def wrap_connect(
278280
db_api_integration_factory: type[DatabaseApiIntegration] | None = None,
279281
commenter_options: dict[str, Any] | None = None,
280282
enable_attribute_commenter: bool = False,
281-
comment_position: str = "end",
283+
comment_position: CommentPositionT = "end",
282284
):
283285
"""Integrate with DB API library.
284286
https://www.python.org/dev/peps/pep-0249/
@@ -347,7 +349,7 @@ def unwrap_connect(
347349
unwrap(connect_module, connect_method_name)
348350

349351

350-
def instrument_connection(
352+
def instrument_connection( # pylint: disable=too-many-positional-arguments
351353
name: str,
352354
connection: ConnectionT | TracedConnectionProxy[ConnectionT],
353355
database_system: str,
@@ -360,7 +362,7 @@ def instrument_connection(
360362
connect_module: Callable[..., Any] | None = None,
361363
enable_attribute_commenter: bool = False,
362364
db_api_integration_factory: type[DatabaseApiIntegration] | None = None,
363-
comment_position: str = "end",
365+
comment_position: CommentPositionT = "end",
364366
) -> TracedConnectionProxy[ConnectionT]:
365367
"""Enable instrumentation in a database connection.
366368
@@ -430,7 +432,7 @@ def uninstrument_connection(
430432

431433

432434
class DatabaseApiIntegration:
433-
def __init__(
435+
def __init__( # pylint: disable=too-many-positional-arguments
434436
self,
435437
name: str,
436438
database_system: str,
@@ -442,7 +444,7 @@ def __init__(
442444
commenter_options: dict[str, Any] | None = None,
443445
connect_module: Callable[..., Any] | None = None,
444446
enable_attribute_commenter: bool = False,
445-
comment_position: str = "end",
447+
comment_position: CommentPositionT = "end",
446448
):
447449
if connection_attributes is None:
448450
self.connection_attributes = {
@@ -689,7 +691,11 @@ def _update_args_with_added_sql_comment(self, args, cursor) -> tuple:
689691
args_list[0] = args_list[0].as_string(cursor.connection)
690692

691693
args_list[0] = str(args_list[0])
692-
statement = _add_sql_comment(args_list[0], comment_position=self._db_api_integration.comment_position, **commenter_data)
694+
statement = _add_sql_comment(
695+
args_list[0],
696+
comment_position=self._db_api_integration.comment_position,
697+
**commenter_data,
698+
)
693699
args_list[0] = statement
694700
args = tuple(args_list)
695701
except Exception as exc: # pylint: disable=broad-except

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@
1616
from opentelemetry.instrumentation.utils import _url_quote
1717

1818

19-
def _add_sql_comment(sql, **meta) -> str:
19+
def _add_sql_comment(sql, comment_position="end", **meta) -> str:
2020
"""
2121
Appends comments to the sql statement and returns it
2222
"""
2323
meta.update(**_add_framework_tags())
2424
comment = _generate_sql_comment(**meta)
2525
sql = sql.rstrip()
2626
if sql.endswith(";"):
27-
sql = sql[:-1] + comment + ";"
27+
sql = sql[:-1]
28+
end = ";"
2829
else:
29-
sql = sql + comment
30+
end = ""
31+
if comment_position == "start":
32+
sql = comment + " " + sql + end
33+
else:
34+
sql = sql + comment + end
3035
return sql
3136

3237

0 commit comments

Comments
 (0)