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: default variant #3168

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions config/migrations/cockroachdb/10_variants_default.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE variants ADD COLUMN default BOOLEAN DEFAULT FALSE NOT NULL;
CREATE UNIQUE INDEX variants_default_unique_index ON variants (namespace_key, flag_key) WHERE default = TRUE;
2 changes: 2 additions & 0 deletions config/migrations/mysql/12_variants_default.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE variants ADD COLUMN default BOOLEAN DEFAULT FALSE NOT NULL;
CREATE UNIQUE INDEX variants_default_unique_index ON variants (namespace_key, flag_key) WHERE default = TRUE;
2 changes: 2 additions & 0 deletions config/migrations/postgres/13_variants_default.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE variants ADD COLUMN default BOOLEAN DEFAULT FALSE NOT NULL;
CREATE UNIQUE INDEX variants_default_unique_index ON variants (namespace_key, flag_key) WHERE default = TRUE;
2 changes: 2 additions & 0 deletions config/migrations/sqlite3/12_variants_default.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE variants ADD COLUMN `default` BOOLEAN DEFAULT FALSE NOT NULL;
CREATE UNIQUE INDEX variants_default_unique_index ON variants (namespace_key, flag_key) WHERE `default` = TRUE;
6 changes: 5 additions & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down Expand Up @@ -1052,6 +1053,7 @@ golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
Expand Down Expand Up @@ -1115,6 +1117,7 @@ golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Expand All @@ -1124,6 +1127,7 @@ golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -1152,7 +1156,7 @@ golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.10.0/go.mod h1:UJwyiVBsOA2uwvK/e5OY3GTpDUJriEd+/YlqAwLPmyM=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
google.golang.org/api v0.0.0-20160322025152-9bf6e6e569ff/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.35.0/go.mod h1:/XrVsuzM0rZmrsbjJutiuftIzeuTQcEeaYcSk/mQ1dg=
google.golang.org/api v0.36.0/go.mod h1:+z5ficQTmoYpPn8LCUNVpK5I7hwkpjbcgqA7I34qYtE=
Expand Down
10 changes: 10 additions & 0 deletions internal/server/evaluation/legacy_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (e *Evaluator) Evaluate(ctx context.Context, flag *flipt.Flag, r *evaluatio
flagAttr,
metrics.AttributeMatch.Bool(resp.Match),
metrics.AttributeSegment.String(resp.SegmentKey),
metrics.AttributeSegments.String(strings.Join(resp.SegmentKeys, ",")),
metrics.AttributeReason.String(resp.Reason.String()),
metrics.AttributeValue.String(resp.Value),
metrics.AttributeType.String("variant"),
Expand Down Expand Up @@ -186,6 +187,15 @@ func (e *Evaluator) Evaluate(ctx context.Context, flag *flipt.Flag, r *evaluatio

// no distributions for rule
if len(validDistributions) == 0 {
// find default variant
// TODO: we may want to try do this elsewhere to avoid this loop
for _, v := range flag.Variants {
if v.Default {
resp.Value = v.Key
resp.Attachment = v.Attachment
break
}
}
e.logger.Info("no distributions for rule")
resp.Match = true
resp.Reason = flipt.EvaluationReason_MATCH_EVALUATION_REASON
Expand Down
191 changes: 190 additions & 1 deletion internal/server/evaluation/legacy_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,108 @@ func TestEvaluator_MatchAll_NoVariants_NoDistributions(t *testing.T) {
}
}

var flagWithDefaultVariant = &flipt.Flag{
Key: "foo",
Enabled: true,
Variants: []*flipt.Variant{
{
Key: "default",
Default: true,
},
{
Key: "variant",
},
},
}

func TestEvaluator_MatchAll_DefaultVariant_NoDistributions(t *testing.T) {
var (
store = &evaluationStoreMock{}
logger = zaptest.NewLogger(t)
s = NewEvaluator(logger, store)
)

store.On("GetEvaluationRules", mock.Anything, storage.NewResource("", "foo")).Return(
[]*storage.EvaluationRule{
{
ID: "1",
FlagKey: "foo",
Rank: 0,
Segments: map[string]*storage.EvaluationSegment{
"bar": {
SegmentKey: "bar",
MatchType: flipt.MatchType_ALL_MATCH_TYPE,
Constraints: []storage.EvaluationConstraint{
{
ID: "2",
Type: flipt.ComparisonType_STRING_COMPARISON_TYPE,
Property: "bar",
Operator: flipt.OpEQ,
Value: "baz",
},
},
},
},
},
}, nil)

store.On("GetEvaluationDistributions", mock.Anything, storage.NewID("1")).Return([]*storage.EvaluationDistribution{}, nil)

tests := []struct {
name string
req *evaluation.EvaluationRequest
wantMatch bool
}{
{
name: "match string value",
req: &evaluation.EvaluationRequest{
FlagKey: "foo",
EntityId: "1",
Context: map[string]string{
"bar": "baz",
},
},
wantMatch: true,
},
{
name: "no match string value",
req: &evaluation.EvaluationRequest{
FlagKey: "foo",
EntityId: "1",
Context: map[string]string{
"bar": "boz",
},
},
},
}

for _, tt := range tests {
var (
req = tt.req
wantMatch = tt.wantMatch
)

t.Run(tt.name, func(t *testing.T) {
resp, err := s.Evaluate(context.TODO(), flagWithDefaultVariant, req)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "foo", resp.FlagKey)
assert.Equal(t, req.Context, resp.RequestContext)

if !wantMatch {
assert.False(t, resp.Match)
assert.Empty(t, resp.SegmentKey)
return
}

assert.True(t, resp.Match)
assert.Equal(t, "bar", resp.SegmentKey)
assert.Equal(t, "default", resp.Value)
assert.Equal(t, flipt.EvaluationReason_MATCH_EVALUATION_REASON, resp.Reason)
})
}
}

func TestEvaluator_MatchAll_MultipleSegments(t *testing.T) {
var (
store = &evaluationStoreMock{}
Expand Down Expand Up @@ -1411,7 +1513,6 @@ func TestEvaluator_DistributionNotMatched(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "foo", resp.FlagKey)

assert.False(t, resp.Match, "distribution not matched")
}

Expand Down Expand Up @@ -1940,6 +2041,94 @@ func TestEvaluator_MatchAny_NoVariants_NoDistributions(t *testing.T) {
}
}

func TestEvaluator_MatchAny_DefaultVariant_NoDistributions(t *testing.T) {
var (
store = &evaluationStoreMock{}
logger = zaptest.NewLogger(t)
s = NewEvaluator(logger, store)
)

store.On("GetEvaluationRules", mock.Anything, storage.NewResource("", "foo")).Return(
[]*storage.EvaluationRule{
{
ID: "1",
FlagKey: "foo",
Rank: 0,
Segments: map[string]*storage.EvaluationSegment{
"bar": {
SegmentKey: "bar",
MatchType: flipt.MatchType_ANY_MATCH_TYPE,
Constraints: []storage.EvaluationConstraint{
{
ID: "2",
Type: flipt.ComparisonType_STRING_COMPARISON_TYPE,
Property: "bar",
Operator: flipt.OpEQ,
Value: "baz",
},
},
},
},
},
}, nil)

store.On("GetEvaluationDistributions", mock.Anything, storage.NewID("1")).Return([]*storage.EvaluationDistribution{}, nil)

tests := []struct {
name string
req *evaluation.EvaluationRequest
wantMatch bool
}{
{
name: "match string value",
req: &evaluation.EvaluationRequest{
FlagKey: "foo",
EntityId: "1",
Context: map[string]string{
"bar": "baz",
},
},
wantMatch: true,
},
{
name: "no match string value",
req: &evaluation.EvaluationRequest{
FlagKey: "foo",
EntityId: "1",
Context: map[string]string{
"bar": "boz",
},
},
},
}

for _, tt := range tests {
var (
req = tt.req
wantMatch = tt.wantMatch
)

t.Run(tt.name, func(t *testing.T) {
resp, err := s.Evaluate(context.TODO(), flagWithDefaultVariant, req)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, "foo", resp.FlagKey)
assert.Equal(t, req.Context, resp.RequestContext)

if !wantMatch {
assert.False(t, resp.Match)
assert.Empty(t, resp.SegmentKey)
return
}

assert.True(t, resp.Match)
assert.Equal(t, "bar", resp.SegmentKey)
assert.Equal(t, "default", resp.Value)
assert.Equal(t, flipt.EvaluationReason_MATCH_EVALUATION_REASON, resp.Reason)
})
}
}

func TestEvaluator_MatchAny_SingleVariantDistribution(t *testing.T) {
var (
store = &evaluationStoreMock{}
Expand Down
6 changes: 4 additions & 2 deletions internal/server/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ var (

// Attributes used in evaluation metrics
//nolint
AttributeMatch = attribute.Key("match")
AttributeFlag = attribute.Key("flag")
AttributeMatch = attribute.Key("match")
AttributeFlag = attribute.Key("flag")
// deprecated
AttributeSegment = attribute.Key("segment")
AttributeSegments = attribute.Key("segments")
AttributeReason = attribute.Key("reason")
AttributeValue = attribute.Key("value")
AttributeNamespace = attribute.Key("namespace")
Expand Down
Loading
Loading