lazy feature flag evaluation
Created by: thenamankumar
Closes https://github.com/sourcegraph/sourcegraph/issues/35541. Closes https://github.com/sourcegraph/sourcegraph/issues/35542. Closes https://github.com/sourcegraph/sourcegraph/issues/35543. Closes https://github.com/sourcegraph/sourcegraph/issues/35544.
Test plan
This PR brings lazy feature flag evaluation. Currently, all feature flags are evaluated the first time user lands on the page and not when the user actually participates in the experiment.
In the old API, there is no way to evaluate a feature flag on-demand in a lazy manner. To support the purpose of this PR,
- We removed the old method FromContext which combined both evaluating all feature flags and returning the values at once
- Created two separate functions:
-
EvaluateForActorFromContext
which on-demand evaluates a single feature flag and returns its value. It is used by frontend/backend clients to evaluate flags and perform actions accordingly. -
GetEvaluatedFlagsFromContext
returns the list of already evaluated feature flags for the user. It is only used to pass the feature flags context to all the event logs we record.
-
This PR implements a new graphql field evaluateFeatureFlag
which evaluates a feature flag and returns the value using EvaluateForActorFromContext
. It also stores the already evaluated feature flags in Redis cache and passes the evaluated feature flags' context to all the event logs using GetEvaluatedFlagsFromContext
.
Why store evaluated feature flags in Redis?
Feature flags are evaluated not just for logged in users but un-authenticated visitors as well. That means we need to store feature flag value for each of those monthly visitors separately for each feature flag, delete them if/when the flag is deleted and also query them when logging events. Redis provides easy management of this cache when this data is only used to pass onto event logs and is not used in any business logic.