Flag evaluation and targeting
Flag evaluation turns a flag configuration and an evaluation context into one typed variation for a request. The evaluation path supports ordered targeting, stable percentage assignment, time-based rollouts, safe SDK defaults, and a bulk snapshot for clients. See the feature flag SDK for the wider SDK model, SDK configuration and retrieval for flag loading, and evaluation contexts for the input data.
Evaluation model
The evaluation model separates the flag value from the reason for the choice. The SDK reads a flag from its current configuration, applies the request context, and returns both the selected value and resolution details. This keeps normal variation calls simple while allowing callers and providers to inspect errors, rule information, cache safety, and metadata.
The main evaluation path is:
flowchart TD
A[Flag key, SDK default, evaluation context] --> B[Load flag configuration]
B --> C[Apply active scheduled changes]
C --> D[Add common context attributes]
D --> E[Resolve targeting or bucketing key]
E --> F{Flag active and key valid?}
F -- No --> G[Return SDK default with reason]
F -- Yes --> H[Check targeting rules in order]
H --> I{Rule matches?}
I -- Yes --> J[Resolve fixed or split variation]
I -- No --> K[Evaluate default rule]
K --> J
J --> L[Return typed value and details]
The public typed APIs use the SDK default when a flag is missing, disabled, outside its experiment window, or cannot be evaluated. A value from the flag must also match the requested type. A type mismatch is reported as an evaluation failure rather than silently changing the application contract.
References: modules/…/internal_flag.go• L63-129 modules/…/evaluation.go• L16-66 variation.go• L347-403
Variation resolution
InternalFlag is the core model for one evaluable flag. It groups the available named variations with ordered targeting rules, a default rule, an optional bucketing key, and time-based rollout state. The evaluator selects a variation name first, then reads the matching value from the variation map.
Each rule can produce a result in one of three ways:
- A fixed variation applies when the rule query matches.
- A percentage split assigns a stable share of users to named variations.
- A progressive rollout moves users from an initial variation or percentage to an end variation or percentage over time.
The rule result takes precedence over the default rule only when the rule applies. The evaluator checks targeting rules in their declared order and stops at the first applicable rule. It then evaluates the default rule when no targeting rule applies.
References: modules/…/internal_flag.go• L19-40 modules/…/internal_flag.go• L160-202 modules/…/rule.go• L26-50 modules/…/rule.go• L68-103
Targeting rules
Targeting rules decide whether a context belongs to an audience before they choose a variation. A rule query can use the attributes present in the evaluation context. The evaluator supports the repository's Nikunjy query format and JSONLogic when the query is expressed as a JSON object.
The rule list has these important behaviors:
- A non-default rule needs a query. An empty query matches, but a missing query is invalid for a targeting rule.
- A disabled targeting rule does not apply.
- A fixed rule returns its named variation.
- A split rule needs a bucketing key and distributes contexts by a stable hash.
- A default rule ignores its query and acts as the final choice after targeting rules.
The configuration checks that referenced variations exist and that a flag has a valid default rule. It also checks the shape of percentage and progressive rollout data before evaluation can use it.
References: modules/…/rule.go• L68-104 modules/…/rule.go• L106-165 modules/…/rule_validation.go• L11-38 modules/…/rule_validation.go• L124-158
Bucketing and rollout assignment
Bucketing provides repeatable assignment for percentage-based decisions. The evaluator hashes the flag name together with the bucketing key, maps the result into the configured percentage range, and uses the same result for later evaluations with the same inputs.
The normal bucketing key is the context targeting key. A flag can name a custom key that is read from a context attribute, including a nested attribute. A key is required only when the active rules need bucketing. This allows a fixed flag to evaluate without a key while still rejecting an incomplete context for a split or progressive rollout.
Progressive rollout uses the evaluation date to set the current share of the end state. The date comes from the protected context time when present. Otherwise, the evaluator uses the current time. Scheduled changes are applied before rule selection, and an experiment outside its start or end window serves the SDK default.
References: modules/…/internal_flag.go• L404-489 modules/…/rule.go• L168-209 modules/…/rule.go• L211-259 modules/…/hash.go• L12-19
Default behavior
The default rule is the flag's final configured behavior. It can return a fixed variation, a percentage split, or a progressive rollout. The system requires this rule because a context that matches no targeting rule still needs a defined result.
The SDK default is different from the flag's default rule. The SDK default is supplied by the application and protects the application when the provider is not ready, the flag is absent, the flag is disabled, the evaluation context lacks a required key, the configuration is invalid, or the selected value has the wrong type. The result marks these cases with a failure state and an error code when the SDK contract requires it.
Disabled flags and experiments outside their active window use the SDK default with a disabled reason. Offline evaluation also uses the SDK default, but it reports an offline reason rather than an evaluation failure.
References: modules/…/internal_flag.go• L101-129 modules/…/internal_flag.go• L286-340 variation.go• L347-403 modules/…/resolution_reason.go• L21-43 modules/…/error_code.go• L3-19
Bulk evaluation
Bulk evaluation creates one AllFlags snapshot for a context. It evaluates either the full current flag set or a requested subset, then stores each flag's value and resolution state under its flag key. This model supports client providers that need one context-specific set of values instead of one remote call per flag.
The bulk path follows this selection rule:
| Request input | Evaluated flags |
|---|---|
| Empty or missing list | All flags in the current cache |
| Non-empty flag list | Only the named flags that can be retrieved |
The gofeatureflag.flagList protected context field carries the subset for integrations that use the context to request bulk evaluation. An empty list means all flags. The resulting snapshot has a valid state. A failed flag evaluation makes the snapshot invalid, while an unavailable cache can prevent a usable snapshot from being returned.
The relay bulk endpoint uses the same distinction. It selects the list from the protected context when present and otherwise calls the full snapshot path. It returns each flag's value, reason, variant, metadata, and any error details.
References: variation_all_flags.go• L31-91 internal/…/all_flags.go• L7-55 modules/…/goff_context_specifics.go• L5-12 cmd/…/evaluate.go• L155-223
Evaluation result model
VariationResult is the typed result returned by a detailed evaluation. Its Value is the application value. Its resolution fields explain how that value was chosen and whether the caller should treat the evaluation as failed.
| Field | Meaning |
|---|---|
Value | The selected flag value or the SDK default. |
VariationType | The selected variation name or SdkDefault. |
Reason | The high-level cause of the result. |
Failed | Whether the evaluation produced an error condition. |
ErrorCode, ErrorDetails | The failure category and supporting message. |
Cacheable | Whether a provider can safely reuse the result. Dynamic schedules, experiments, and progressive rules can make a result non-cacheable. |
Version, Metadata | Flag information carried with the result. |
TrackEvents | Whether the result is eligible for feature event tracking. |
The main reasons distinguish a direct targeting match, a targeting match with a split, a general split, a static or default result, a disabled flag, offline mode, and an error. These details let providers preserve evaluation meaning when they return results through another SDK or protocol.
References: modules/…/variation_result.go• L9-46 modules/…/resolution_detail.go• L3-29 modules/…/resolution_reason.go• L3-43 modules/…/internal_flag.go• L132-158
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the variation result fields aligned with the relay and OpenFeature response models. The result is a cross-boundary contract.
- Keep the names and meanings of resolution reasons and error codes aligned with the provider protocols that expose them.
- When changing bucketing inputs or bucket order, consider stable assignment. Existing users can move between percentage variations.
- When changing protected context fields, update both context parsing and bulk evaluation selection.
- When changing scheduled or progressive rollout time rules, check the explicit evaluation date path as well as the live clock path.
- When changing default handling, preserve the distinction between the flag's default rule and the application's SDK default.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A split or progressive rule has no usable targeting or custom bucketing key | Evaluation returns the SDK default with a targeting-key error | Supply a stable key for any flag that requires bucketing. |
| A targeting rule names a variation that is not defined | The flag configuration is rejected or evaluation fails | Validate every rule result against the flag's variation map. |
| A flag has no default rule | No safe final variation exists | Keep one valid default rule on every flag. |
| The selected flag value does not match the typed API | The SDK default is returned with a type-mismatch failure | Keep all variations for a flag at one compatible type. |
| A query cannot be parsed or evaluated | The rule does not apply, so evaluation continues to the next rule or default rule | Use a valid query and provide every attribute used by that query. |
| A scheduled or progressive rule is evaluated with an unexpected date | The rollout can serve the wrong stage | Set the protected evaluation time when replaying or testing time-based behavior. |
Key files
| File | Why to read it |
|---|---|
| variation.go | Shows the public typed evaluation contract and the difference between an application SDK default and a configured flag value. |
| modules/…/evaluation.go | Shows how a raw flag result becomes a typed variation result. |
| modules/…/internal_flag.go | Shows the end-to-end decision model for one flag, including rule order, default behavior, bucketing keys, and time gates. |
| modules/…/rule.go | Shows the supported targeting result types and the stable split and progressive rollout concepts. |
| variation_all_flags.go | Shows how one context produces a full or selected bulk snapshot. |
| internal/…/all_flags.go | Defines the bulk snapshot and its validity contract. |
| modules/…/context.go | Defines the targeting key and custom attributes that drive queries. |
| modules/…/goff_context_specifics.go | Defines protected context inputs for bulk flag lists and evaluation time. |
| modules/…/variation_result.go | Defines the detailed result model passed across SDK and provider boundaries. |
| cmd/…/evaluate.go | Shows how bulk evaluation is exposed through the relay endpoint. |