-
Notifications
You must be signed in to change notification settings - Fork 246
feat: add single evaluation endpoint for OFREP #3267
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
Merged
kodiakhq
merged 5 commits into
flipt-io:main
from
thepabloaguilar:issue-2980-single-evaluation
Jul 29, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
772853e
feat: add single evaluation endpoint for OFREP
thepabloaguilar 7701aba
chore: return an empty metadata
thepabloaguilar 0ea1d59
fix: missing body declaration in proto config
thepabloaguilar da7e3a6
chore: support targetingKey
thepabloaguilar 5cc098a
Merge branch 'main' into issue-2980-single-evaluation
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package evaluation | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strconv" | ||
|
||
"github.com/google/uuid" | ||
|
||
"go.flipt.io/flipt/internal/server/ofrep" | ||
|
||
rpcevaluation "go.flipt.io/flipt/rpc/flipt/evaluation" | ||
|
||
fliptotel "go.flipt.io/flipt/internal/server/otel" | ||
"go.flipt.io/flipt/internal/storage" | ||
"go.flipt.io/flipt/rpc/flipt" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
const ofrepCtxTargetingKey = "targetingKey" | ||
|
||
func (s *Server) OFREPEvaluationBridge(ctx context.Context, input ofrep.EvaluationBridgeInput) (ofrep.EvaluationBridgeOutput, error) { | ||
flag, err := s.store.GetFlag(ctx, storage.NewResource(input.NamespaceKey, input.FlagKey)) | ||
if err != nil { | ||
return ofrep.EvaluationBridgeOutput{}, err | ||
} | ||
|
||
span := trace.SpanFromContext(ctx) | ||
span.SetAttributes( | ||
fliptotel.AttributeNamespace.String(input.NamespaceKey), | ||
fliptotel.AttributeFlag.String(input.FlagKey), | ||
fliptotel.AttributeProviderName, | ||
) | ||
|
||
req := &rpcevaluation.EvaluationRequest{ | ||
NamespaceKey: input.NamespaceKey, | ||
FlagKey: input.FlagKey, | ||
EntityId: uuid.NewString(), | ||
Context: input.Context, | ||
} | ||
|
||
// https://openfeature.dev/docs/reference/concepts/evaluation-context/#targeting-key | ||
if targetingKey, ok := input.Context[ofrepCtxTargetingKey]; ok { | ||
req.EntityId = targetingKey | ||
} | ||
|
||
switch flag.Type { | ||
case flipt.FlagType_VARIANT_FLAG_TYPE: | ||
resp, err := s.variant(ctx, flag, req) | ||
if err != nil { | ||
return ofrep.EvaluationBridgeOutput{}, err | ||
} | ||
|
||
span.SetAttributes( | ||
fliptotel.AttributeMatch.Bool(resp.Match), | ||
fliptotel.AttributeValue.String(resp.VariantKey), | ||
fliptotel.AttributeReason.String(resp.Reason.String()), | ||
fliptotel.AttributeSegments.StringSlice(resp.SegmentKeys), | ||
fliptotel.AttributeFlagKey(resp.FlagKey), | ||
fliptotel.AttributeFlagVariant(resp.VariantKey), | ||
) | ||
|
||
return ofrep.EvaluationBridgeOutput{ | ||
FlagKey: resp.FlagKey, | ||
Reason: resp.Reason, | ||
Variant: resp.VariantKey, | ||
Value: resp.VariantKey, | ||
}, nil | ||
case flipt.FlagType_BOOLEAN_FLAG_TYPE: | ||
resp, err := s.boolean(ctx, flag, req) | ||
if err != nil { | ||
return ofrep.EvaluationBridgeOutput{}, err | ||
} | ||
|
||
span.SetAttributes( | ||
fliptotel.AttributeValue.Bool(resp.Enabled), | ||
fliptotel.AttributeReason.String(resp.Reason.String()), | ||
fliptotel.AttributeFlagVariant(strconv.FormatBool(resp.Enabled)), | ||
) | ||
|
||
return ofrep.EvaluationBridgeOutput{ | ||
FlagKey: resp.FlagKey, | ||
Variant: strconv.FormatBool(resp.Enabled), | ||
Reason: resp.Reason, | ||
Value: resp.Enabled, | ||
}, nil | ||
default: | ||
return ofrep.EvaluationBridgeOutput{}, errors.New("unsupported flag type for ofrep bridge") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package evaluation | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"go.flipt.io/flipt/internal/server/ofrep" | ||
"go.flipt.io/flipt/internal/storage" | ||
"go.flipt.io/flipt/rpc/flipt" | ||
rpcevaluation "go.flipt.io/flipt/rpc/flipt/evaluation" | ||
"go.uber.org/zap/zaptest" | ||
) | ||
|
||
func TestOFREPEvaluationBridge_Variant(t *testing.T) { | ||
var ( | ||
flagKey = "test-flag" | ||
namespaceKey = "test-namespace" | ||
store = &evaluationStoreMock{} | ||
logger = zaptest.NewLogger(t) | ||
s = New(logger, store) | ||
flag = &flipt.Flag{ | ||
NamespaceKey: namespaceKey, | ||
Key: flagKey, | ||
Enabled: true, | ||
Type: flipt.FlagType_VARIANT_FLAG_TYPE, | ||
} | ||
) | ||
|
||
store.On("GetFlag", mock.Anything, storage.NewResource(namespaceKey, flagKey)).Return(flag, nil) | ||
|
||
store.On("GetEvaluationRules", mock.Anything, storage.NewResource(namespaceKey, flagKey)).Return([]*storage.EvaluationRule{ | ||
{ | ||
ID: "1", | ||
FlagKey: flagKey, | ||
Rank: 0, | ||
Segments: map[string]*storage.EvaluationSegment{ | ||
"bar": { | ||
SegmentKey: "bar", | ||
MatchType: flipt.MatchType_ANY_MATCH_TYPE, | ||
}, | ||
}, | ||
}, | ||
}, nil) | ||
|
||
store.On("GetEvaluationDistributions", mock.Anything, storage.NewID("1")).Return([]*storage.EvaluationDistribution{ | ||
{ | ||
ID: "4", | ||
RuleID: "1", | ||
VariantID: "5", | ||
Rollout: 100, | ||
VariantKey: "boz", | ||
}, | ||
}, nil) | ||
|
||
output, err := s.OFREPEvaluationBridge(context.TODO(), ofrep.EvaluationBridgeInput{ | ||
FlagKey: flagKey, | ||
NamespaceKey: namespaceKey, | ||
Context: map[string]string{ | ||
"hello": "world", | ||
"targetingKey": "12345", | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, flagKey, output.FlagKey) | ||
assert.Equal(t, rpcevaluation.EvaluationReason_MATCH_EVALUATION_REASON, output.Reason) | ||
assert.Equal(t, "boz", output.Variant) | ||
assert.Equal(t, "boz", output.Value) | ||
} | ||
|
||
func TestOFREPEvaluationBridge_Boolean(t *testing.T) { | ||
var ( | ||
flagKey = "test-flag" | ||
namespaceKey = "test-namespace" | ||
store = &evaluationStoreMock{} | ||
logger = zaptest.NewLogger(t) | ||
s = New(logger, store) | ||
flag = &flipt.Flag{ | ||
NamespaceKey: namespaceKey, | ||
Key: flagKey, | ||
Enabled: true, | ||
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE, | ||
} | ||
) | ||
|
||
store.On("GetFlag", mock.Anything, storage.NewResource(namespaceKey, flagKey)).Return(flag, nil) | ||
|
||
store.On("GetEvaluationRollouts", mock.Anything, storage.NewResource(namespaceKey, flagKey)).Return([]*storage.EvaluationRollout{}, nil) | ||
|
||
output, err := s.OFREPEvaluationBridge(context.TODO(), ofrep.EvaluationBridgeInput{ | ||
FlagKey: flagKey, | ||
NamespaceKey: namespaceKey, | ||
Context: map[string]string{ | ||
"targetingKey": "12345", | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, flagKey, output.FlagKey) | ||
assert.Equal(t, rpcevaluation.EvaluationReason_DEFAULT_EVALUATION_REASON, output.Reason) | ||
assert.Equal(t, "true", output.Variant) | ||
assert.Equal(t, true, output.Value) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ofrep | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
var _ Bridge = &bridgeMock{} | ||
|
||
type bridgeMock struct { | ||
mock.Mock | ||
} | ||
|
||
func (b *bridgeMock) OFREPEvaluationBridge(ctx context.Context, input EvaluationBridgeInput) (EvaluationBridgeOutput, error) { | ||
args := b.Called(ctx, input) | ||
return args.Get(0).(EvaluationBridgeOutput), args.Error(1) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package ofrep | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type errorCode string | ||
|
||
const ( | ||
errorCodeFlagNotFound errorCode = "FLAG_NOT_FOUND" | ||
errorCodeParseError errorCode = "PARSE_ERROR" | ||
errorCodeTargetingKeyMissing errorCode = "TARGETING_KEY_MISSING" | ||
errorCodeInvalidContext errorCode = "INVALID_CONTEXT" | ||
errorCodeParseGeneral errorCode = "GENERAL" | ||
) | ||
|
||
type errorSchema struct { | ||
Key string `json:"key,omitempty"` | ||
ErrorCode string `json:"errorCode,omitempty"` | ||
ErrorDetails string `json:"errorDetails"` | ||
} | ||
|
||
func NewBadRequestError(key string, err error) error { | ||
msg, err := json.Marshal(errorSchema{ | ||
Key: key, | ||
ErrorCode: string(errorCodeParseGeneral), | ||
ErrorDetails: err.Error(), | ||
}) | ||
if err != nil { | ||
return NewInternalServerError(err) | ||
} | ||
|
||
return status.Error(codes.InvalidArgument, string(msg)) | ||
} | ||
|
||
func NewUnauthenticatedError() error { | ||
msg, err := json.Marshal(errorSchema{ErrorDetails: "unauthenticated error"}) | ||
if err != nil { | ||
return NewInternalServerError(err) | ||
} | ||
|
||
return status.Error(codes.Unauthenticated, string(msg)) | ||
} | ||
|
||
func NewUnauthorizedError() error { | ||
msg, err := json.Marshal(errorSchema{ErrorDetails: "unauthorized error"}) | ||
if err != nil { | ||
return NewInternalServerError(err) | ||
} | ||
|
||
return status.Error(codes.PermissionDenied, string(msg)) | ||
} | ||
|
||
func NewFlagNotFoundError(key string) error { | ||
msg, err := json.Marshal(errorSchema{ | ||
Key: key, | ||
ErrorCode: string(errorCodeFlagNotFound), | ||
}) | ||
if err != nil { | ||
return NewInternalServerError(err) | ||
} | ||
|
||
return status.Error(codes.NotFound, string(msg)) | ||
} | ||
|
||
func NewTargetingKeyMissing() error { | ||
msg, err := json.Marshal(errorSchema{ | ||
ErrorCode: string(errorCodeTargetingKeyMissing), | ||
ErrorDetails: "flag key was not provided", | ||
}) | ||
if err != nil { | ||
return NewInternalServerError(err) | ||
} | ||
|
||
return status.Error(codes.InvalidArgument, string(msg)) | ||
} | ||
|
||
func NewInternalServerError(err error) error { | ||
return status.Error( | ||
codes.Internal, | ||
fmt.Sprintf(`{"errorDetails": "%s"}`, strings.ReplaceAll(err.Error(), `"`, `\"`)), | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.