@@ -23,7 +23,7 @@ def parse_dsn(dsn):
2323
2424from ..trace import trace_factory
2525from ..event import BaseEvent
26- from ..utils import database_connection_type
26+ from ..utils import database_connection_type , print_debug
2727
2828MAX_QUERY_SIZE = 2048
2929
@@ -68,33 +68,47 @@ def __init__(
6868 """
6969
7070 super (DBAPIEvent , self ).__init__ (start_time )
71-
7271 self .event_id = 'dbapi-{}' .format (str (uuid4 ()))
7372
7473 # in case of pg instrumentation we extract data from the dsn property
7574 if hasattr (connection , 'dsn' ):
75+ print_debug ('Parsing using dsn' )
7676 dsn = parse_dsn (connection .dsn )
77- db_name = dsn ['dbname' ]
77+ print_debug ('Parsed dsn: {}' .format (dsn ))
78+ db_name = dsn .get ('dbname' , '' )
7879 host = dsn .get ('host' , 'local' )
7980 query = cursor .query
8081 else :
8182 query = _args [0 ]
8283 host = connection .extract_hostname
8384 db_name = connection .extract_dbname
85+ print_debug (
86+ 'Extracted db name - {}, host - {}, query - {}' .format (
87+ db_name , host , query
88+ )
89+ )
8490
8591 self .resource ['name' ] = db_name if db_name else host
8692
8793 # NOTE: The operation might not be identified properly when
8894 # using 'WITH' clause
89- operation = query .split ()[0 ].lower ()
95+ splitted_query = query .split ()
96+ if not splitted_query :
97+ print_debug ('Cannot extract operation from query {}' .format (query ))
98+ operation = ''
99+ else :
100+ operation = splitted_query [0 ].lower ()
90101 self .resource ['operation' ] = operation
91-
92102 # override event type with the specific DB type
93103 self .resource ['type' ] = database_connection_type (
94104 host ,
95105 self .RESOURCE_TYPE
96106 )
97-
107+ print_debug (
108+ '{}: resource name - {}, operation - {}' .format (
109+ self .event_id , self .resource ['name' ], self .resource ['operation' ]
110+ )
111+ )
98112 self .resource ['metadata' ] = {
99113 'Host' : host ,
100114 'Driver' : connection .__class__ .__module__ .split ('.' )[0 ],
@@ -107,6 +121,11 @@ def __init__(
107121 (not trace_factory .metadata_only )
108122 ):
109123 self .resource ['metadata' ]['Query' ] = query [:MAX_QUERY_SIZE ]
124+ print_debug (
125+ '{}: collected query {}' .format (
126+ self .event_id , self .resource ['metadata' ]['Query' ]
127+ )
128+ )
110129
111130 if exception is None :
112131 # Update response data
@@ -155,6 +174,7 @@ def create_event(wrapped, cursor_wrapper, args, kwargs, start_time,
155174 :param exception:
156175 :return:
157176 """
177+ print_debug ('Creating DBAPI event' )
158178 event = DBAPIEvent (
159179 cursor_wrapper .connection_wrapper ,
160180 cursor_wrapper ,
@@ -163,4 +183,5 @@ def create_event(wrapped, cursor_wrapper, args, kwargs, start_time,
163183 start_time ,
164184 exception ,
165185 )
186+ print_debug ('Adding DBAPI event to trace' )
166187 trace_factory .add_event (event )
0 commit comments