Skip to content

Commit 16bf801

Browse files
authored
fix(logs): Fixing log messages for Targeted Rollouts and feature variable evaluation. (#337)
1 parent f94e079 commit 16bf801

File tree

8 files changed

+174
-184
lines changed

8 files changed

+174
-184
lines changed

Sources/Data Model/Group.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -32,7 +32,7 @@ struct Group: Codable, Equatable {
3232

3333
extension Group {
3434

35-
func getExperiemnt(id: String) -> Experiment? {
35+
func getExperiment(id: String) -> Experiment? {
3636
return experiments.filter { $0.id == id }.first
3737
}
3838

Sources/Implementation/DefaultBucketer.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -33,7 +33,7 @@ class DefaultBucketer: OPTBucketer {
3333

3434
// check for mutex
3535

36-
let group = config.project.groups.filter { $0.getExperiemnt(id: experiment.id) != nil }.first
36+
let group = config.project.groups.filter { $0.getExperiment(id: experiment.id) != nil }.first
3737

3838
if let group = group {
3939
switch group.policy {
@@ -61,18 +61,16 @@ class DefaultBucketer: OPTBucketer {
6161
// bucket to variation only if experiment passes Mutex check
6262

6363
if let variation = bucketToVariation(experiment: experiment, bucketingId: bucketingId) {
64-
logger.i(.userBucketedIntoVariationInExperiment(bucketingId, experiment.key, variation.key))
6564
return variation
6665
} else {
67-
logger.i(.userNotBucketedIntoVariationInExperiment(bucketingId, experiment.key))
6866
return nil
6967
}
7068
}
7169

7270
func bucketToExperiment(config: ProjectConfig, group: Group, bucketingId: String) -> Experiment? {
7371
let hashId = makeHashIdFromBucketingId(bucketingId: bucketingId, entityId: group.id)
7472
let bucketValue = self.generateBucketValue(bucketingId: hashId)
75-
logger.d(.userAssignedToExperimentBucketValue(bucketValue, bucketingId))
73+
logger.d(.userAssignedToBucketValue(bucketValue, bucketingId))
7674

7775
if group.trafficAllocation.count == 0 {
7876
logger.e(.groupHasNoTrafficAllocation(group.id))
@@ -97,7 +95,7 @@ class DefaultBucketer: OPTBucketer {
9795
func bucketToVariation(experiment: Experiment, bucketingId: String) -> Variation? {
9896
let hashId = makeHashIdFromBucketingId(bucketingId: bucketingId, entityId: experiment.id)
9997
let bucketValue = generateBucketValue(bucketingId: hashId)
100-
logger.d(.userAssignedToVariationBucketValue(bucketValue, bucketingId))
98+
logger.d(.userAssignedToBucketValue(bucketValue, bucketingId))
10199

102100
if experiment.trafficAllocation.count == 0 {
103101
logger.e(.experimentHasNoTrafficAllocation(experiment.key))

Sources/Implementation/DefaultDecisionService.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ class DefaultDecisionService: OPTDecisionService {
3939
return nil
4040
}
4141

42-
// ---- check for whitelisted variation registered at runtime ----
42+
// ---- check if the user is forced into a variation ----
4343
if let variationId = config.getForcedVariation(experimentKey: experiment.key, userId: userId)?.id,
4444
let variation = experiment.getVariation(id: variationId) {
4545
return variation
4646
}
4747

48-
// ---- check if the experiment has forced variation ----
48+
// ---- check to see if user is white-listed for a certain variation ----
4949
if let variationKey = experiment.forcedVariations[userId] {
5050
if let variation = experiment.getVariation(key: variationKey) {
5151
logger.i(.forcedVariationFound(variationKey, userId))
5252
return variation
5353
}
5454

55-
// mapped to invalid variation - ignore and continue for other deciesions
55+
// mapped to invalid variation - ignore and continue for other decisions
5656
logger.e(.forcedVariationFoundButInvalid(variationKey, userId))
5757
}
5858

@@ -65,22 +65,26 @@ class DefaultDecisionService: OPTDecisionService {
6565

6666
var bucketedVariation: Variation?
6767
// ---- check if the user passes audience targeting before bucketing ----
68-
if isInExperiment(config: config, experiment: experiment, userId: userId, attributes: attributes) {
68+
if doesMeetAudienceConditions(config: config, experiment: experiment, userId: userId, attributes: attributes) {
6969
// bucket user into a variation
7070
bucketedVariation = bucketer.bucketExperiment(config: config, experiment: experiment, bucketingId: bucketingId)
7171

7272
if let bucketedVariation = bucketedVariation {
73+
logger.i(.userBucketedIntoVariationInExperiment(userId, experiment.key, bucketedVariation.key))
7374
// save to user profile
7475
self.saveProfile(userId: userId, experimentId: experimentId, variationId: bucketedVariation.id)
76+
} else {
77+
logger.i(.userNotBucketedIntoVariation(userId))
7578
}
79+
7680
} else {
7781
logger.i(.userNotInExperiment(userId, experiment.key))
7882
}
7983

8084
return bucketedVariation
8185
}
8286

83-
func isInExperiment(config: ProjectConfig, experiment: Experiment, userId: String, attributes: OptimizelyAttributes, logType: Constants.EvaluationLogType = .experiment, loggingKey: String? = nil) -> Bool {
87+
func doesMeetAudienceConditions(config: ProjectConfig, experiment: Experiment, userId: String, attributes: OptimizelyAttributes, logType: Constants.EvaluationLogType = .experiment, loggingKey: String? = nil) -> Bool {
8488

8589
var result = true // success as default (no condition, etc)
8690
let evType = logType.rawValue
@@ -133,19 +137,13 @@ class DefaultDecisionService: OPTDecisionService {
133137
//1. Attempt to bucket user into experiment using feature flag.
134138
// Check if the feature flag is under an experiment and the the user is bucketed into one of these experiments
135139
if let pair = getVariationForFeatureExperiment(config: config, featureFlag: featureFlag, userId: userId, attributes: attributes) {
136-
logger.d(.userInFeatureExperiment(userId, pair.variation?.key ?? "unknown", pair.experiment?.key ?? "unknown", featureFlag.key))
137140
return pair
138-
} else {
139-
logger.d(.userNotInFeatureExperiment(userId, featureFlag.key))
140141
}
141142

142143
//2. Attempt to bucket user into rollout using the feature flag.
143144
// Check if the feature flag has rollout and the user is bucketed into one of it's rules
144145
if let variation = getVariationForFeatureRollout(config: config, featureFlag: featureFlag, userId: userId, attributes: attributes) {
145-
logger.d(.userInRollout(userId, featureFlag.key))
146146
return (nil, variation)
147-
} else {
148-
logger.d(.userNotInRollout(userId, featureFlag.key))
149147
}
150148

151149
return nil
@@ -202,7 +200,7 @@ class DefaultDecisionService: OPTDecisionService {
202200
for index in 0..<rolloutRules.count.advanced(by: -1) {
203201
let loggingKey = index + 1
204202
let experiment = rolloutRules[index]
205-
if isInExperiment(config: config, experiment: experiment, userId: userId, attributes: attributes, logType: .rolloutRule, loggingKey: "\(loggingKey)") {
203+
if doesMeetAudienceConditions(config: config, experiment: experiment, userId: userId, attributes: attributes, logType: .rolloutRule, loggingKey: "\(loggingKey)") {
206204
logger.d(.userMeetsConditionsForTargetingRule(userId, loggingKey))
207205
if let variation = bucketer.bucketExperiment(config: config, experiment: experiment, bucketingId: bucketingId) {
208206
logger.d(.userBucketedIntoTargetingRule(userId, loggingKey))
@@ -217,7 +215,7 @@ class DefaultDecisionService: OPTDecisionService {
217215
// Evaluate fall back rule / last rule now
218216
let experiment = rolloutRules[rolloutRules.count - 1]
219217

220-
if isInExperiment(config: config, experiment: experiment, userId: userId, attributes: attributes, logType: .rolloutRule, loggingKey: "Everyone Else") {
218+
if doesMeetAudienceConditions(config: config, experiment: experiment, userId: userId, attributes: attributes, logType: .rolloutRule, loggingKey: "Everyone Else") {
221219
if let variation = bucketer.bucketExperiment(config: config, experiment: experiment, bucketingId: bucketingId) {
222220
logger.d(.userBucketedIntoEveryoneTargetingRule(userId))
223221

Sources/Optimizely/OptimizelyClient.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,10 @@ open class OptimizelyClient: NSObject {
535535
if let featureVariable = decision.variation?.variables?.filter({$0.id == variable.id}).first {
536536
if let featureEnabled = decision.variation?.featureEnabled, featureEnabled {
537537
featureValue = featureVariable.value
538-
539-
logger.i(.userReceivedVariableValue(userId, featureKey, variableKey, featureValue))
538+
logger.i(.userReceivedVariableValue(featureValue, variableKey, featureKey))
540539
} else {
541540
logger.i(.featureNotEnabledReturnDefaultVariableValue(userId, featureKey, variableKey))
542541
}
543-
} else {
544-
logger.i(.variableNotUsedReturnDefaultVariableValue(variableKey))
545542
}
546543
} else {
547544
logger.i(.userReceivedDefaultVariableValue(userId, featureKey, variableKey))
@@ -627,6 +624,13 @@ open class OptimizelyClient: NSObject {
627624
attributes: attributes ?? OptimizelyAttributes())
628625
if let featureEnabled = decision?.variation?.featureEnabled {
629626
enabled = featureEnabled
627+
if featureEnabled {
628+
logger.i(.featureEnabledForUser(featureKey, userId))
629+
} else {
630+
logger.i(.featureNotEnabledForUser(featureKey, userId))
631+
}
632+
} else {
633+
logger.i(.userReceivedAllDefaultVariableValues(userId, featureKey))
630634
}
631635

632636
for (_, v) in featureFlag.variablesMap {

Sources/Protocols/OPTBucketer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -27,7 +27,7 @@ protocol OPTBucketer {
2727

2828
/**
2929
Bucket a bucketingId into an experiment.
30-
- Parameter experiment: The experiment in which to bucket the bucketingId.
30+
- Parameter experiment: The experiment or rollout rule in which to bucket the bucketingId.
3131
- Parameter bucketingId: The ID to bucket. This must be a non-null, non-empty string.
3232
- Returns: The variation the bucketingId was bucketed into.
3333
*/

0 commit comments

Comments
 (0)