-
Notifications
You must be signed in to change notification settings - Fork 63
feat(flagd): add new context enrichment approach for in-process provider #730
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
base: main
Are you sure you want to change the base?
Changes from all commits
340cc23
0fd48a3
b6c9664
0286738
38fd36c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
+1 −1 | .release-please-manifest.json | |
+7 −0 | CHANGELOG.md | |
+1 −1 | docker-compose.yaml | |
+24 −0 | gherkin/connection.feature | |
+1 −1 | version.txt |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package process | |
import ( | ||
"context" | ||
"fmt" | ||
|
||
"regexp" | ||
parallel "sync" | ||
|
||
|
@@ -33,6 +32,8 @@ type InProcess struct { | |
sync sync.ISync | ||
syncEnd context.CancelFunc | ||
wg parallel.WaitGroup | ||
contextValues map[string]any | ||
mtx parallel.RWMutex | ||
} | ||
|
||
type Configuration struct { | ||
|
@@ -113,6 +114,12 @@ func (i *InProcess) Init() error { | |
case data := <-syncChan: | ||
// re-syncs are ignored as we only support single flag sync source | ||
changes, _, err := i.evaluator.SetState(data) | ||
if data.SyncContext != nil { | ||
i.mtx.Lock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where do you see the benefit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly for consistency. But you are right, it's just the Java dev in me that sees lock/unlock outside of a try/finally context and becomes uneasy |
||
i.contextValues = data.SyncContext.AsMap() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In java we have an optional lambda to manipulate this... I think it should be simple to add it here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I think you added this in the hook itself. In the Java impl, we only run this transform function when we actually get the sync data. I think there's a reason to do it in the hook so many times, when we can do it before we store the context. |
||
i.mtx.Unlock() | ||
} | ||
|
||
if err != nil { | ||
i.events <- of.Event{ | ||
ProviderName: "flagd", EventType: of.ProviderError, | ||
|
@@ -294,6 +301,22 @@ func (i *InProcess) appendMetadata(evalMetadata model.Metadata) { | |
} | ||
} | ||
|
||
func (i *InProcess) ContextValues() map[string]any { | ||
i.mtx.RLock() | ||
defer i.mtx.RUnlock() | ||
|
||
if i.contextValues == nil { | ||
return nil | ||
} | ||
|
||
// Return a copy to prevent mutation of internal state | ||
contextValuesCopy := make(map[string]any, len(i.contextValues)) | ||
for k, v := range i.contextValues { | ||
contextValuesCopy[k] = v | ||
} | ||
return contextValuesCopy | ||
} | ||
|
||
// makeSyncProvider is a helper to create sync.ISync and return the underlying uri used by it to the caller | ||
func makeSyncProvider(cfg Configuration, log *logger.Logger) (sync.ISync, string) { | ||
if cfg.CustomSyncProvider != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package flagd | ||
|
||
import ( | ||
"context" | ||
"github.com/open-feature/go-sdk/openfeature" | ||
) | ||
|
||
type ContextEnricher func(map[string]any) *openfeature.EvaluationContext | ||
|
||
type SyncContextHook struct { | ||
openfeature.UnimplementedHook | ||
contextEnricher func() *openfeature.EvaluationContext | ||
} | ||
|
||
func NewSyncContextHook(contextEnricher func() *openfeature.EvaluationContext) SyncContextHook { | ||
return SyncContextHook{contextEnricher: contextEnricher} | ||
} | ||
|
||
func (hook SyncContextHook) Before(ctx context.Context, hookContext openfeature.HookContext, hookHints openfeature.HookHints) (*openfeature.EvaluationContext, error) { | ||
return hook.contextEnricher(), nil | ||
} |
Uh oh!
There was an error while loading. Please reload this page.