@@ -27,11 +27,9 @@ def get_config(config_key):
2727 param = SSMParameter (os .environ ['FAILURE_INJECTION_PARAM' ])
2828 try :
2929 value = json .loads (param .value )
30- isEnabled = value ["isEnabled" ]
31- if not isEnabled :
30+ if not value ["isEnabled" ]:
3231 return 0
33- key_ = value [config_key ]
34- return key_
32+ return value [config_key ], value .get ('rate' , 1 )
3533 except InvalidParameterError as e :
3634 # key does not exist in SSM
3735 raise InvalidParameterError ("{} does not exist in SSM" .format (e ))
@@ -42,18 +40,14 @@ def get_config(config_key):
4240
4341def corrupt_delay (func ):
4442 def latency (* args , ** kw ):
45- delay = get_config ('delay' )
46- print (delay )
43+ delay , rate = get_config ('delay' )
44+ print (" delay: {0}, rate: {1}" . format ( delay , rate ) )
4745 start = time .time ()
48- # if delay exist, delaying with that value
49- if delay > 0 :
50- time .sleep (delay / 1000.0 )
51- # if delay = -1 make random delays
52- elif delay < 0 :
53- # add latency approx 50% of the time
54- if random .random () > 0.5 :
55- # random sleep time between 1 and 10 seconds
56- time .sleep (random .randint (1 , 10 ))
46+ # if delay and rate exist, delaying with that value at that rate
47+ if delay > 0 and rate >= 0 :
48+ # add latency approx rate% of the time
49+ if random .random () <= rate :
50+ time .sleep (delay / 1000.0 )
5751
5852 result = func (* args , ** kw )
5953 end = time .time ()
@@ -69,19 +63,27 @@ def latency(*args, **kw):
6963def corrupt_expection (func ):
7064 def wrapper (* args , ** kwargs ):
7165 result = func (* args , ** kwargs )
72- exception_msg = get_config ('exception_msg' )
73- print ("exception_msg from config {}" .format (exception_msg ))
74- print ("corrupting now" )
75- raise Exception (exception_msg )
66+ exception_msg , rate = get_config ('exception_msg' )
67+ print ("exception_msg from config {0} with a rate of {1}" .format (exception_msg , rate ))
68+ # add injection approx rate% of the time
69+ if random .random () <= rate :
70+ print ("corrupting now" )
71+ raise Exception (exception_msg )
72+ else :
73+ return result
7674 return wrapper
7775
7876
7977def corrupt_statuscode (func ):
8078 def wrapper (* args , ** kwargs ):
8179 result = func (* args , ** kwargs )
82- error_code = get_config ('error_code' )
83- print ("Error from config {}" .format (error_code ))
84- print ("corrupting now" )
85- result ['statusCode' ] = error_code
86- return result
80+ error_code , rate = get_config ('error_code' )
81+ print ("Error from config {0} at a rate of {1}" .format (error_code , rate ))
82+ # add injection approx rate% of the time
83+ if random .random () <= rate :
84+ print ("corrupting now" )
85+ result ['statusCode' ] = error_code
86+ return result
87+ else :
88+ return result
8789 return wrapper
0 commit comments