@@ -105,6 +105,7 @@ def wrap_connect(
105
105
capture_parameters : bool = False ,
106
106
enable_commenter : bool = False ,
107
107
db_api_integration_factory = None ,
108
+ commenter_options : dict = None ,
108
109
):
109
110
"""Integrate with DB API library.
110
111
https://www.python.org/dev/peps/pep-0249/
@@ -119,6 +120,8 @@ def wrap_connect(
119
120
tracer_provider: The :class:`opentelemetry.trace.TracerProvider` to
120
121
use. If omitted the current configured one is used.
121
122
capture_parameters: Configure if db.statement.parameters should be captured.
123
+ enable_commenter: Flag to enable/disable sqlcommenter.
124
+ commenter_options: Configurations for tags to be appended at the sql query.
122
125
123
126
"""
124
127
db_api_integration_factory = (
@@ -140,6 +143,8 @@ def wrap_connect_(
140
143
tracer_provider = tracer_provider ,
141
144
capture_parameters = capture_parameters ,
142
145
enable_commenter = enable_commenter ,
146
+ commenter_options = commenter_options ,
147
+ connect_module = connect_module ,
143
148
)
144
149
return db_integration .wrapped_connection (wrapped , args , kwargs )
145
150
@@ -173,6 +178,7 @@ def instrument_connection(
173
178
tracer_provider : typing .Optional [TracerProvider ] = None ,
174
179
capture_parameters : bool = False ,
175
180
enable_commenter : bool = False ,
181
+ commenter_options : dict = None ,
176
182
):
177
183
"""Enable instrumentation in a database connection.
178
184
@@ -185,6 +191,9 @@ def instrument_connection(
185
191
tracer_provider: The :class:`opentelemetry.trace.TracerProvider` to
186
192
use. If omitted the current configured one is used.
187
193
capture_parameters: Configure if db.statement.parameters should be captured.
194
+ enable_commenter: Flag to enable/disable sqlcommenter.
195
+ commenter_options: Configurations for tags to be appended at the sql query.
196
+
188
197
Returns:
189
198
An instrumented connection.
190
199
"""
@@ -200,6 +209,7 @@ def instrument_connection(
200
209
tracer_provider = tracer_provider ,
201
210
capture_parameters = capture_parameters ,
202
211
enable_commenter = enable_commenter ,
212
+ commenter_options = commenter_options ,
203
213
)
204
214
db_integration .get_connection_attributes (connection )
205
215
return get_traced_connection_proxy (connection , db_integration )
@@ -231,6 +241,8 @@ def __init__(
231
241
tracer_provider : typing .Optional [TracerProvider ] = None ,
232
242
capture_parameters : bool = False ,
233
243
enable_commenter : bool = False ,
244
+ commenter_options : dict = None ,
245
+ connect_module : typing .Callable [..., typing .Any ] = None ,
234
246
):
235
247
self .connection_attributes = connection_attributes
236
248
if self .connection_attributes is None :
@@ -249,11 +261,13 @@ def __init__(
249
261
)
250
262
self .capture_parameters = capture_parameters
251
263
self .enable_commenter = enable_commenter
264
+ self .commenter_options = commenter_options
252
265
self .database_system = database_system
253
266
self .connection_props = {}
254
267
self .span_attributes = {}
255
268
self .name = ""
256
269
self .database = ""
270
+ self .connect_module = connect_module
257
271
258
272
def wrapped_connection (
259
273
self ,
@@ -335,12 +349,18 @@ class CursorTracer:
335
349
def __init__ (self , db_api_integration : DatabaseApiIntegration ) -> None :
336
350
self ._db_api_integration = db_api_integration
337
351
self ._commenter_enabled = self ._db_api_integration .enable_commenter
352
+ self ._commenter_options = (
353
+ self ._db_api_integration .commenter_options
354
+ if self ._db_api_integration .commenter_options
355
+ else {}
356
+ )
357
+ self ._connect_module = self ._db_api_integration .connect_module
338
358
339
359
def _populate_span (
340
360
self ,
341
361
span : trace_api .Span ,
342
362
cursor ,
343
- * args : typing .Tuple [typing .Any , typing .Any ]
363
+ * args : typing .Tuple [typing .Any , typing .Any ],
344
364
):
345
365
if not span .is_recording ():
346
366
return
@@ -380,7 +400,7 @@ def traced_execution(
380
400
cursor ,
381
401
query_method : typing .Callable [..., typing .Any ],
382
402
* args : typing .Tuple [typing .Any , typing .Any ],
383
- ** kwargs : typing .Dict [typing .Any , typing .Any ]
403
+ ** kwargs : typing .Dict [typing .Any , typing .Any ],
384
404
):
385
405
name = self .get_operation_name (cursor , args )
386
406
if not name :
@@ -397,14 +417,32 @@ def traced_execution(
397
417
if args and self ._commenter_enabled :
398
418
try :
399
419
args_list = list (args )
400
- commenter_data = {}
401
- commenter_data .update (_get_opentelemetry_values ())
420
+ commenter_data = dict (
421
+ # Psycopg2/framework information
422
+ db_driver = f"psycopg2:{ self ._connect_module .__version__ .split (' ' )[0 ]} " ,
423
+ dbapi_threadsafety = self ._connect_module .threadsafety ,
424
+ dbapi_level = self ._connect_module .apilevel ,
425
+ libpq_version = self ._connect_module .__libpq_version__ ,
426
+ driver_paramstyle = self ._connect_module .paramstyle ,
427
+ )
428
+ if self ._commenter_options .get (
429
+ "opentelemetry_values" , True
430
+ ):
431
+ commenter_data .update (** _get_opentelemetry_values ())
432
+
433
+ # Filter down to just the requested attributes.
434
+ commenter_data = {
435
+ k : v
436
+ for k , v in commenter_data .items ()
437
+ if self ._commenter_options .get (k , True )
438
+ }
402
439
statement = _add_sql_comment (
403
440
args_list [0 ], ** commenter_data
404
441
)
405
442
406
443
args_list [0 ] = statement
407
444
args = tuple (args_list )
445
+
408
446
except Exception as exc : # pylint: disable=broad-except
409
447
_logger .exception (
410
448
"Exception while generating sql comment: %s" , exc
0 commit comments