|
| 1 | +OpenFeature Multi-Provider |
| 2 | +------------ |
| 3 | + |
| 4 | +> [!WARNING] |
| 5 | +> The multi package for the go-sdk is experimental. |
| 6 | +
|
| 7 | + |
| 8 | +The multi-provider allows you to use multiple underlying providers as sources of flag data for the OpenFeature server SDK. |
| 9 | +The multi-provider acts as a wrapper providing a unified interface to interact with all of those providers at once. |
| 10 | +When a flag is being evaluated, the Multi-Provider will consult each underlying provider it is managing in order to |
| 11 | +determine the final result. Different evaluation strategies can be defined to control which providers get evaluated and |
| 12 | +which result is used. |
| 13 | + |
| 14 | +The multi-provider is defined within [Appendix A: Included Utilities](https://openfeature.dev/specification/appendix-a#multi-provider) |
| 15 | +of the openfeature spec. |
| 16 | + |
| 17 | +The multi-provider is a powerful tool for performing migrations between flag providers, or combining multiple providers |
| 18 | +into a single feature flagging interface. For example: |
| 19 | + |
| 20 | +- **Migration**: When migrating between two providers, you can run both in parallel under a unified flagging interface. |
| 21 | + As flags are added to the new provider, the multi-provider will automatically find and return them, falling back to the old provider |
| 22 | + if the new provider does not have |
| 23 | +- **Multiple Data Sources**: The multi-provider allows you to seamlessly combine many sources of flagging data, such as |
| 24 | + environment variables, local files, database values and SaaS hosted feature management systems. |
| 25 | + |
| 26 | +# Usage |
| 27 | + |
| 28 | +```go |
| 29 | +import ( |
| 30 | + "github.com/open-feature/go-sdk/openfeature" |
| 31 | + "github.com/open-feature/go-sdk/openfeature/multi" |
| 32 | + "github.com/open-feature/go-sdk/openfeature/memprovider" |
| 33 | +) |
| 34 | + |
| 35 | +providers := make(multi.ProviderMap) |
| 36 | +providers["providerA"] = memprovider.NewInMemoryProvider(map[string]memprovider.InMemoryFlag{}) |
| 37 | +providers["providerB"] = myCustomProvider |
| 38 | +mprovider, err := multi.NewProvider(providers, multi.StrategyFirstMatch) |
| 39 | +if err != nil { |
| 40 | + return err |
| 41 | +} |
| 42 | + |
| 43 | +openfeature.SetNamedProviderAndWait("multiprovider", mprovider) |
| 44 | +``` |
| 45 | + |
| 46 | +# Strategies |
| 47 | + |
| 48 | +There are three strategies that are defined by the spec and are available within this multi-provider implementation. In |
| 49 | +addition to the three provided strategies a custom strategy can be defined as well. |
| 50 | + |
| 51 | +The three provided strategies are: |
| 52 | + |
| 53 | +- _First Match_ |
| 54 | +- _First Success_ |
| 55 | +- _Comparison_ |
| 56 | + |
| 57 | +## First Match Strategy |
| 58 | + |
| 59 | +The first match strategy works by **sequentially** calling each provider until a valid result is returned. |
| 60 | +The first provider that returns a result will be used. It will try calling the next provider whenever it encounters a `FLAG_NOT_FOUND` |
| 61 | +error. However, if a provider returns an error other than `FLAG_NOT_FOUND` the provider will stop and return the default |
| 62 | +value along with setting the error details if a detailed request is issued. |
| 63 | + |
| 64 | +## First Success Strategy |
| 65 | + |
| 66 | +The first success strategy also works by calling each provider **sequentially**. The first provider that returns a response |
| 67 | +with no errors is used. This differs from the first match strategy in that any provider raising an error will not halt |
| 68 | +calling the next provider if a successful result has not yet been encountered. If no provider provides a successful result |
| 69 | +the default value will be returned to the caller. |
| 70 | + |
| 71 | +## Comparison Strategy |
| 72 | + |
| 73 | +The comparison strategy works by calling each provider in **parallel**. All results are collected from each provider and |
| 74 | +then the resolved results are compared to each other. If they all agree then that value is returned. If not a fallback |
| 75 | +provider can be specified to be executed instead or the default value will be returned. If a provider returns |
| 76 | +`FLAG_NOT_FOUND` that result will not be included in the comparison. If all providers return not found then the default |
| 77 | +value is returned. Finally, if any provider returns an error other than `FLAG_NOT_FOUND` the evaluation immediately stops |
| 78 | +and that error result is returned with the default value. |
| 79 | + |
| 80 | +The fallback provider can be set using the `WithFallbackProvider` [`Option`](#options). |
| 81 | + |
| 82 | +Special care must be taken when this strategy is used with `ObjectEvaluation`. If the resulting value is not a |
| 83 | +[`comparable`](https://go.dev/blog/comparable) type then the default result or fallback provider will always be used. In |
| 84 | +order to evaluate non `comparable` types a `Comparator` function must be provided as an `Option` to the constructor. |
| 85 | + |
| 86 | +## Custom Strategies |
| 87 | + |
| 88 | +A custom strategy can be defined using the `WithCustomStrategy` `Option` along with the `StrategyCustom` constant. |
| 89 | +A custom strategy is defined by the following generic function signature: |
| 90 | + |
| 91 | +```go |
| 92 | +StrategyFn[T FlagTypes] func(ctx context.Context, flag string, defaultValue T, flatCtx openfeature.FlattenedContext) openfeature.GenericResolutionDetail[T] |
| 93 | +``` |
| 94 | + |
| 95 | +However, this doesn't provide any way to retrieve the providers! Therefore, there's the type `StrategyConstructor` that |
| 96 | +is called for you to close over the providers inside your `StratetegyFn` implementation. |
| 97 | + |
| 98 | +```go |
| 99 | +type StrategyConstructor func(providers []*NamedProvider) StrategyFn[FlagTypes] |
| 100 | +``` |
| 101 | + |
| 102 | +Build your strategy to wrap around the slice of providers |
| 103 | +```go |
| 104 | +option := multi.WithCustomStrategy(func(providers []*NamedProvider) StrategyFn[FlagTypes] { |
| 105 | + return func[T FlagTypes](ctx context.Context, flag string, defaultValue T, flatCtx openfeature.FlattenedContext) openfeature.GenericResolutionDetail[T] { |
| 106 | + // implementation |
| 107 | + // ... |
| 108 | + } |
| 109 | +}) |
| 110 | +``` |
| 111 | + |
| 112 | +It is highly recommended to use the provided exposed functions to build your custom strategy. Specifically, the functions |
| 113 | +`BuildDefaultResult` & `Evaluate` are exposed for those implementing their own custom strategies. |
| 114 | + |
| 115 | +The `Evaluate` method should be used for evaluating the result of a single `NamedProvider`. It determines the evaluation |
| 116 | +type via the type of the generic `defaultVal` parameter. |
| 117 | + |
| 118 | +The `BuildDefaultResult` method should be called when an error is encountered or the strategy "fails" and needs to return |
| 119 | +the default result passed to one of the Evaluation methods of `openfeature.FeatureProvider`. |
| 120 | + |
| 121 | +# Options |
| 122 | + |
| 123 | +The `multi.NewProvider` constructor implements the optional pattern for setting additional configuration. |
| 124 | + |
| 125 | +## General Options |
| 126 | + |
| 127 | +### `WithLogger` |
| 128 | + |
| 129 | +Allows for providing a `slog.Logger` instance for internal logging of the multi-provider's evaluation processing for debugging |
| 130 | +purposes. By default, are logs are discarded unless this option is used. |
| 131 | + |
| 132 | +### `WithCustomStrategy` |
| 133 | + |
| 134 | +Allows for setting a custom strategy function for the evaluation of providers. This must be used in conjunction with the |
| 135 | +`StrategyCustom` `EvaluationStrategy` parameter. The option itself takes a `StrategyConstructor` function, which is |
| 136 | +essentially a factory that allows the `StrategyFn` to wrap around a slice of `NamedProvider` instances. |
| 137 | + |
| 138 | +### `WithGlobalHooks` |
| 139 | + |
| 140 | +Allows for setting global hooks for the multi-provider. These are `openfeature.Hook` implementations that affect |
| 141 | +**all** internal `FeatureProvider` instances. |
| 142 | + |
| 143 | +### `WithProviderHooks` |
| 144 | + |
| 145 | +Allows for setting `openfeature.Hook` implementations on a specific named `FeatureProvider` within the multi-provider. |
| 146 | +This should only be used when hooks need to be attached to a `FeatureProvider` instance that does not implement that functionality. |
| 147 | +Using a provider name that is not known will cause an error to be returned during the creation time. This option can be |
| 148 | +used multiple times using unique provider names. |
| 149 | + |
| 150 | +## `StrategyComparision` specific options |
| 151 | + |
| 152 | +There are two options specifically for usage with the `StrategyComparision` `EvaluationStrategy`. If these options are |
| 153 | +used with a different `EvaluationStrategy` they are ignored. |
| 154 | + |
| 155 | +### `WithFallbackProvider` |
| 156 | + |
| 157 | +When the results are not in agreement with each other the fallback provider will be called. The result of this provider |
| 158 | +is what will be returned to the caller. If no fallback provider is set then the default value will be returned instead. |
| 159 | + |
| 160 | +### `WithCustomComparator` |
| 161 | + |
| 162 | +When using `ObjectEvaluation` there are cases where the results are not able to be compared to each other by default. |
| 163 | +This happens if the returned type is not `comparable`. In that situation all the results are passed to the custom `Comparator` |
| 164 | +to evaluate if they are in agreement or not. If not provided and the return type is not `comparable` then either the fallback |
| 165 | +provider is used or the default value. |
0 commit comments