11
11
# See the License for the specific language governing permissions and
12
12
# limitations under the License.
13
13
14
- import sys
15
14
from collections import namedtuple
16
15
17
16
from . import bucketer
21
20
from .helpers import validator
22
21
from .user_profile import UserProfile
23
22
24
-
25
23
Decision = namedtuple ('Decision' , 'experiment variation source' )
26
24
DECISION_SOURCE_EXPERIMENT = 'experiment'
27
25
DECISION_SOURCE_ROLLOUT = 'rollout'
@@ -68,8 +66,7 @@ def get_forced_variation(self, experiment, user_id):
68
66
variation_key = forced_variations .get (user_id )
69
67
variation = self .config .get_variation_from_key (experiment .key , variation_key )
70
68
if variation :
71
- self .config .logger .log (enums .LogLevels .INFO ,
72
- 'User "%s" is forced in variation "%s".' % (user_id , variation_key ))
69
+ self .logger .info ('User "%s" is forced in variation "%s".' % (user_id , variation_key ))
73
70
return variation
74
71
75
72
return None
@@ -91,9 +88,11 @@ def get_stored_variation(self, experiment, user_profile):
91
88
if variation_id :
92
89
variation = self .config .get_variation_from_id (experiment .key , variation_id )
93
90
if variation :
94
- self .config .logger .log (enums .LogLevels .INFO ,
95
- 'Found a stored decision. User "%s" is in variation "%s" of experiment "%s".' %
96
- (user_id , variation .key , experiment .key ))
91
+ self .logger .info ('Found a stored decision. User "%s" is in variation "%s" of experiment "%s".' % (
92
+ user_id ,
93
+ variation .key ,
94
+ experiment .key
95
+ ))
97
96
return variation
98
97
99
98
return None
@@ -119,7 +118,7 @@ def get_variation(self, experiment, user_id, attributes, ignore_user_profile=Fal
119
118
120
119
# Check if experiment is running
121
120
if not experiment_helper .is_experiment_running (experiment ):
122
- self .logger .log ( enums . LogLevels . INFO , 'Experiment "%s" is not running.' % experiment .key )
121
+ self .logger .info ( 'Experiment "%s" is not running.' % experiment .key )
123
122
return None
124
123
125
124
# Check if the user is forced into a variation
@@ -138,11 +137,7 @@ def get_variation(self, experiment, user_id, attributes, ignore_user_profile=Fal
138
137
try :
139
138
retrieved_profile = self .user_profile_service .lookup (user_id )
140
139
except :
141
- error = sys .exc_info ()[1 ]
142
- self .logger .log (
143
- enums .LogLevels .ERROR ,
144
- 'Unable to retrieve user profile for user "%s" as lookup failed. Error: %s' % (user_id , str (error ))
145
- )
140
+ self .logger .exception ('Unable to retrieve user profile for user "%s" as lookup failed.' % user_id )
146
141
retrieved_profile = None
147
142
148
143
if validator .is_user_profile_valid (retrieved_profile ):
@@ -151,14 +146,14 @@ def get_variation(self, experiment, user_id, attributes, ignore_user_profile=Fal
151
146
if variation :
152
147
return variation
153
148
else :
154
- self .logger .log ( enums . LogLevels . WARNING , 'User profile has invalid format.' )
149
+ self .logger .warning ( 'User profile has invalid format.' )
155
150
156
151
# Bucket user and store the new decision
157
152
if not audience_helper .is_user_in_experiment (self .config , experiment , attributes ):
158
- self .logger .log (
159
- enums . LogLevels . INFO ,
160
- 'User "%s" does not meet conditions to be in experiment "%s".' % ( user_id , experiment . key )
161
- )
153
+ self .logger .info ( 'User "%s" does not meet conditions to be in experiment "%s".' % (
154
+ user_id ,
155
+ experiment . key
156
+ ))
162
157
return None
163
158
164
159
# Determine bucketing ID to be used
@@ -172,9 +167,7 @@ def get_variation(self, experiment, user_id, attributes, ignore_user_profile=Fal
172
167
user_profile .save_variation_for_experiment (experiment .id , variation .id )
173
168
self .user_profile_service .save (user_profile .__dict__ )
174
169
except :
175
- error = sys .exc_info ()[1 ]
176
- self .logger .log (enums .LogLevels .ERROR ,
177
- 'Unable to save user profile for user "%s". Error: %s' % (user_id , str (error )))
170
+ self .logger .exception ('Unable to save user profile for user "%s".' % user_id )
178
171
return variation
179
172
180
173
return None
@@ -199,25 +192,27 @@ def get_variation_for_rollout(self, rollout, user_id, attributes=None):
199
192
200
193
# Check if user meets audience conditions for targeting rule
201
194
if not audience_helper .is_user_in_experiment (self .config , experiment , attributes ):
202
- self .logger .log (
203
- enums . LogLevels . DEBUG ,
204
- 'User "%s" does not meet conditions for targeting rule %s.' % ( user_id , idx + 1 )
205
- )
195
+ self .logger .debug ( 'User "%s" does not meet conditions for targeting rule %s.' % (
196
+ user_id ,
197
+ idx + 1
198
+ ))
206
199
continue
207
200
208
- self .logger .log ( enums . LogLevels . DEBUG , 'User "%s" meets conditions for targeting rule %s.' % (user_id , idx + 1 ))
201
+ self .logger .debug ( 'User "%s" meets conditions for targeting rule %s.' % (user_id , idx + 1 ))
209
202
# Determine bucketing ID to be used
210
203
bucketing_id = self ._get_bucketing_id (user_id , attributes )
211
204
variation = self .bucketer .bucket (experiment , user_id , bucketing_id )
212
205
if variation :
213
- self .logger .log (enums .LogLevels .DEBUG ,
214
- 'User "%s" is in variation %s of experiment %s.' % (user_id , variation .key , experiment .key ))
206
+ self .logger .debug ('User "%s" is in variation %s of experiment %s.' % (
207
+ user_id ,
208
+ variation .key ,
209
+ experiment .key
210
+ ))
215
211
return Decision (experiment , variation , DECISION_SOURCE_ROLLOUT )
216
212
else :
217
213
# Evaluate no further rules
218
- self .logger .log (enums .LogLevels .DEBUG ,
219
- 'User "%s" is not in the traffic group for the targeting else. '
220
- 'Checking "Everyone Else" rule now.' % user_id )
214
+ self .logger .debug ('User "%s" is not in the traffic group for the targeting else. '
215
+ 'Checking "Everyone Else" rule now.' % user_id )
221
216
break
222
217
223
218
# Evaluate last rule i.e. "Everyone Else" rule
@@ -229,8 +224,7 @@ def get_variation_for_rollout(self, rollout, user_id, attributes=None):
229
224
bucketing_id = self ._get_bucketing_id (user_id , attributes )
230
225
variation = self .bucketer .bucket (everyone_else_experiment , user_id , bucketing_id )
231
226
if variation :
232
- self .logger .log (enums .LogLevels .DEBUG ,
233
- 'User "%s" meets conditions for targeting rule "Everyone Else".' % user_id )
227
+ self .logger .debug ('User "%s" meets conditions for targeting rule "Everyone Else".' % user_id )
234
228
return Decision (everyone_else_experiment , variation , DECISION_SOURCE_ROLLOUT )
235
229
236
230
return Decision (None , None , DECISION_SOURCE_ROLLOUT )
@@ -250,14 +244,17 @@ def get_experiment_in_group(self, group, bucketing_id):
250
244
if experiment_id :
251
245
experiment = self .config .get_experiment_from_id (experiment_id )
252
246
if experiment :
253
- self .logger .log (enums .LogLevels .INFO ,
254
- 'User with bucketing ID "%s" is in experiment %s of group %s.' %
255
- (bucketing_id , experiment .key , group .id ))
247
+ self .logger .info ('User with bucketing ID "%s" is in experiment %s of group %s.' % (
248
+ bucketing_id ,
249
+ experiment .key ,
250
+ group .id
251
+ ))
256
252
return experiment
257
253
258
- self .logger .log (enums .LogLevels .INFO ,
259
- 'User with bucketing ID "%s" is not in any experiments of group %s.' %
260
- (bucketing_id , group .id ))
254
+ self .logger .info ('User with bucketing ID "%s" is not in any experiments of group %s.' % (
255
+ bucketing_id ,
256
+ group .id
257
+ ))
261
258
262
259
return None
263
260
@@ -286,10 +283,13 @@ def get_variation_for_feature(self, feature, user_id, attributes=None):
286
283
variation = self .get_variation (experiment , user_id , attributes )
287
284
288
285
if variation :
289
- self .logger .log (enums .LogLevels .DEBUG ,
290
- 'User "%s" is in variation %s of experiment %s.' % (user_id , variation .key , experiment .key ))
286
+ self .logger .debug ('User "%s" is in variation %s of experiment %s.' % (
287
+ user_id ,
288
+ variation .key ,
289
+ experiment .key
290
+ ))
291
291
else :
292
- self .logger .log ( enums . LogLevels . ERROR , enums .Errors .INVALID_GROUP_ID_ERROR .format ('_get_variation_for_feature' ))
292
+ self .logger .error ( enums .Errors .INVALID_GROUP_ID_ERROR .format ('_get_variation_for_feature' ))
293
293
294
294
# Next check if the feature is being experimented on
295
295
elif feature .experimentIds :
@@ -299,8 +299,11 @@ def get_variation_for_feature(self, feature, user_id, attributes=None):
299
299
variation = self .get_variation (experiment , user_id , attributes )
300
300
301
301
if variation :
302
- self .logger .log (enums .LogLevels .DEBUG ,
303
- 'User "%s" is in variation %s of experiment %s.' % (user_id , variation .key , experiment .key ))
302
+ self .logger .debug ('User "%s" is in variation %s of experiment %s.' % (
303
+ user_id ,
304
+ variation .key ,
305
+ experiment .key
306
+ ))
304
307
305
308
# Next check if user is part of a rollout
306
309
if not variation and feature .rolloutId :
0 commit comments