7
7
from .client_ip import ClientIp
8
8
from .update_companies import Company
9
9
from .update_users import User
10
+ from . import global_variable as gv
11
+
10
12
from datetime import *
11
13
import base64
12
14
import json
13
15
import os
14
16
from pprint import pprint
15
17
import base64
18
+
19
+ import random
20
+ import math
21
+
16
22
try :
17
23
from urllib import urlencode
18
24
except ImportError :
24
30
def get_time_took_in_ms (start_time , end_time ):
25
31
return (end_time - start_time ).total_seconds () * 1000
26
32
33
+
27
34
def start_capture_outgoing (moesif_options ):
28
35
try :
29
36
if moesif_options .get ('DEBUG' , False ):
30
- print ('Start capturing outgoing requests' )
37
+ print ('[moesif] Start capturing outgoing requests' )
38
+
31
39
# Start capturing outgoing requests
32
40
moesif_options ['APPLICATION_ID' ] = os .environ ["MOESIF_APPLICATION_ID" ]
33
41
StartCapture ().start_capture_outgoing (moesif_options )
42
+
43
+ if moesif_options .get ('DEBUG' , False ):
44
+ print ("[moesif] end capturing moesif options" )
34
45
except Exception as e :
35
46
print ('Error while starting to capture the outgoing events' )
36
47
print (e )
48
+ return
49
+
37
50
38
51
# Initialized the client
39
- if os .environ ["MOESIF_APPLICATION_ID" ]:
40
- api_client = MoesifAPIClient (os .environ ["MOESIF_APPLICATION_ID" ]).api
41
- else :
42
- raise Exception ('Moesif Application ID is required in settings' )
52
+ api_client = gv .api_client
53
+
43
54
44
55
def update_user (user_profile , moesif_options ):
45
56
User ().update_user (user_profile , api_client , moesif_options )
46
57
58
+
47
59
def update_users_batch (user_profiles , moesif_options ):
48
60
User ().update_users_batch (user_profiles , api_client , moesif_options )
49
61
62
+
50
63
def update_company (company_profile , moesif_options ):
51
64
Company ().update_company (company_profile , api_client , moesif_options )
52
65
66
+
53
67
def update_companies_batch (companies_profiles , moesif_options ):
54
68
Company ().update_companies_batch (companies_profiles , api_client , moesif_options )
55
69
@@ -70,12 +84,9 @@ def __init__(self, handler):
70
84
self .event = None
71
85
self .context = None
72
86
self .payload_version = None
73
-
74
- # Intialized the client
75
- if os .environ .get ("MOESIF_APPLICATION_ID" ):
76
- self .api_client = MoesifAPIClient (os .environ ["MOESIF_APPLICATION_ID" ]).api
77
- else :
78
- raise Exception ('Moesif Application ID is required in settings' )
87
+
88
+ # Set the client
89
+ self .api_client = api_client
79
90
80
91
def clear_state (self ):
81
92
"""Function to clear state of local variable"""
@@ -108,16 +119,16 @@ def get_user_id(self, event, context):
108
119
username = rc_identity_id
109
120
except :
110
121
if self .DEBUG :
111
- print ("MOESIF can not fetch apiKey from cognitoIdentityId event, setting userId to None." )
122
+ print ("[moesif] can not fetch apiKey from cognitoIdentityId event, setting userId to None." )
112
123
except Exception as e :
113
124
if self .DEBUG :
114
- print ("MOESIF can not execute identify_user function, please check moesif settings." )
125
+ print ("[moesif] can not execute identify_user function, please check moesif settings." )
115
126
print (e )
116
127
end_time_get_user_id = datetime .utcnow ()
117
128
if self .DEBUG :
118
129
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 )))
119
130
return username
120
-
131
+
121
132
def get_company_id (self , event , context ):
122
133
"""Function to fetch CompanyId"""
123
134
start_time_get_company_id = datetime .utcnow ()
@@ -128,23 +139,23 @@ def get_company_id(self, event, context):
128
139
company_id = identify_company (event , context )
129
140
except Exception as e :
130
141
if self .DEBUG :
131
- print ("MOESIF can not execute identify_company function, please check moesif settings." )
142
+ print ("[moesif] can not execute identify_company function, please check moesif settings." )
132
143
print (e )
133
144
end_time_get_company_id = datetime .utcnow ()
134
145
if self .DEBUG :
135
146
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 )))
136
147
return company_id
137
-
148
+
138
149
def build_uri (self , event , payload_format_version_1_0 ):
139
150
140
- uri = event ['headers' ].get ('X-Forwarded-Proto' , event ['headers' ].get ('x-forwarded-proto' , 'http' )) + '://' + event ['headers' ].get ('Host' , event ['headers' ].get ('host' , 'localhost' ))
141
-
151
+ uri = event ['headers' ].get ('X-Forwarded-Proto' , event ['headers' ].get ('x-forwarded-proto' , 'http' )) + '://' + event ['headers' ].get ('Host' , event ['headers' ].get ('host' , 'localhost' ))
152
+
142
153
if payload_format_version_1_0 :
143
154
uri = uri + event .get ('path' , '/' )
144
155
if event .get ('multiValueQueryStringParameters' , {}):
145
- uri = uri + '?' + urlencode (event ['multiValueQueryStringParameters' ], doseq = True )
156
+ uri = uri + '?' + urlencode (event ['multiValueQueryStringParameters' ], doseq = True )
146
157
elif event .get ('queryStringParameters' , {}):
147
- uri = uri + '?' + urlencode (event ['queryStringParameters' ])
158
+ uri = uri + '?' + urlencode (event ['queryStringParameters' ])
148
159
else :
149
160
uri = uri + event .get ('rawPath' , '/' )
150
161
if event .get ('rawQueryString' , {}):
@@ -211,7 +222,7 @@ def before(self, event, context):
211
222
else :
212
223
request_verb = event .get ('requestContext' , {}).get ('http' , {}).get ('method' )
213
224
if request_verb is None :
214
- print ('MOESIF : [before] AWS Lambda trigger must be a Load Balancer or API Gateway See https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html or https://docs.aws.amazon.com/lambda/latest/dg/with-on-demand-https.html.' )
225
+ print ('[moesif] : [before] AWS Lambda trigger must be a Load Balancer or API Gateway See https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html or https://docs.aws.amazon.com/lambda/latest/dg/with-on-demand-https.html.' )
215
226
self .event = None
216
227
self .context = None
217
228
self .payload_version = None
@@ -224,23 +235,23 @@ def before(self, event, context):
224
235
req_headers = APIHelper .json_deserialize (event ['headers' ])
225
236
except Exception as e :
226
237
if self .DEBUG :
227
- print ('MOESIF Error while fetching request headers' )
238
+ print ('[moesif] Error while fetching request headers' )
228
239
print (e )
229
240
230
241
# Request Time
231
242
if self .is_payload_format_version_1_0 (self .payload_version ):
232
243
epoch = event and event .get ('requestContext' , {}).get ('requestTimeEpoch' )
233
244
else :
234
245
epoch = event and event .get ('requestContext' , {}).get ('timeEpoch' )
235
- if epoch is not None :
246
+ if epoch is not None :
236
247
# Dividing by 1000 to convert from ms to seconds and `.0` to preserve millisecond precision
237
248
request_time = datetime .utcfromtimestamp (epoch / 1000.0 )
238
249
else :
239
250
request_time = datetime .utcnow ()
240
-
251
+
241
252
# Request Body
242
253
req_body , req_transfer_encoding = self .process_body (event )
243
-
254
+
244
255
# Metadata
245
256
start_time_get_metadata = datetime .utcnow ()
246
257
try :
@@ -258,15 +269,15 @@ def before(self, event, context):
258
269
}
259
270
except :
260
271
if self .DEBUG :
261
- print ("MOESIF can not fetch default function_name and request_context from aws context, setting metadata to None." )
272
+ print ("[moesif] can not fetch default function_name and request_context from aws context, setting metadata to None." )
262
273
except Exception as e :
263
274
if self .DEBUG :
264
- print ("MOESIF can not execute GET_METADATA function, please check moesif settings." )
275
+ print ("[moesif] can not execute GET_METADATA function, please check moesif settings." )
265
276
print (e )
266
277
end_time_get_metadata = datetime .utcnow ()
267
278
if self .DEBUG :
268
279
print ("[moesif] Time took in fetching metadata in millisecond - " + str (get_time_took_in_ms (start_time_get_metadata , end_time_get_metadata )))
269
-
280
+
270
281
# User Id
271
282
start_time_identify_user = datetime .utcnow ()
272
283
self .user_id = self .get_user_id (event , context )
@@ -294,10 +305,10 @@ def before(self, event, context):
294
305
self .session_token = rc_api_key
295
306
except KeyError :
296
307
if self .DEBUG :
297
- print ("MOESIF can not fetch apiKey from aws event, setting session_token to None." )
308
+ print ("[moesif] can not fetch apiKey from aws event, setting session_token to None." )
298
309
except Exception as e :
299
310
if self .DEBUG :
300
- print ("MOESIF can not execute GET_SESSION_TOKEN function, please check moesif settings." )
311
+ print ("[moesif] can not execute GET_SESSION_TOKEN function, please check moesif settings." )
301
312
print (e )
302
313
303
314
# Api Version
@@ -312,12 +323,12 @@ def before(self, event, context):
312
323
api_version = context .function_version
313
324
except KeyError :
314
325
if self .DEBUG :
315
- print ("MOESIF can not fetch default function_version from aws context, setting api_version to None." )
326
+ print ("[moesif] can not fetch default function_version from aws context, setting api_version to None." )
316
327
except Exception as e :
317
328
if self .DEBUG :
318
- print ("MOESIF can not execute GET_API_VERSION function, please check moesif settings." )
329
+ print ("[moesif] can not execute GET_API_VERSION function, please check moesif settings." )
319
330
print (e )
320
-
331
+
321
332
# IpAddress
322
333
if self .is_payload_format_version_1_0 (self .payload_version ):
323
334
ip_address = event .get ('requestContext' , {}).get ('identity' , {}).get ('sourceIp' , None )
@@ -339,11 +350,12 @@ def before(self, event, context):
339
350
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 )))
340
351
# Return event, context
341
352
return event , context
342
-
353
+
343
354
def after (self , retval ):
344
355
"""This function runs after the handler is invoked, is passed the response and must return an response too."""
345
-
356
+
346
357
start_time_after_handler_function = datetime .utcnow ()
358
+ event_send = None
347
359
if self .event is not None :
348
360
# Response body
349
361
resp_body , resp_transfer_encoding = self .process_body (retval )
@@ -362,27 +374,27 @@ def after(self, retval):
362
374
company_id = self .company_id ,
363
375
session_token = self .session_token ,
364
376
metadata = self .metadata )
365
-
377
+
366
378
# Mask Event Model
367
379
try :
368
380
mask_event_model = self .moesif_options .get ('MASK_EVENT_MODEL' , None )
369
381
if mask_event_model is not None :
370
382
event_model = mask_event_model (event_model )
371
- except :
383
+ except Exception as e :
372
384
if self .DEBUG :
373
- print ("MOESIF Can not execute MASK_EVENT_MODEL function. Please check moesif settings." )
385
+ print ("[moesif] Can not execute MASK_EVENT_MODEL function. Please check moesif settings." , e )
374
386
375
387
# Skip Event
376
388
try :
377
389
skip_event = self .moesif_options .get ('SKIP' , None )
378
390
if skip_event is not None :
379
391
if skip_event (self .event , self .context ):
380
392
if self .DEBUG :
381
- print ('MOESIF Skip sending event to Moesif' )
393
+ print ('[moesif] Skip sending event to Moesif' )
382
394
return retval
383
- except :
395
+ except Exception as e :
384
396
if self .DEBUG :
385
- print ("MOESIF Having difficulty executing skip_event function. Please check moesif settings." )
397
+ print ("[moesif] Having difficulty executing skip_event function. Please check moesif settings." , e )
386
398
387
399
# Add direction field
388
400
event_model .direction = "Incoming"
@@ -391,17 +403,56 @@ def after(self, retval):
391
403
if self .DEBUG :
392
404
print ('Moesif Event Model:' )
393
405
print (json .dumps (self .event ))
394
-
395
- if self .DEBUG :
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
-
406
+
407
+ # Sampling Rate
408
+ try :
409
+ random_percentage = random .random () * 100
410
+ gv .sampling_percentage = gv .app_config .get_sampling_percentage (
411
+ event_model ,
412
+ gv .config ,
413
+ self .user_id ,
414
+ self .company_id ,
415
+ )
416
+
417
+ if gv .sampling_percentage >= random_percentage :
418
+ event_model .weight = 1 if gv .sampling_percentage == 0 else math .floor (
419
+ 100 / gv .sampling_percentage )
420
+
421
+ if self .DEBUG :
422
+ start_time_sending_event_w_rsp = datetime .utcnow ()
423
+ event_send = self .api_client .create_event (event_model )
424
+ end_time_sending_event_w_rsp = datetime .utcnow ()
425
+ print ("[moesif] sampling_percentage" + str (
426
+ gv .sampling_percentage ) + " and random percentage: " + str (random_percentage ))
427
+ print ("[moesif] Time took in sending event to moesif in millisecond - " + str (
428
+ get_time_took_in_ms (start_time_sending_event_w_rsp , end_time_sending_event_w_rsp )))
429
+ print ('[moesif] Event Sent successfully ' + str (event_send ))
430
+
431
+ else :
432
+ if datetime .utcnow () > gv .last_updated_time + timedelta (seconds = gv .refresh_config_time_seconds ):
433
+ event_send = self .api_client .create_event (event_model )
434
+ else :
435
+ self .api_client .create_event (event_model )
436
+
437
+ try :
438
+ # Check if we need to update config
439
+ new_config_etag = event_send ['x-moesif-config-etag' ]
440
+ if gv .config_etag is None or (gv .config_etag != new_config_etag ):
441
+ gv .config_etag = new_config_etag
442
+ gv .config = gv .app_config .get_config (self .api_client , self .DEBUG )
443
+ except (KeyError , TypeError , ValueError ) as ex :
444
+ # ignore the error because "event_send" is not set in non-blocking call
445
+ pass
446
+ finally :
447
+ gv .last_updated_time = datetime .utcnow ()
448
+
449
+ else :
450
+ if self .DEBUG :
451
+ print ("Skipped Event due to sampling percentage: " + str (
452
+ gv .sampling_percentage ) + " and random percentage: " + str (random_percentage ))
453
+ except Exception as ex :
454
+ print ("[moesif] Error when fetching sampling rate from app config" , ex )
455
+
405
456
end_time_after_handler_function = datetime .utcnow ()
406
457
if self .DEBUG :
407
458
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 )))
0 commit comments