1818except ImportError :
1919 from urllib .parse import urlencode
2020from moesifpythonrequest .start_capture .start_capture import StartCapture
21+ from datetime import datetime
22+
23+
24+ def get_time_took_in_ms (start_time , end_time ):
25+ return (end_time - start_time ).total_seconds () * 1000
2126
2227def start_capture_outgoing (moesif_options ):
2328 try :
@@ -89,6 +94,7 @@ def is_payload_format_version_1_0(cls, payload_format_version):
8994
9095 def get_user_id (self , event , context ):
9196 """Function to fetch UserId"""
97+ start_time_get_user_id = datetime .utcnow ()
9298 username = None
9399 try :
94100 identify_user = self .moesif_options .get ("IDENTIFY_USER" )
@@ -107,10 +113,14 @@ def get_user_id(self, event, context):
107113 if self .DEBUG :
108114 print ("MOESIF can not execute identify_user function, please check moesif settings." )
109115 print (e )
116+ end_time_get_user_id = datetime .utcnow ()
117+ if self .DEBUG :
118+ print ("[moesif] Time took in fetching user id in millisecond - " + str (get_time_took_in_ms (start_time_get_user_id , end_time_get_user_id )))
110119 return username
111120
112121 def get_company_id (self , event , context ):
113122 """Function to fetch CompanyId"""
123+ start_time_get_company_id = datetime .utcnow ()
114124 company_id = None
115125 try :
116126 identify_company = self .moesif_options .get ("IDENTIFY_COMPANY" )
@@ -120,6 +130,9 @@ def get_company_id(self, event, context):
120130 if self .DEBUG :
121131 print ("MOESIF can not execute identify_company function, please check moesif settings." )
122132 print (e )
133+ end_time_get_company_id = datetime .utcnow ()
134+ if self .DEBUG :
135+ print ("[moesif] Time took in fetching company id in millisecond - " + str (get_time_took_in_ms (start_time_get_company_id , end_time_get_company_id )))
123136 return company_id
124137
125138 def build_uri (self , event , payload_format_version_1_0 ):
@@ -179,6 +192,8 @@ def process_body(self, body_wrapper):
179192 def before (self , event , context ):
180193 """This function runs before the handler is invoked, is passed the event & context and must return an event & context too."""
181194
195+ start_time_before_handler_function = datetime .utcnow ()
196+
182197 # Clear the state of the local variables
183198 self .clear_state ()
184199
@@ -227,6 +242,7 @@ def before(self, event, context):
227242 req_body , req_transfer_encoding = self .process_body (event )
228243
229244 # Metadata
245+ start_time_get_metadata = datetime .utcnow ()
230246 try :
231247 get_meta = self .moesif_options .get ("GET_METADATA" )
232248 if get_meta is not None :
@@ -247,12 +263,23 @@ def before(self, event, context):
247263 if self .DEBUG :
248264 print ("MOESIF can not execute GET_METADATA function, please check moesif settings." )
249265 print (e )
266+ end_time_get_metadata = datetime .utcnow ()
267+ if self .DEBUG :
268+ print ("[moesif] Time took in fetching metadata in millisecond - " + str (get_time_took_in_ms (start_time_get_metadata , end_time_get_metadata )))
250269
251270 # User Id
271+ start_time_identify_user = datetime .utcnow ()
252272 self .user_id = self .get_user_id (event , context )
273+ end_time_identify_user = datetime .utcnow ()
274+ if self .DEBUG :
275+ print ("[moesif] Time took in identifying the user in millisecond - " + str (get_time_took_in_ms (start_time_identify_user , end_time_identify_user )))
253276
254277 # Company Id
278+ start_time_identify_company = datetime .utcnow ()
255279 self .company_id = self .get_company_id (event , context )
280+ end_time_identify_company = datetime .utcnow ()
281+ if self .DEBUG :
282+ print ("[moesif] Time took in identifying the company in millisecond - " + str (get_time_took_in_ms (start_time_identify_company , end_time_identify_company )))
256283
257284 # Session Token
258285 try :
@@ -307,12 +334,16 @@ def before(self, event, context):
307334 body = req_body ,
308335 transfer_encoding = req_transfer_encoding )
309336
337+ end_time_before_handler_function = datetime .utcnow ()
338+ if self .DEBUG :
339+ print ("[moesif] Time took before the handler is invoked in millisecond - " + str (get_time_took_in_ms (start_time_before_handler_function , end_time_before_handler_function )))
310340 # Return event, context
311341 return event , context
312342
313343 def after (self , retval ):
314344 """This function runs after the handler is invoked, is passed the response and must return an response too."""
315345
346+ start_time_after_handler_function = datetime .utcnow ()
316347 if self .event is not None :
317348 # Response body
318349 resp_body , resp_transfer_encoding = self .process_body (retval )
@@ -361,9 +392,19 @@ def after(self, retval):
361392 print ('Moesif Event Model:' )
362393 print (json .dumps (self .event ))
363394
364- event_send = self .api_client .create_event (event_model )
365395 if self .DEBUG :
366- print ('MOESIF ' + str (event_send ))
396+ start_time_sending_event_w_rsp = datetime .utcnow ()
397+ event_send = self .api_client .create_event (event_model )
398+ end_time_sending_event_w_rsp = datetime .utcnow ()
399+ if self .DEBUG :
400+ print ("[moesif] Time took in sending event to moesif in millisecond - " + str (get_time_took_in_ms (start_time_sending_event_w_rsp , end_time_sending_event_w_rsp )))
401+ print ('[moesif] Event Sent successfully ' + str (event_send ))
402+ else :
403+ self .api_client .create_event (event_model )
404+
405+ end_time_after_handler_function = datetime .utcnow ()
406+ if self .DEBUG :
407+ print ("[moesif] Time took after the handler is invoked in millisecond - " + str (get_time_took_in_ms (start_time_after_handler_function , end_time_after_handler_function )))
367408
368409 # Send response
369410 return retval
0 commit comments