18
18
except ImportError :
19
19
from urllib .parse import urlencode
20
20
from 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
21
26
22
27
def start_capture_outgoing (moesif_options ):
23
28
try :
@@ -89,6 +94,7 @@ def is_payload_format_version_1_0(cls, payload_format_version):
89
94
90
95
def get_user_id (self , event , context ):
91
96
"""Function to fetch UserId"""
97
+ start_time_get_user_id = datetime .utcnow ()
92
98
username = None
93
99
try :
94
100
identify_user = self .moesif_options .get ("IDENTIFY_USER" )
@@ -107,10 +113,14 @@ def get_user_id(self, event, context):
107
113
if self .DEBUG :
108
114
print ("MOESIF can not execute identify_user function, please check moesif settings." )
109
115
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 )))
110
119
return username
111
120
112
121
def get_company_id (self , event , context ):
113
122
"""Function to fetch CompanyId"""
123
+ start_time_get_company_id = datetime .utcnow ()
114
124
company_id = None
115
125
try :
116
126
identify_company = self .moesif_options .get ("IDENTIFY_COMPANY" )
@@ -120,6 +130,9 @@ def get_company_id(self, event, context):
120
130
if self .DEBUG :
121
131
print ("MOESIF can not execute identify_company function, please check moesif settings." )
122
132
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 )))
123
136
return company_id
124
137
125
138
def build_uri (self , event , payload_format_version_1_0 ):
@@ -179,6 +192,8 @@ def process_body(self, body_wrapper):
179
192
def before (self , event , context ):
180
193
"""This function runs before the handler is invoked, is passed the event & context and must return an event & context too."""
181
194
195
+ start_time_before_handler_function = datetime .utcnow ()
196
+
182
197
# Clear the state of the local variables
183
198
self .clear_state ()
184
199
@@ -227,6 +242,7 @@ def before(self, event, context):
227
242
req_body , req_transfer_encoding = self .process_body (event )
228
243
229
244
# Metadata
245
+ start_time_get_metadata = datetime .utcnow ()
230
246
try :
231
247
get_meta = self .moesif_options .get ("GET_METADATA" )
232
248
if get_meta is not None :
@@ -247,12 +263,23 @@ def before(self, event, context):
247
263
if self .DEBUG :
248
264
print ("MOESIF can not execute GET_METADATA function, please check moesif settings." )
249
265
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 )))
250
269
251
270
# User Id
271
+ start_time_identify_user = datetime .utcnow ()
252
272
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 )))
253
276
254
277
# Company Id
278
+ start_time_identify_company = datetime .utcnow ()
255
279
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 )))
256
283
257
284
# Session Token
258
285
try :
@@ -307,12 +334,16 @@ def before(self, event, context):
307
334
body = req_body ,
308
335
transfer_encoding = req_transfer_encoding )
309
336
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 )))
310
340
# Return event, context
311
341
return event , context
312
342
313
343
def after (self , retval ):
314
344
"""This function runs after the handler is invoked, is passed the response and must return an response too."""
315
345
346
+ start_time_after_handler_function = datetime .utcnow ()
316
347
if self .event is not None :
317
348
# Response body
318
349
resp_body , resp_transfer_encoding = self .process_body (retval )
@@ -361,9 +392,19 @@ def after(self, retval):
361
392
print ('Moesif Event Model:' )
362
393
print (json .dumps (self .event ))
363
394
364
- event_send = self .api_client .create_event (event_model )
365
395
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 )))
367
408
368
409
# Send response
369
410
return retval
0 commit comments