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: Implement dynamic targeting key #2218

Merged
merged 16 commits into from
Sep 13, 2024

Conversation

ricardobeat
Copy link
Contributor

@ricardobeat ricardobeat commented Aug 13, 2024

Description

Introduces the bucketingKey field in flag configuration. It allows the targeting key to be dynamically overwritten at evaluation time by another value coming from the context. See related issue for more info.

This is a non-breaking change.

Closes issue(s)

Resolve #2135

Checklist

  • I have tested this code
  • I have added unit test to cover this code
  • I have updated the documentation (README.md and /website/docs)
  • I have followed the contributing guide

Copy link

netlify bot commented Aug 13, 2024

Deploy Preview for go-feature-flag-doc-preview ready!

Name Link
🔨 Latest commit 50a990f
🔍 Latest deploy log https://app.netlify.com/sites/go-feature-flag-doc-preview/deploys/66e4477aa21f550008d79b37
😎 Deploy Preview https://deploy-preview-2218--go-feature-flag-doc-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@ricardobeat
Copy link
Contributor Author

Since rules don't have access to the parent flag, I had to pass the key around, there might be a better way to achieve this?

@ricardobeat ricardobeat changed the title Implement dynamic targeting key feat: Implement dynamic targeting key Aug 13, 2024
Copy link
Owner

@thomaspoignant thomaspoignant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ricardobeat, thanks a lot for creating this pull request! It helps me understand a bit better what we were discussing.

If I'm correct, you want the evaluation to fail and return to the default value provided in the evaluation context if the bucketingKey is not found.

This is an interesting idea and a simple way to avoid having to fall back on the targetingKey. However, I'm concerned about the new ErrorCode we're sending because it's not a known one from OpenFeature (see list here).

I've also reviewed the different documentation you shared in the Slack channel. It seems there's no consensus on whether to fall back to default behavior or fail.

Optimizely falls back to the targetingKey (see here).

If you don't pass a bucketing ID to the attributes parameter, users are bucketed by user IDs, which is the default method.

I haven't done a deep review of the code yet but it looks good, I am just not sure if falling is a good idea when the bucketingKey required by the flag is not set in the evaluation context 🤔.

@ricardobeat
Copy link
Contributor Author

ricardobeat commented Aug 15, 2024

Optimizely falls back to the targetingKey
not sure if falling is a good idea when the bucketingKey required by the flag is not set in the evaluation context

I couldn't find Optimizely's reasoning for this. Here is my attempt at illustrating the problem:

image

The user in org1 receives the on variant; let's imagine this is a new "Team updates" section in their app. The other user in the organization is in the off variant and will never see the feature.

This defeats the purpose of the feature test, and when we set up analytics for the A/B test, we are going to measure things like users in organization who interacted with feature X. Having users randomly assigned to different variants within the same organization means we will measure a mix of A and B, making the metric not useful, or worse, silently biased if this (missing key) happens only for a small % of users.

Personally I don't see a logical reason to have a fallback, and would expect it to behave exactly as if targetingKey is missing; but would be happy if this is at least configurable behaviour.

@thomaspoignant thomaspoignant marked this pull request as ready for review August 15, 2024 15:51
Copy link
Owner

@thomaspoignant thomaspoignant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, falling back to the default value is probably the best option here.
I've put some comments in the PR, especially about replacing the key in the context where I don't see the adding value.

If we want to move forward with this PR, I think we should address the documentation part because it will be necessary to explain well how it works.
If you are lost with the doc, I can always help you.

PS: For the CI falling on swagger, you can run make swagger in the root of the repo to update the swagger files.

@@ -14,6 +14,6 @@ func ContextToMap(ctx ffcontext.Context) map[string]interface{} {
userCopy[key] = value
}
userCopy["anonymous"] = ctx.IsAnonymous()
userCopy["key"] = ctx.GetKey()
userCopy["key"] = key
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the issue, I'd prefer not to do this.
key and targetingKey are special fields in the evaluation context and we should keep them like that.

For this feature, the important part is to be sure that we split the users based on another field it should not have an impact on the way to write rules.

This part will change how you write your rules and may lead to misleading configuration of your flags.

Let's say you have this flag

my-flag:
  variation:
     enabled: true
     disabled: false
  defaultRule:
     percentage:
       enabled: 10
       disabled: 90
  bucketingKey: teamId

and this evaluation context:

{
  "targetingKey": "f1f0d804-f23f-4c93-a935-6e04ce2cf696",
  "teamId": 567,
  "email": "[email protected]"
}

If you have to add a rule inside the flag based on the teamId you can go with

# ...
  targeting:
     - query: teamId eq "567"
       variation: enabled
# ...

I don't see the adding value to also put teamId value as key.

@ricardobeat
Copy link
Contributor Author

@thomaspoignant I've done a bit of refactoring according to the comments above:

  • renamed GetBucketingKey
  • removed the non-standard error
  • added ErrorDetails (note: OpenFeature refers to it as 'error message' but OFREP has ErrorDetails instead)

I also took a first pass at documentation. Let me know what you think.

Copy link
Owner

@thomaspoignant thomaspoignant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ricardobeat sorry I have been on vacation for some weeks this is why I was a bit quiet.

I think we are close to the solution with this PR, my only remarks is about the tests that are not testing what the change of key is affecting (in percentage-based evaluation).

As soon as we have a good test for that I think we will be ready to merge.

Comment on lines 1727 to 1809
{
name: "Should use custom bucketing key when set",
flag: flag.InternalFlag{
Variations: &map[string]*interface{}{
"variation_A": testconvert.Interface("value_A"),
"variation_B": testconvert.Interface("value_B"),
"variation_C": testconvert.Interface("value_C"),
},
BucketingKey: "teamId",
Rules: &[]flag.Rule{
{
Query: testconvert.String("key eq \"team-123\""),
VariationResult: testconvert.String("variation_B"),
},
{
Query: testconvert.String("key eq \"user-key\""),
VariationResult: testconvert.String("variation_C"),
},
},
DefaultRule: &flag.Rule{
VariationResult: testconvert.String("variation_A"),
},
Metadata: &map[string]interface{}{
"teamId": "team-123",
},
},
args: args{
flagName: "my-flag",
user: ffcontext.NewEvaluationContextBuilder("user-key").AddCustom("teamId", "team-123").Build(),
flagContext: flag.Context{
DefaultSdkValue: "value_default",
},
},
want: "value_B",
want1: flag.ResolutionDetails{
Variant: "variation_B",
Reason: flag.ReasonTargetingMatch,
RuleIndex: testconvert.Int(0),
Cacheable: true,
Metadata: map[string]interface{}{
"teamId": "team-123",
},
},
},
{
name: "Should not use user key when bucketing key is set",
flag: flag.InternalFlag{
Variations: &map[string]*interface{}{
"variation_A": testconvert.Interface("value_A"),
"variation_B": testconvert.Interface("value_B"),
},
BucketingKey: "region",
Rules: &[]flag.Rule{
{
Query: testconvert.String("key eq \"user-key\""),
VariationResult: testconvert.String("variation_B"),
},
},
DefaultRule: &flag.Rule{
VariationResult: testconvert.String("variation_A"),
},
Metadata: &map[string]interface{}{
"region": "nl-nh",
},
},
args: args{
flagName: "my-flag",
user: ffcontext.NewEvaluationContextBuilder("user-key").AddCustom("region", "nl-nh").Build(),
flagContext: flag.Context{
DefaultSdkValue: "value_default",
},
},
want: "value_A",
want1: flag.ResolutionDetails{
Variant: "variation_A",
Reason: flag.ReasonDefault,
Cacheable: true,
Metadata: map[string]interface{}{
"region": "nl-nh",
},
},
},
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure those tests are relevant because they do not testing the percentage split based on the bucketing key.

A good test here could be to have a percentage-based evaluation, set a targetingKey that affect to 1 variant and a teamId that affect to another variant.

The test should check that when the bucketingKey is set we are serving the good variation.

@thomaspoignant
Copy link
Owner

@ricardobeat do you need help finishing this PR?

@ricardobeat
Copy link
Contributor Author

@thomaspoignant sorry, I was also on holiday the week before and just got around to reviewing this. Need some help with figuring out the tests indeed, commented above 🙏

Copy link

codecov bot commented Sep 13, 2024

Codecov Report

Attention: Patch coverage is 92.85714% with 4 lines in your changes missing coverage. Please review.

Project coverage is 86.07%. Comparing base (ad53f58) to head (50a990f).

Files with missing lines Patch % Lines
internal/flag/rule.go 80.95% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2218      +/-   ##
==========================================
+ Coverage   86.02%   86.07%   +0.05%     
==========================================
  Files         102      102              
  Lines        3743     3771      +28     
==========================================
+ Hits         3220     3246      +26     
- Misses        399      400       +1     
- Partials      124      125       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Thomas Poignant <[email protected]>
Copy link

@kodiakhq kodiakhq bot merged commit 65a084c into thomaspoignant:main Sep 13, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(feature) Support for Custom Assignment/Targeting Keys
2 participants