Skip to content

Commit efa480f

Browse files
authored
fix(logs): Fixing log messages (#201)
1 parent 3f53ed9 commit efa480f

13 files changed

+211
-136
lines changed

src/Optimizely/Bucketer.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2019, Optimizely
3+
* Copyright 2016-2020, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -133,7 +133,7 @@ private function findBucket($bucketingId, $userId, $parentId, $trafficAllocation
133133
* Determine variation the user should be put in.
134134
*
135135
* @param $config ProjectConfigInterface Configuration for the project.
136-
* @param $experiment Experiment Experiment in which user is to be bucketed.
136+
* @param $experiment Experiment Experiment or Rollout rule in which user is to be bucketed.
137137
* @param $bucketingId string A customer-assigned value used to create the key for the murmur hash.
138138
* @param $userId string User identifier.
139139
*
@@ -146,6 +146,7 @@ public function bucket(ProjectConfigInterface $config, Experiment $experiment, $
146146
}
147147

148148
// Determine if experiment is in a mutually exclusive group.
149+
// This will not affect evaluation of rollout rules.
149150
if ($experiment->isInMutexGroup()) {
150151
$group = $config->getGroup($experiment->getGroupId());
151152

@@ -188,19 +189,9 @@ public function bucket(ProjectConfigInterface $config, Experiment $experiment, $
188189
if (!empty($variationId)) {
189190
$variation = $config->getVariationFromId($experiment->getKey(), $variationId);
190191

191-
$this->_logger->log(
192-
Logger::INFO,
193-
sprintf(
194-
'User "%s" is in variation %s of experiment %s.',
195-
$userId,
196-
$variation->getKey(),
197-
$experiment->getKey()
198-
)
199-
);
200192
return $variation;
201193
}
202194

203-
$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no variation.', $userId));
204195
return null;
205196
}
206197
}

src/Optimizely/DecisionService/DecisionService.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2017-2019, Optimizely Inc and Contributors
3+
* Copyright 2017-2020, Optimizely Inc and Contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -152,7 +152,7 @@ public function getVariation(ProjectConfigInterface $projectConfig, Experiment $
152152
}
153153
}
154154

155-
if (!Validator::isUserInExperiment($projectConfig, $experiment, $attributes, $this->_logger)) {
155+
if (!Validator::doesUserMeetAudienceConditions($projectConfig, $experiment, $attributes, $this->_logger)) {
156156
$this->_logger->log(
157157
Logger::INFO,
158158
sprintf('User "%s" does not meet conditions to be in experiment "%s".', $userId, $experiment->getKey())
@@ -161,9 +161,21 @@ public function getVariation(ProjectConfigInterface $projectConfig, Experiment $
161161
}
162162

163163
$variation = $this->_bucketer->bucket($projectConfig, $experiment, $bucketingId, $userId);
164-
if (!is_null($variation)) {
164+
if ($variation === null) {
165+
$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no variation.', $userId));
166+
} else {
165167
$this->saveVariation($experiment, $variation, $userProfile);
168+
$this->_logger->log(
169+
Logger::INFO,
170+
sprintf(
171+
'User "%s" is in variation %s of experiment %s.',
172+
$userId,
173+
$variation->getKey(),
174+
$experiment->getKey()
175+
)
176+
);
166177
}
178+
167179
return $variation;
168180
}
169181

@@ -298,40 +310,40 @@ public function getVariationForFeatureRollout(ProjectConfigInterface $projectCon
298310

299311
// Evaluate all rollout rules except for last one
300312
for ($i = 0; $i < sizeof($rolloutRules) - 1; $i++) {
301-
$experiment = $rolloutRules[$i];
313+
$rolloutRule = $rolloutRules[$i];
302314

303315
// Evaluate if user meets the audience condition of this rollout rule
304-
if (!Validator::isUserInExperiment($projectConfig, $experiment, $userAttributes, $this->_logger)) {
316+
if (!Validator::doesUserMeetAudienceConditions($projectConfig, $rolloutRule, $userAttributes, $this->_logger, 'Optimizely\Enums\RolloutAudienceEvaluationLogs', $i + 1)) {
305317
$this->_logger->log(
306318
Logger::DEBUG,
307-
sprintf("User '%s' did not meet the audience conditions to be in rollout rule '%s'.", $userId, $experiment->getKey())
319+
sprintf("User '%s' does not meet conditions for targeting rule %s.", $userId, $i+1)
308320
);
309321
// Evaluate this user for the next rule
310322
continue;
311323
}
312324

313325
// Evaluate if user satisfies the traffic allocation for this rollout rule
314-
$variation = $this->_bucketer->bucket($projectConfig, $experiment, $bucketing_id, $userId);
326+
$variation = $this->_bucketer->bucket($projectConfig, $rolloutRule, $bucketing_id, $userId);
315327
if ($variation && $variation->getKey()) {
316-
return new FeatureDecision($experiment, $variation, FeatureDecision::DECISION_SOURCE_ROLLOUT);
328+
return new FeatureDecision($rolloutRule, $variation, FeatureDecision::DECISION_SOURCE_ROLLOUT);
317329
}
318330
break;
319331
}
320332
// Evaluate Everyone Else Rule / Last Rule now
321-
$experiment = $rolloutRules[sizeof($rolloutRules)-1];
333+
$rolloutRule = $rolloutRules[sizeof($rolloutRules) - 1];
322334

323335
// Evaluate if user meets the audience condition of Everyone Else Rule / Last Rule now
324-
if (!Validator::isUserInExperiment($projectConfig, $experiment, $userAttributes, $this->_logger)) {
336+
if (!Validator::doesUserMeetAudienceConditions($projectConfig, $rolloutRule, $userAttributes, $this->_logger, 'Optimizely\Enums\RolloutAudienceEvaluationLogs', 'Everyone Else')) {
325337
$this->_logger->log(
326338
Logger::DEBUG,
327-
sprintf("User '%s' did not meet the audience conditions to be in rollout rule '%s'.", $userId, $experiment->getKey())
339+
sprintf("User '%s' does not meet conditions for targeting rule 'Everyone Else'.", $userId)
328340
);
329341
return null;
330342
}
331343

332-
$variation = $this->_bucketer->bucket($projectConfig, $experiment, $bucketing_id, $userId);
344+
$variation = $this->_bucketer->bucket($projectConfig, $rolloutRule, $bucketing_id, $userId);
333345
if ($variation && $variation->getKey()) {
334-
return new FeatureDecision($experiment, $variation, FeatureDecision::DECISION_SOURCE_ROLLOUT);
346+
return new FeatureDecision($rolloutRule, $variation, FeatureDecision::DECISION_SOURCE_ROLLOUT);
335347
}
336348
return null;
337349
}

src/Optimizely/Enums/AudienceEvaluationLogs.php renamed to src/Optimizely/Enums/CommonAudienceEvaluationLogs.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2019, Optimizely
3+
* Copyright 2019-2020, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -17,11 +17,9 @@
1717

1818
namespace Optimizely\Enums;
1919

20-
class AudienceEvaluationLogs
20+
class CommonAudienceEvaluationLogs
2121
{
2222
const AUDIENCE_EVALUATION_RESULT = "Audience \"%s\" evaluated to %s.";
23-
const AUDIENCE_EVALUATION_RESULT_COMBINED = "Audiences for experiment \"%s\" collectively evaluated to %s.";
24-
const EVALUATING_AUDIENCES_COMBINED = "Evaluating audiences for experiment \"%s\": %s.";
2523
const EVALUATING_AUDIENCE = "Starting to evaluate audience \"%s\" with conditions: %s.";
2624
const INFINITE_ATTRIBUTE_VALUE = "Audience condition %s evaluated to UNKNOWN because the number value for user attribute \"%s\" is not in the range [-2^53, +2^53].";
2725
const MISSING_ATTRIBUTE_VALUE = "Audience condition %s evaluated to UNKNOWN because no value was passed for user attribute \"%s\".";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright 2020, Optimizely
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Optimizely\Enums;
19+
20+
class ExperimentAudienceEvaluationLogs extends CommonAudienceEvaluationLogs
21+
{
22+
const AUDIENCE_EVALUATION_RESULT_COMBINED = "Audiences for experiment \"%s\" collectively evaluated to %s.";
23+
const EVALUATING_AUDIENCES_COMBINED = "Evaluating audiences for experiment \"%s\": %s.";
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright 2020, Optimizely
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Optimizely\Enums;
19+
20+
class RolloutAudienceEvaluationLogs extends CommonAudienceEvaluationLogs
21+
{
22+
const AUDIENCE_EVALUATION_RESULT_COMBINED = "Audiences for rule %s collectively evaluated to %s.";
23+
const EVALUATING_AUDIENCES_COMBINED = "Evaluating audiences for rule %s: %s.";
24+
}

src/Optimizely/Optimizely.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ private function getFeatureVariableValueFromVariation($featureFlagKey, $variable
931931
if ($variation === null) {
932932
$this->_logger->log(
933933
Logger::INFO,
934-
"User '{$userId}'is not in any variation, ".
934+
"User '{$userId}' is not in experiment or rollout, ".
935935
"returning default value '{$variableValue}'."
936936
);
937937
} else {
@@ -941,21 +941,20 @@ private function getFeatureVariableValueFromVariation($featureFlagKey, $variable
941941
$variableValue = $variableUsage->getValue();
942942
$this->_logger->log(
943943
Logger::INFO,
944-
"Returning variable value '{$variableValue}' for variation '{$variation->getKey()}' ".
945-
"of feature flag '{$featureFlagKey}'"
944+
"Returning variable value '{$variableValue}' for variable key '{$variableKey}' ".
945+
"of feature flag '{$featureFlagKey}'."
946946
);
947947
} else {
948948
$this->_logger->log(
949949
Logger::INFO,
950-
"Variable '{$variableKey}' is not used in variation '{$variation->getKey()}', ".
951-
"returning default value '{$variableValue}'."
950+
"Variable value is not defined. Returning the default variable value '{$variableValue}'."
952951
);
953952
}
954953
} else {
955954
$this->_logger->log(
956955
Logger::INFO,
957-
"Feature '{$featureFlagKey}' for variation '{$variation->getKey()}' is not enabled, ".
958-
"returning default value '{$variableValue}'."
956+
"Feature '{$featureFlagKey}' is not enabled for user '{$userId}'. ".
957+
"Returning the default variable value '{$variableValue}'."
959958
);
960959
}
961960
}

src/Optimizely/Utils/CustomAttributeConditionEvaluator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2018-2019, Optimizely
3+
* Copyright 2018-2020, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -18,7 +18,7 @@
1818
namespace Optimizely\Utils;
1919

2020
use Monolog\Logger;
21-
use Optimizely\Enums\AudienceEvaluationLogs as logs;
21+
use Optimizely\Enums\CommonAudienceEvaluationLogs as logs;
2222
use Optimizely\Utils\Validator;
2323

2424
class CustomAttributeConditionEvaluator

src/Optimizely/Utils/Validator.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Monolog\Logger;
2121
use Optimizely\Config\ProjectConfigInterface;
2222
use Optimizely\Entity\Experiment;
23-
use Optimizely\Enums\AudienceEvaluationLogs;
2423
use Optimizely\Logger\LoggerInterface;
2524
use Optimizely\Utils\ConditionTreeEvaluator;
2625
use Optimizely\Utils\CustomAttributeConditionEvaluator;
@@ -133,30 +132,40 @@ public static function areEventTagsValid($eventTags)
133132

134133
/**
135134
* @param $config ProjectConfigInterface Configuration for the project.
136-
* @param $experiment Experiment Entity representing the experiment.
135+
* @param $experiment Experiment Entity representing the experiment or rollout rule.
137136
* @param $userAttributes array Attributes of the user.
138137
* @param $logger LoggerInterface.
138+
* @param $loggingClass String Class holding log strings with placeholders.
139+
* @param $loggingKey String Identifier of an experiment/rollout rule.
139140
*
140141
* @return boolean Representing whether user meets audience conditions to be in experiment or not.
141142
*/
142-
public static function isUserInExperiment($config, $experiment, $userAttributes, $logger)
143+
public static function doesUserMeetAudienceConditions($config, $experiment, $userAttributes, $logger, $loggingClass = null, $loggingKey = null)
143144
{
145+
if ($loggingClass === null) {
146+
$loggingClass = 'Optimizely\Enums\ExperimentAudienceEvaluationLogs';
147+
}
148+
149+
if ($loggingKey === null) {
150+
$loggingKey = $experiment->getKey();
151+
}
152+
144153
$audienceConditions = $experiment->getAudienceConditions();
145154
if ($audienceConditions === null) {
146155
$audienceConditions = $experiment->getAudienceIds();
147156
}
148157

149158
$logger->log(Logger::DEBUG, sprintf(
150-
AudienceEvaluationLogs::EVALUATING_AUDIENCES_COMBINED,
151-
$experiment->getKey(),
159+
$loggingClass::EVALUATING_AUDIENCES_COMBINED,
160+
$loggingKey,
152161
json_encode($audienceConditions)
153162
));
154163

155164
// Return true if experiment is not targeted to any audience.
156165
if (empty($audienceConditions)) {
157166
$logger->log(Logger::INFO, sprintf(
158-
AudienceEvaluationLogs::AUDIENCE_EVALUATION_RESULT_COMBINED,
159-
$experiment->getKey(),
167+
$loggingClass::AUDIENCE_EVALUATION_RESULT_COMBINED,
168+
$loggingKey,
160169
'TRUE'
161170
));
162171
return true;
@@ -171,14 +180,14 @@ public static function isUserInExperiment($config, $experiment, $userAttributes,
171180
return $customAttrCondEval->evaluate($leafCondition);
172181
};
173182

174-
$evaluateAudience = function ($audienceId) use ($config, $evaluateCustomAttr, $logger) {
183+
$evaluateAudience = function ($audienceId) use ($config, $evaluateCustomAttr, $logger, $loggingClass) {
175184
$audience = $config->getAudience($audienceId);
176185
if ($audience === null) {
177186
return null;
178187
}
179188

180189
$logger->log(Logger::DEBUG, sprintf(
181-
AudienceEvaluationLogs::EVALUATING_AUDIENCE,
190+
$loggingClass::EVALUATING_AUDIENCE,
182191
$audienceId,
183192
json_encode($audience->getConditionsList())
184193
));
@@ -188,7 +197,7 @@ public static function isUserInExperiment($config, $experiment, $userAttributes,
188197
$resultStr = $result === null ? 'UNKNOWN' : strtoupper(var_export($result, true));
189198

190199
$logger->log(Logger::DEBUG, sprintf(
191-
AudienceEvaluationLogs::AUDIENCE_EVALUATION_RESULT,
200+
$loggingClass::AUDIENCE_EVALUATION_RESULT,
192201
$audienceId,
193202
$resultStr
194203
));
@@ -201,8 +210,8 @@ public static function isUserInExperiment($config, $experiment, $userAttributes,
201210
$evalResult = $evalResult || false;
202211

203212
$logger->log(Logger::INFO, sprintf(
204-
AudienceEvaluationLogs::AUDIENCE_EVALUATION_RESULT_COMBINED,
205-
$experiment->getKey(),
213+
$loggingClass::AUDIENCE_EVALUATION_RESULT_COMBINED,
214+
$loggingKey,
206215
strtoupper(var_export($evalResult, true))
207216
));
208217

0 commit comments

Comments
 (0)