Skip to content

Commit b8b5a67

Browse files
(WIP) db-api opt-in for enable_attribute_commenter
1 parent 52ff7bd commit b8b5a67

File tree

2 files changed

+519
-44
lines changed

2 files changed

+519
-44
lines changed

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

Lines changed: 115 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def trace_integration(
7171
capture_parameters: bool = False,
7272
enable_commenter: bool = False,
7373
db_api_integration_factory=None,
74+
enable_attribute_commenter: bool = False,
7475
):
7576
"""Integrate with DB API library.
7677
https://www.python.org/dev/peps/pep-0249/
@@ -88,6 +89,7 @@ def trace_integration(
8889
enable_commenter: Flag to enable/disable sqlcommenter.
8990
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the
9091
default one is used.
92+
enable_attribute_commenter: Flag to enable/disable sqlcomment inclusion in `db.statement` span attribute. Only available if enable_commenter=True.
9193
"""
9294
wrap_connect(
9395
__name__,
@@ -100,6 +102,7 @@ def trace_integration(
100102
capture_parameters=capture_parameters,
101103
enable_commenter=enable_commenter,
102104
db_api_integration_factory=db_api_integration_factory,
105+
enable_attribute_commenter=enable_attribute_commenter,
103106
)
104107

105108

@@ -115,6 +118,7 @@ def wrap_connect(
115118
enable_commenter: bool = False,
116119
db_api_integration_factory=None,
117120
commenter_options: dict = None,
121+
enable_attribute_commenter: bool = False,
118122
):
119123
"""Integrate with DB API library.
120124
https://www.python.org/dev/peps/pep-0249/
@@ -133,6 +137,7 @@ def wrap_connect(
133137
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the
134138
default one is used.
135139
commenter_options: Configurations for tags to be appended at the sql query.
140+
enable_attribute_commenter: Flag to enable/disable sqlcomment inclusion in `db.statement` span attribute. Only available if enable_commenter=True.
136141
137142
"""
138143
db_api_integration_factory = (
@@ -156,6 +161,7 @@ def wrap_connect_(
156161
enable_commenter=enable_commenter,
157162
commenter_options=commenter_options,
158163
connect_module=connect_module,
164+
enable_attribute_commenter=enable_attribute_commenter,
159165
)
160166
return db_integration.wrapped_connection(wrapped, args, kwargs)
161167

@@ -191,6 +197,7 @@ def instrument_connection(
191197
enable_commenter: bool = False,
192198
commenter_options: dict = None,
193199
connect_module: typing.Callable[..., typing.Any] = None,
200+
enable_attribute_commenter: bool = False,
194201
):
195202
"""Enable instrumentation in a database connection.
196203
@@ -206,6 +213,7 @@ def instrument_connection(
206213
enable_commenter: Flag to enable/disable sqlcommenter.
207214
commenter_options: Configurations for tags to be appended at the sql query.
208215
connect_module: Module name where connect method is available.
216+
enable_attribute_commenter: Flag to enable/disable sqlcomment inclusion in `db.statement` span attribute. Only available if enable_commenter=True.
209217
210218
Returns:
211219
An instrumented connection.
@@ -224,6 +232,7 @@ def instrument_connection(
224232
enable_commenter=enable_commenter,
225233
commenter_options=commenter_options,
226234
connect_module=connect_module,
235+
enable_attribute_commenter=enable_attribute_commenter,
227236
)
228237
db_integration.get_connection_attributes(connection)
229238
return get_traced_connection_proxy(connection, db_integration)
@@ -257,6 +266,7 @@ def __init__(
257266
enable_commenter: bool = False,
258267
commenter_options: dict = None,
259268
connect_module: typing.Callable[..., typing.Any] = None,
269+
enable_attribute_commenter: bool = False,
260270
):
261271
self.connection_attributes = connection_attributes
262272
if self.connection_attributes is None:
@@ -277,6 +287,7 @@ def __init__(
277287
self.capture_parameters = capture_parameters
278288
self.enable_commenter = enable_commenter
279289
self.commenter_options = commenter_options
290+
self.enable_attribute_commenter = enable_attribute_commenter
280291
self.database_system = database_system
281292
self.connection_props = {}
282293
self.span_attributes = {}
@@ -434,6 +445,9 @@ def __init__(self, db_api_integration: DatabaseApiIntegration) -> None:
434445
if self._db_api_integration.commenter_options
435446
else {}
436447
)
448+
self._enable_attribute_commenter = (
449+
self._db_api_integration.enable_attribute_commenter
450+
)
437451
self._connect_module = self._db_api_integration.connect_module
438452
self._leading_comment_remover = re.compile(r"^/\*.*?\*/")
439453

@@ -497,51 +511,109 @@ def traced_execution(
497511
) as span:
498512
if span.is_recording():
499513
if args and self._commenter_enabled:
500-
try:
501-
args_list = list(args)
502-
503-
# lazy capture of mysql-connector client version using cursor
504-
if (
505-
self._db_api_integration.database_system == "mysql"
506-
and self._db_api_integration.connect_module.__name__
507-
== "mysql.connector"
508-
and not self._db_api_integration.commenter_data[
509-
"mysql_client_version"
510-
]
511-
):
512-
self._db_api_integration.commenter_data[
513-
"mysql_client_version"
514-
] = cursor._cnx._cmysql.get_client_info()
515-
516-
commenter_data = dict(
517-
self._db_api_integration.commenter_data
518-
)
519-
if self._commenter_options.get(
520-
"opentelemetry_values", True
521-
):
522-
commenter_data.update(
523-
**_get_opentelemetry_values()
514+
# sqlcomment in query and span attribute
515+
if self._enable_attribute_commenter:
516+
try:
517+
args_list = list(args)
518+
519+
# lazy capture of mysql-connector client version using cursor
520+
if (
521+
self._db_api_integration.database_system
522+
== "mysql"
523+
and self._db_api_integration.connect_module.__name__
524+
== "mysql.connector"
525+
and not self._db_api_integration.commenter_data[
526+
"mysql_client_version"
527+
]
528+
):
529+
self._db_api_integration.commenter_data[
530+
"mysql_client_version"
531+
] = cursor._cnx._cmysql.get_client_info()
532+
533+
commenter_data = dict(
534+
self._db_api_integration.commenter_data
535+
)
536+
if self._commenter_options.get(
537+
"opentelemetry_values", True
538+
):
539+
commenter_data.update(
540+
**_get_opentelemetry_values()
541+
)
542+
543+
# Filter down to just the requested attributes.
544+
commenter_data = {
545+
k: v
546+
for k, v in commenter_data.items()
547+
if self._commenter_options.get(k, True)
548+
}
549+
statement = _add_sql_comment(
550+
args_list[0], **commenter_data
551+
)
552+
553+
args_list[0] = statement
554+
args = tuple(args_list)
555+
556+
except Exception as exc: # pylint: disable=broad-except
557+
_logger.exception(
558+
"Exception while generating sql comment: %s",
559+
exc,
560+
)
561+
562+
self._populate_span(span, cursor, *args)
563+
564+
# sqlcomment in query only
565+
else:
566+
self._populate_span(span, cursor, *args)
567+
568+
try:
569+
args_list = list(args)
570+
571+
# lazy capture of mysql-connector client version using cursor
572+
if (
573+
self._db_api_integration.database_system
574+
== "mysql"
575+
and self._db_api_integration.connect_module.__name__
576+
== "mysql.connector"
577+
and not self._db_api_integration.commenter_data[
578+
"mysql_client_version"
579+
]
580+
):
581+
self._db_api_integration.commenter_data[
582+
"mysql_client_version"
583+
] = cursor._cnx._cmysql.get_client_info()
584+
585+
commenter_data = dict(
586+
self._db_api_integration.commenter_data
587+
)
588+
if self._commenter_options.get(
589+
"opentelemetry_values", True
590+
):
591+
commenter_data.update(
592+
**_get_opentelemetry_values()
593+
)
594+
595+
# Filter down to just the requested attributes.
596+
commenter_data = {
597+
k: v
598+
for k, v in commenter_data.items()
599+
if self._commenter_options.get(k, True)
600+
}
601+
statement = _add_sql_comment(
602+
args_list[0], **commenter_data
603+
)
604+
605+
args_list[0] = statement
606+
args = tuple(args_list)
607+
608+
except Exception as exc: # pylint: disable=broad-except
609+
_logger.exception(
610+
"Exception while generating sql comment: %s",
611+
exc,
524612
)
525613

526-
# Filter down to just the requested attributes.
527-
commenter_data = {
528-
k: v
529-
for k, v in commenter_data.items()
530-
if self._commenter_options.get(k, True)
531-
}
532-
statement = _add_sql_comment(
533-
args_list[0], **commenter_data
534-
)
535-
536-
args_list[0] = statement
537-
args = tuple(args_list)
538-
539-
except Exception as exc: # pylint: disable=broad-except
540-
_logger.exception(
541-
"Exception while generating sql comment: %s", exc
542-
)
543-
544-
self._populate_span(span, cursor, *args)
614+
# No sqlcommenting
615+
else:
616+
self._populate_span(span, cursor, *args)
545617

546618
return query_method(*args, **kwargs)
547619

0 commit comments

Comments
 (0)