@@ -105,6 +105,7 @@ def wrap_connect(
105105 capture_parameters : bool = False ,
106106 enable_commenter : bool = False ,
107107 db_api_integration_factory = None ,
108+ commenter_options : dict = None ,
108109):
109110 """Integrate with DB API library.
110111 https://www.python.org/dev/peps/pep-0249/
@@ -119,6 +120,8 @@ def wrap_connect(
119120 tracer_provider: The :class:`opentelemetry.trace.TracerProvider` to
120121 use. If omitted the current configured one is used.
121122 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.
122125
123126 """
124127 db_api_integration_factory = (
@@ -140,6 +143,8 @@ def wrap_connect_(
140143 tracer_provider = tracer_provider ,
141144 capture_parameters = capture_parameters ,
142145 enable_commenter = enable_commenter ,
146+ commenter_options = commenter_options ,
147+ connect_module = connect_module ,
143148 )
144149 return db_integration .wrapped_connection (wrapped , args , kwargs )
145150
@@ -173,6 +178,7 @@ def instrument_connection(
173178 tracer_provider : typing .Optional [TracerProvider ] = None ,
174179 capture_parameters : bool = False ,
175180 enable_commenter : bool = False ,
181+ commenter_options : dict = None ,
176182):
177183 """Enable instrumentation in a database connection.
178184
@@ -185,6 +191,9 @@ def instrument_connection(
185191 tracer_provider: The :class:`opentelemetry.trace.TracerProvider` to
186192 use. If omitted the current configured one is used.
187193 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+
188197 Returns:
189198 An instrumented connection.
190199 """
@@ -200,6 +209,7 @@ def instrument_connection(
200209 tracer_provider = tracer_provider ,
201210 capture_parameters = capture_parameters ,
202211 enable_commenter = enable_commenter ,
212+ commenter_options = commenter_options ,
203213 )
204214 db_integration .get_connection_attributes (connection )
205215 return get_traced_connection_proxy (connection , db_integration )
@@ -231,6 +241,8 @@ def __init__(
231241 tracer_provider : typing .Optional [TracerProvider ] = None ,
232242 capture_parameters : bool = False ,
233243 enable_commenter : bool = False ,
244+ commenter_options : dict = None ,
245+ connect_module : typing .Callable [..., typing .Any ] = None ,
234246 ):
235247 self .connection_attributes = connection_attributes
236248 if self .connection_attributes is None :
@@ -249,11 +261,13 @@ def __init__(
249261 )
250262 self .capture_parameters = capture_parameters
251263 self .enable_commenter = enable_commenter
264+ self .commenter_options = commenter_options
252265 self .database_system = database_system
253266 self .connection_props = {}
254267 self .span_attributes = {}
255268 self .name = ""
256269 self .database = ""
270+ self .connect_module = connect_module
257271
258272 def wrapped_connection (
259273 self ,
@@ -335,12 +349,18 @@ class CursorTracer:
335349 def __init__ (self , db_api_integration : DatabaseApiIntegration ) -> None :
336350 self ._db_api_integration = db_api_integration
337351 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
338358
339359 def _populate_span (
340360 self ,
341361 span : trace_api .Span ,
342362 cursor ,
343- * args : typing .Tuple [typing .Any , typing .Any ]
363+ * args : typing .Tuple [typing .Any , typing .Any ],
344364 ):
345365 if not span .is_recording ():
346366 return
@@ -380,7 +400,7 @@ def traced_execution(
380400 cursor ,
381401 query_method : typing .Callable [..., typing .Any ],
382402 * args : typing .Tuple [typing .Any , typing .Any ],
383- ** kwargs : typing .Dict [typing .Any , typing .Any ]
403+ ** kwargs : typing .Dict [typing .Any , typing .Any ],
384404 ):
385405 name = self .get_operation_name (cursor , args )
386406 if not name :
@@ -397,14 +417,32 @@ def traced_execution(
397417 if args and self ._commenter_enabled :
398418 try :
399419 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+ }
402439 statement = _add_sql_comment (
403440 args_list [0 ], ** commenter_data
404441 )
405442
406443 args_list [0 ] = statement
407444 args = tuple (args_list )
445+
408446 except Exception as exc : # pylint: disable=broad-except
409447 _logger .exception (
410448 "Exception while generating sql comment: %s" , exc
0 commit comments