|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2025, Optimizely, Inc. and contributors * |
| 3 | + * * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); * |
| 5 | + * you may not use this file except in compliance with the License. * |
| 6 | + * You may obtain a copy of the License at * |
| 7 | + * * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 9 | + * * |
| 10 | + * Unless required by applicable law or agreed to in writing, software * |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 13 | + * See the License for the specific language governing permissions and * |
| 14 | + * limitations under the License. * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +// Package decision // |
| 18 | +package decision |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/optimizely/go-sdk/v2/pkg/config" |
| 24 | + "github.com/optimizely/go-sdk/v2/pkg/decide" |
| 25 | + "github.com/optimizely/go-sdk/v2/pkg/decision/bucketer" |
| 26 | + "github.com/optimizely/go-sdk/v2/pkg/decision/evaluator" |
| 27 | + "github.com/optimizely/go-sdk/v2/pkg/entities" |
| 28 | + "github.com/optimizely/go-sdk/v2/pkg/logging" |
| 29 | +) |
| 30 | + |
| 31 | +// HoldoutService evaluates holdout groups for feature flags |
| 32 | +type HoldoutService struct { |
| 33 | + audienceTreeEvaluator evaluator.TreeEvaluator |
| 34 | + bucketer bucketer.ExperimentBucketer |
| 35 | + logger logging.OptimizelyLogProducer |
| 36 | +} |
| 37 | + |
| 38 | +// NewHoldoutService returns a new instance of the HoldoutService |
| 39 | +func NewHoldoutService(sdkKey string) *HoldoutService { |
| 40 | + logger := logging.GetLogger(sdkKey, "HoldoutService") |
| 41 | + return &HoldoutService{ |
| 42 | + audienceTreeEvaluator: evaluator.NewMixedTreeEvaluator(logger), |
| 43 | + bucketer: *bucketer.NewMurmurhashExperimentBucketer(logger, bucketer.DefaultHashSeed), |
| 44 | + logger: logger, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// GetDecision returns a decision for holdouts associated with the feature |
| 49 | +func (h HoldoutService) GetDecision(decisionContext FeatureDecisionContext, userContext entities.UserContext, options *decide.Options) (FeatureDecision, decide.DecisionReasons, error) { |
| 50 | + feature := decisionContext.Feature |
| 51 | + reasons := decide.NewDecisionReasons(options) |
| 52 | + |
| 53 | + holdouts := decisionContext.ProjectConfig.GetHoldoutsForFlag(feature.Key) |
| 54 | + |
| 55 | + for _, holdout := range holdouts { |
| 56 | + h.logger.Debug(fmt.Sprintf("Evaluating holdout %s for feature %s", holdout.Key, feature.Key)) |
| 57 | + |
| 58 | + // Check if holdout is running |
| 59 | + if holdout.Status != entities.HoldoutStatusRunning { |
| 60 | + reason := reasons.AddInfo("Holdout %s is not running.", holdout.Key) |
| 61 | + h.logger.Info(reason) |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + // Check audience conditions |
| 66 | + inAudience := h.checkIfUserInHoldoutAudience(&holdout, userContext, decisionContext.ProjectConfig, options) |
| 67 | + reasons.Append(inAudience.reasons) |
| 68 | + |
| 69 | + if !inAudience.result { |
| 70 | + reason := reasons.AddInfo("User %s does not meet conditions for holdout %s.", userContext.ID, holdout.Key) |
| 71 | + h.logger.Info(reason) |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + reason := reasons.AddInfo("User %s meets conditions for holdout %s.", userContext.ID, holdout.Key) |
| 76 | + h.logger.Info(reason) |
| 77 | + |
| 78 | + // Get bucketing ID |
| 79 | + bucketingID, err := userContext.GetBucketingID() |
| 80 | + if err != nil { |
| 81 | + errorMessage := reasons.AddInfo("Error computing bucketing ID for holdout %q: %q", holdout.Key, err.Error()) |
| 82 | + h.logger.Debug(errorMessage) |
| 83 | + } |
| 84 | + |
| 85 | + if bucketingID != userContext.ID { |
| 86 | + h.logger.Debug(fmt.Sprintf("Using bucketing ID: %q for user %q", bucketingID, userContext.ID)) |
| 87 | + } |
| 88 | + |
| 89 | + // Convert holdout to experiment structure for bucketing |
| 90 | + experimentForBucketing := entities.Experiment{ |
| 91 | + ID: holdout.ID, |
| 92 | + Key: holdout.Key, |
| 93 | + Variations: holdout.Variations, |
| 94 | + TrafficAllocation: holdout.TrafficAllocation, |
| 95 | + AudienceIds: holdout.AudienceIds, |
| 96 | + AudienceConditions: holdout.AudienceConditions, |
| 97 | + AudienceConditionTree: holdout.AudienceConditionTree, |
| 98 | + } |
| 99 | + |
| 100 | + // Bucket user into holdout variation |
| 101 | + variation, _, _ := h.bucketer.Bucket(bucketingID, experimentForBucketing, entities.Group{}) |
| 102 | + |
| 103 | + if variation != nil { |
| 104 | + reason := reasons.AddInfo("User %s is in variation %s of holdout %s.", userContext.ID, variation.Key, holdout.Key) |
| 105 | + h.logger.Info(reason) |
| 106 | + |
| 107 | + featureDecision := FeatureDecision{ |
| 108 | + Experiment: experimentForBucketing, |
| 109 | + Variation: variation, |
| 110 | + Source: Holdout, |
| 111 | + } |
| 112 | + return featureDecision, reasons, nil |
| 113 | + } |
| 114 | + |
| 115 | + reason = reasons.AddInfo("User %s is in no holdout variation.", userContext.ID) |
| 116 | + h.logger.Info(reason) |
| 117 | + } |
| 118 | + |
| 119 | + return FeatureDecision{}, reasons, nil |
| 120 | +} |
| 121 | + |
| 122 | +// checkIfUserInHoldoutAudience evaluates if user meets holdout audience conditions |
| 123 | +func (h HoldoutService) checkIfUserInHoldoutAudience(holdout *entities.Holdout, userContext entities.UserContext, projectConfig config.ProjectConfig, options *decide.Options) decisionResult { |
| 124 | + decisionReasons := decide.NewDecisionReasons(options) |
| 125 | + |
| 126 | + if holdout == nil { |
| 127 | + logMessage := decisionReasons.AddInfo("Holdout is nil, defaulting to false") |
| 128 | + h.logger.Debug(logMessage) |
| 129 | + return decisionResult{result: false, reasons: decisionReasons} |
| 130 | + } |
| 131 | + |
| 132 | + if holdout.AudienceConditionTree != nil { |
| 133 | + condTreeParams := entities.NewTreeParameters(&userContext, projectConfig.GetAudienceMap()) |
| 134 | + h.logger.Debug(fmt.Sprintf("Evaluating audiences for holdout %q.", holdout.Key)) |
| 135 | + |
| 136 | + evalResult, _, audienceReasons := h.audienceTreeEvaluator.Evaluate(holdout.AudienceConditionTree, condTreeParams, options) |
| 137 | + decisionReasons.Append(audienceReasons) |
| 138 | + |
| 139 | + logMessage := decisionReasons.AddInfo("Audiences for holdout %s collectively evaluated to %v.", holdout.Key, evalResult) |
| 140 | + h.logger.Debug(logMessage) |
| 141 | + |
| 142 | + return decisionResult{result: evalResult, reasons: decisionReasons} |
| 143 | + } |
| 144 | + |
| 145 | + logMessage := decisionReasons.AddInfo("Audiences for holdout %s collectively evaluated to true.", holdout.Key) |
| 146 | + h.logger.Debug(logMessage) |
| 147 | + return decisionResult{result: true, reasons: decisionReasons} |
| 148 | +} |
| 149 | + |
| 150 | +// decisionResult is a helper struct to return both result and reasons |
| 151 | +type decisionResult struct { |
| 152 | + result bool |
| 153 | + reasons decide.DecisionReasons |
| 154 | +} |
0 commit comments