Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: allow custom seed when using targetingKey override for fractional op #1266

Merged
merged 7 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/pkg/evaluator/fractional.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@ func parseFractionalEvaluationData(values, data any) (string, []fractionalEvalua
if ok {
valuesArray = valuesArray[1:]
} else {
bucketBy, ok = dataMap[targetingKeyKey].(string)
targetingKey, ok := dataMap[targetingKeyKey].(string)
if !ok {
return "", nil, errors.New("bucketing value not supplied and no targetingKey in context")
}

bucketBy = fmt.Sprintf("%s%s", properties.FlagKey, targetingKey)
}

feDistributions, err := parseFractionalEvaluationDistributions(valuesArray)
if err != nil {
return "", nil, err
}

return fmt.Sprintf("%s%s", properties.FlagKey, bucketBy), feDistributions, nil
return bucketBy, feDistributions, nil
}

func parseFractionalEvaluationDistributions(values []any) ([]fractionalEvaluationDistribution, error) {
Expand Down
70 changes: 69 additions & 1 deletion core/pkg/evaluator/fractional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestFractionalEvaluation(t *testing.T) {
},
{
"fractional": [
{"var": "email"},
{"cat": [{"var": "$flagd.flagKey"}, {"var": "email"}]},
[
"red",
25
Expand All @@ -54,6 +54,34 @@ func TestFractionalEvaluation(t *testing.T) {
]
}`),
},
"customSeededHeaderColor": {
State: "ENABLED",
DefaultVariant: "red",
Variants: map[string]any{
"red": "#FF0000",
"blue": "#0000FF",
"green": "#00FF00",
"yellow": "#FFFF00",
},
Targeting: []byte(`{
"if": [
{
"in": ["@faas.com", {
"var": ["email"]
}]
},
{
"fractional": [
{"cat": ["my-seed", {"var": "email"}]},
["red",25],
["blue",25],
["green",25],
["yellow",25]
]
}, null
]
}`),
},
},
}

Expand Down Expand Up @@ -106,6 +134,46 @@ func TestFractionalEvaluation(t *testing.T) {
expectedValue: "#00FF00",
expectedReason: model.TargetingMatchReason,
},
"[email protected] with custom seed": {
flags: flags,
flagKey: "customSeededHeaderColor",
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "green",
expectedValue: "#00FF00",
expectedReason: model.TargetingMatchReason,
},
"[email protected] with custom seed": {
flags: flags,
flagKey: "customSeededHeaderColor",
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"[email protected] with custom seed": {
flags: flags,
flagKey: "customSeededHeaderColor",
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "green",
expectedValue: "#00FF00",
expectedReason: model.TargetingMatchReason,
},
"[email protected] with custom seed": {
flags: flags,
flagKey: "customSeededHeaderColor",
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "green",
expectedValue: "#00FF00",
expectedReason: model.TargetingMatchReason,
},
"[email protected] with different flag key": {
flags: Flags{
Flags: map[string]model.Flag{
Expand Down
21 changes: 17 additions & 4 deletions docs/reference/custom-operations/fractional-operation.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ OpenFeature allows clients to pass contextual information which can then be used
// Factional evaluation property name used in a targeting rule
"fractional": [
// Evaluation context property used to determine the split
{ "var": "email" },
// Note using `cat` and `$flagd.flagKey` is the suggested default to seed your hash value and prevent bucketing collisions
{
"cat": [
{ "var": "$flagd.flagKey" },
{ "var": "email" }
]
},
// Split definitions contain an array with a variant and percentage
// Percentages must add up to 100
[
Expand All @@ -33,7 +39,7 @@ The `defaultVariant` is `red`, but it contains a [targeting rule](../flag-defini

In this case, `25%` of the evaluations will receive `red`, `25%` will receive `blue`, and so on.

Assignment is deterministic (sticky) based on the expression supplied as the first parameter (`{ "var": "email" }`, in this case).
Assignment is deterministic (sticky) based on the expression supplied as the first parameter (`{ "cat": [{ "var": "$flagd.flagKey" }, { "var": "email" }]}`, in this case).
The value retrieved by this expression is referred to as the "bucketing value".
The bucketing value expression can be omitted, in which case a concatenation of the `targetingKey` and the `flagKey` will be used.

Expand All @@ -46,8 +52,10 @@ is selected.
As hashing is deterministic we can be sure to get the same result every time for the same data point.

The `fractional` operation can be added as part of a targeting definition.
The value is an array and the first element is the name of the property to use from the evaluation context.
The value is an array and the first element is a nested JsonLogic rule which resolves to the hash key.
This rule should typically consist of a seed concatenated with a session variable to use from the evaluation context.
This value should typically be something that remains consistent for the duration of a users session (e.g. email or session ID).
The seed is typically the flagKey so that experiments running across different flags are statistically independent, however, you can also specify another seed to either align or further decouple your allocations across different feature flags or use-cases.
The other elements in the array are nested arrays with the first element representing a variant and the second being the percentage that this option is selected.
There is no limit to the number of elements but the configured percentages must add up to 100.

Expand All @@ -69,7 +77,12 @@ Flags defined as such:
"state": "ENABLED",
"targeting": {
"fractional": [
{ "var": "email" },
{
"cat": [
{ "var": "$flagd.flagKey" },
{ "var": "email" }
]
},
[
"red",
50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ JSON object. Below is an example of a targeting rule containing a `fractional`:
"state": "ENABLED",
"targeting": {
"fractional": [
{"var":"email"},
{
"cat": [
{ "var": "$flagd.flagKey" },
{ "var": "email" }
]
},
[
"red",
50
Expand Down
7 changes: 6 additions & 1 deletion samples/example_flags.flagd.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@
},
{
"fractional": [
{ "var": "email" },
{
"cat": [
{ "var": "$flagd.flagKey" },
{ "var": "email" }
]
},
["red", 25],
["blue", 25],
["green", 25],
Expand Down
5 changes: 3 additions & 2 deletions samples/example_flags.flagd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ flags:
if:
- "$ref": emailWithFaas
- fractional:
- var:
- email
- cat:
- var: $flagd.flagKey
- var: email
- - red
- 25
- - blue
Expand Down
7 changes: 6 additions & 1 deletion samples/example_flags.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@
},
{
"fractional": [
{ "var": "email" },
{
"cat": [
{ "var": "$flagd.flagKey" },
{ "var": "email" }
]
},
["red", 25],
["blue", 25],
["green", 25],
Expand Down
5 changes: 3 additions & 2 deletions samples/example_flags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ flags:
if:
- "$ref": emailWithFaas
- fractional:
- var:
- email
- cat:
- var: $flagd.flagKey
- var: email
- - red
- 25
- - blue
Expand Down
2 changes: 1 addition & 1 deletion schemas
2 changes: 1 addition & 1 deletion test-harness
Loading