Targeting Rules and Filters
This product is not supported for your selected
Datadog site. (
).
Overview
Targeting rules define which variant to serve to which subjects. Each rule can include a filter, one or more variants, and an optional percentage rollout. Rules are evaluated in order until a match is found.
Targeting rule types
Datadog supports different targeting rule types depending on your rollout strategy:
| Type | Description |
|---|
| Feature gate | Roll out immediately to a percentage of subjects matching your filter (randomized or not) |
| Progressive rollout | Randomized rollout over a schedule with multiple steps |
| Experiment | Randomized allocation associated with an experiment |
To configure targeting rules for a flag:
- Navigate to Feature Flags and select your flag.
- Select the environment whose rules you want to modify.
- Click Add Targeting Rule (or click the targeting rule you want to modify).
For each targeting rule, configure the following:
- Name your targeting rule: Give your targeting rule a name to describe the group it targets.
- Define a filter (optional): If you do not define a filter, the rule matches all subjects in that environment.
- Select variants: Choose which variants to serve to matching subjects. Click Split Traffic to randomize across multiple variants (see Traffic Splitting and Randomization).
- Set the traffic exposure (optional): Serve the variant to a percentage of matching subjects (see Traffic Splitting and Randomization).
After configuring your targeting rules, click Save Changes, then enable the flag in the environment so SDKs can evaluate targeting rules.
SDKs do not evaluate targeting rules when the flag is disabled or overridden in an environment. If the flag is overridden with a fixed variant, the SDK returns that variant instead. If the flag is disabled, the SDK returns the coded default variant.
Filters and evaluation context
Filters use attributes from your SDK’s evaluation context. Define attributes when you set the evaluation context before evaluating flags. Attributes must be flat primitive values (strings, numbers, Booleans). Nested objects and arrays are not supported.
Example evaluation contexts and filters
With the following evaluation context, you can build filters with different operators, such as equality, is one of, is not, or numeric comparisons:
Evaluation context:
await OpenFeature.setContext({
targetingKey: 'user-123',
user_id: 'user-123',
user_role: 'admin',
email: 'user@example.com',
country: 'US',
tier: 'premium',
account_age_days: 120,
});
from openfeature.evaluation_context import EvaluationContext
eval_ctx = EvaluationContext(
targeting_key="user-123",
attributes={
"user_id": "user-123",
"user_role": "admin",
"email": "user@example.com",
"country": "US",
"tier": "premium",
"account_age_days": 120,
},
)
evalCtx := openfeature.NewEvaluationContext(
"user-123",
map[string]interface{}{
"user_id": "user-123",
"user_role": "admin",
"email": "user@example.com",
"country": "US",
"tier": "premium",
"account_age_days": 120,
},
)
Example filters
country is one of US, CAtier equals premiumuser_role is not guestaccount_age_days greater than 90
Note: Other SDKs follow the same pattern. See your platform’s client or server SDK documentation for evaluation context setup.
Rule hierarchy
Targeting rules are evaluated in order from top to bottom:
- The SDK evaluates the first rule. If the subject matches the filter (or no filter is defined), the rule may serve a variant.
- If the subject does not match, evaluation passes through to the next rule.
- If no rule matches, the SDK serves the default variant for that environment.
Further reading
Additional helpful documentation, links, and articles: