Skip to content

Commit

Permalink
feat!: allow custom seed when using targetingKey override for fractio…
Browse files Browse the repository at this point in the history
…nal op (#1266)

<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

## This PR
<!-- add the description of the PR here -->

- no longer injects flagKey as seed for fractional op when user has
provided custom targeting
- updates schema to allow `cat` and other operations so that custom
targeting can be properly seeded

### Related Issues
<!-- add here the GitHub issue that this PR resolves if applicable -->

#1264

### Notes
<!-- any additional notes for this PR -->

### Follow-up Tasks
<!-- anything that is related to this PR but not done here should be
noted under this section -->
<!-- if there is a need for a new issue, please link it here -->

### How to test
<!-- if applicable, add testing instructions under this section -->

```bash
# unit tests
make test

# gherkin tests
./bin/flagd start -f file:test-harness/flags/testing-flags.json -f file:test-harness/flags/custom-ops.json -f file:test-harness/flags/evaluator-refs.json -f file:test-harness/flags/zero-flags.json
make flagd-integration-test
```

---------

Signed-off-by: Cole Bailey <[email protected]>
Signed-off-by: Cole Bailey <[email protected]>
Signed-off-by: Kavindu Dodanduwa <[email protected]>
Co-authored-by: Michael Beemer <[email protected]>
Co-authored-by: Kavindu Dodanduwa <[email protected]>
  • Loading branch information
3 people authored Apr 9, 2024
1 parent 0075932 commit f62bc72
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ jobs:
-f file:${{ github.workspace }}/test-harness/flags/testing-flags.json \
-f file:${{ github.workspace }}/test-harness/flags/custom-ops.json \
-f file:${{ github.workspace }}/test-harness/flags/evaluator-refs.json \
-f file:${{ github.workspace }}/test-harness/flags/zero-flags.json &
-f file:${{ github.workspace }}/test-harness/flags/zero-flags.json \
-f file:${{ github.workspace }}/test-harness/flags/edge-case-flags.json &
- name: Run evaluation test suite
run: go clean -testcache && go test -cover ./test/integration
2 changes: 1 addition & 1 deletion core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
connectrpc.com/otelconnect v0.7.0
github.com/diegoholiveira/jsonlogic/v3 v3.4.0
github.com/fsnotify/fsnotify v1.7.0
github.com/open-feature/flagd-schemas v0.2.9-0.20240215170351-8c72c14eebff
github.com/open-feature/flagd-schemas v0.2.9-0.20240408192555-ea4f119d2bd7
github.com/open-feature/open-feature-operator/apis v0.2.39
github.com/prometheus/client_golang v1.19.0
github.com/robfig/cron v1.2.0
Expand Down
6 changes: 4 additions & 2 deletions core/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ github.com/cncf/xds/go v0.0.0-20230105202645-06c439db220b/go.mod h1:eXthEFrGJvWH
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/diegoholiveira/jsonlogic/v3 v3.4.0 h1:TN++nRmEMA5UHzKl8MJ1kbF5SSzWtKHE0PZ6ITbJeH4=
github.com/diegoholiveira/jsonlogic/v3 v3.4.0/go.mod h1:9oE8z9G+0OMxOoLHF3fhek3KuqD5CBqM0B6XFL08MSg=
github.com/emicklei/go-restful/v3 v3.12.0 h1:y2DdzBAURM29NFF94q6RaY4vjIH1rtwDapwQtU84iWk=
Expand Down Expand Up @@ -626,8 +627,8 @@ github.com/onsi/ginkgo/v2 v2.14.0 h1:vSmGj2Z5YPb9JwCWT6z6ihcUvDhuXLc3sJiqd3jMKAY
github.com/onsi/ginkgo/v2 v2.14.0/go.mod h1:JkUdW7JkN0V6rFvsHcJ478egV3XH9NxpD27Hal/PhZw=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/open-feature/flagd-schemas v0.2.9-0.20240215170351-8c72c14eebff h1:ZJwqlDjz+vfMzs+pYXqThCg3YV8wXxON8YztweiuaQ0=
github.com/open-feature/flagd-schemas v0.2.9-0.20240215170351-8c72c14eebff/go.mod h1:WKtwo1eW9/K6D+4HfgTXWBqCDzpvMhDa5eRxW7R5B2U=
github.com/open-feature/flagd-schemas v0.2.9-0.20240408192555-ea4f119d2bd7 h1:oP+BH8RiNEmSWTffKEXz2ciwen7wbvyX0fESx0aoJ80=
github.com/open-feature/flagd-schemas v0.2.9-0.20240408192555-ea4f119d2bd7/go.mod h1:WKtwo1eW9/K6D+4HfgTXWBqCDzpvMhDa5eRxW7R5B2U=
github.com/open-feature/open-feature-operator/apis v0.2.39 h1:uzZplxAes8/KQWVpC3AE0ME2CNNks2gaaufSZUBkVnM=
github.com/open-feature/open-feature-operator/apis v0.2.39/go.mod h1:I/4tLd5D4JpWpaFZxe2o8R2S1isWGNwHDSC/H5h7o3A=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -637,6 +638,7 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
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
Loading

0 comments on commit f62bc72

Please sign in to comment.