@@ -71,6 +71,7 @@ def trace_integration(
71
71
capture_parameters : bool = False ,
72
72
enable_commenter : bool = False ,
73
73
db_api_integration_factory = None ,
74
+ enable_attribute_commenter : bool = False ,
74
75
):
75
76
"""Integrate with DB API library.
76
77
https://www.python.org/dev/peps/pep-0249/
@@ -88,6 +89,7 @@ def trace_integration(
88
89
enable_commenter: Flag to enable/disable sqlcommenter.
89
90
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the
90
91
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.
91
93
"""
92
94
wrap_connect (
93
95
__name__ ,
@@ -100,6 +102,7 @@ def trace_integration(
100
102
capture_parameters = capture_parameters ,
101
103
enable_commenter = enable_commenter ,
102
104
db_api_integration_factory = db_api_integration_factory ,
105
+ enable_attribute_commenter = enable_attribute_commenter ,
103
106
)
104
107
105
108
@@ -115,6 +118,7 @@ def wrap_connect(
115
118
enable_commenter : bool = False ,
116
119
db_api_integration_factory = None ,
117
120
commenter_options : dict = None ,
121
+ enable_attribute_commenter : bool = False ,
118
122
):
119
123
"""Integrate with DB API library.
120
124
https://www.python.org/dev/peps/pep-0249/
@@ -133,6 +137,7 @@ def wrap_connect(
133
137
db_api_integration_factory: The `DatabaseApiIntegration` to use. If none is passed the
134
138
default one is used.
135
139
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.
136
141
137
142
"""
138
143
db_api_integration_factory = (
@@ -156,6 +161,7 @@ def wrap_connect_(
156
161
enable_commenter = enable_commenter ,
157
162
commenter_options = commenter_options ,
158
163
connect_module = connect_module ,
164
+ enable_attribute_commenter = enable_attribute_commenter ,
159
165
)
160
166
return db_integration .wrapped_connection (wrapped , args , kwargs )
161
167
@@ -191,6 +197,7 @@ def instrument_connection(
191
197
enable_commenter : bool = False ,
192
198
commenter_options : dict = None ,
193
199
connect_module : typing .Callable [..., typing .Any ] = None ,
200
+ enable_attribute_commenter : bool = False ,
194
201
):
195
202
"""Enable instrumentation in a database connection.
196
203
@@ -206,6 +213,7 @@ def instrument_connection(
206
213
enable_commenter: Flag to enable/disable sqlcommenter.
207
214
commenter_options: Configurations for tags to be appended at the sql query.
208
215
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.
209
217
210
218
Returns:
211
219
An instrumented connection.
@@ -224,6 +232,7 @@ def instrument_connection(
224
232
enable_commenter = enable_commenter ,
225
233
commenter_options = commenter_options ,
226
234
connect_module = connect_module ,
235
+ enable_attribute_commenter = enable_attribute_commenter ,
227
236
)
228
237
db_integration .get_connection_attributes (connection )
229
238
return get_traced_connection_proxy (connection , db_integration )
@@ -257,6 +266,7 @@ def __init__(
257
266
enable_commenter : bool = False ,
258
267
commenter_options : dict = None ,
259
268
connect_module : typing .Callable [..., typing .Any ] = None ,
269
+ enable_attribute_commenter : bool = False ,
260
270
):
261
271
self .connection_attributes = connection_attributes
262
272
if self .connection_attributes is None :
@@ -277,6 +287,7 @@ def __init__(
277
287
self .capture_parameters = capture_parameters
278
288
self .enable_commenter = enable_commenter
279
289
self .commenter_options = commenter_options
290
+ self .enable_attribute_commenter = enable_attribute_commenter
280
291
self .database_system = database_system
281
292
self .connection_props = {}
282
293
self .span_attributes = {}
@@ -434,9 +445,52 @@ def __init__(self, db_api_integration: DatabaseApiIntegration) -> None:
434
445
if self ._db_api_integration .commenter_options
435
446
else {}
436
447
)
448
+ self ._enable_attribute_commenter = (
449
+ self ._db_api_integration .enable_attribute_commenter
450
+ )
437
451
self ._connect_module = self ._db_api_integration .connect_module
438
452
self ._leading_comment_remover = re .compile (r"^/\*.*?\*/" )
439
453
454
+ def _capture_mysql_version (self , cursor ) -> None :
455
+ """Lazy capture of mysql-connector client version using cursor, if applicable"""
456
+ if (
457
+ self ._db_api_integration .database_system == "mysql"
458
+ and self ._db_api_integration .connect_module .__name__
459
+ == "mysql.connector"
460
+ and not self ._db_api_integration .commenter_data [
461
+ "mysql_client_version"
462
+ ]
463
+ ):
464
+ self ._db_api_integration .commenter_data ["mysql_client_version" ] = (
465
+ cursor ._cnx ._cmysql .get_client_info ()
466
+ )
467
+
468
+ def _get_commenter_data (self ) -> dict :
469
+ """Uses DB-API integration to return commenter data for sqlcomment"""
470
+ commenter_data = dict (self ._db_api_integration .commenter_data )
471
+ if self ._commenter_options .get ("opentelemetry_values" , True ):
472
+ commenter_data .update (** _get_opentelemetry_values ())
473
+ return {
474
+ k : v
475
+ for k , v in commenter_data .items ()
476
+ if self ._commenter_options .get (k , True )
477
+ }
478
+
479
+ def _update_args_with_added_sql_comment (self , args , cursor ) -> tuple :
480
+ """Updates args with cursor info and adds sqlcomment to query statement"""
481
+ try :
482
+ args_list = list (args )
483
+ self ._capture_mysql_version (cursor )
484
+ commenter_data = self ._get_commenter_data ()
485
+ statement = _add_sql_comment (args_list [0 ], ** commenter_data )
486
+ args_list [0 ] = statement
487
+ args = tuple (args_list )
488
+ except Exception as exc : # pylint: disable=broad-except
489
+ _logger .exception (
490
+ "Exception while generating sql comment: %s" , exc
491
+ )
492
+ return args
493
+
440
494
def _populate_span (
441
495
self ,
442
496
span : trace_api .Span ,
@@ -497,52 +551,22 @@ def traced_execution(
497
551
) as span :
498
552
if span .is_recording ():
499
553
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 ()
524
- )
525
-
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
554
+ if self ._enable_attribute_commenter :
555
+ # sqlcomment is added to executed query and db.statement span attribute
556
+ args = self ._update_args_with_added_sql_comment (
557
+ args , cursor
534
558
)
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
559
+ self . _populate_span ( span , cursor , * args )
560
+ else :
561
+ # sqlcomment is only added to executed query
562
+ # so db.statement is set before add_sql_comment
563
+ self . _populate_span ( span , cursor , * args )
564
+ args = self . _update_args_with_added_sql_comment (
565
+ args , cursor
542
566
)
543
-
544
- self . _populate_span ( span , cursor , * args )
545
-
567
+ else :
568
+ # no sqlcomment anywhere
569
+ self . _populate_span ( span , cursor , * args )
546
570
return query_method (* args , ** kwargs )
547
571
548
572
0 commit comments