You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: libs/providers/multi-provider/README.md
+68-3Lines changed: 68 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,21 +6,23 @@ the final result. Different evaluation strategies can be defined to control whic
6
6
7
7
The Multi-Provider is a powerful tool for performing migrations between flag providers, or combining multiple providers into a single
8
8
feature flagging interface. For example:
9
+
9
10
-*Migration*: When migrating between two providers, you can run both in parallel under a unified flagging interface. As flags are added to the
10
11
new provider, the Multi-Provider will automatically find and return them, falling back to the old provider if the new provider does not have
11
-
-*Multiple Data Sources*: The Multi-Provider allows you to seamlessly combine many sources of flagging data, such as environment variables,
12
+
-*Multiple Data Sources*: The Multi-Provider allows you to seamlessly combine many sources of flagging data, such as environment variables,
12
13
local files, database values and SaaS hosted feature management systems.
13
14
14
15
## Installation
15
16
16
-
```
17
+
```bash
17
18
$ npm install @openfeature/multi-provider
18
19
```
19
20
20
21
> [!TIP]
21
22
> This provider is designed to be used with the [Node.js SDK](https://openfeature.dev/docs/reference/technologies/server/javascript/).
22
23
23
24
## Usage
25
+
24
26
The Multi-Provider is initialized with an array of providers it should evaluate:
25
27
26
28
```typescript
@@ -66,8 +68,10 @@ const multiProvider = new MultiProvider(
66
68
newFirstSuccessfulStrategy()
67
69
)
68
70
```
71
+
69
72
The Multi-Provider comes with three strategies out of the box:
70
-
`FirstMatchStrategy` (default): Evaluates all providers in order and returns the first successful result. Providers that indicate FLAG_NOT_FOUND error will be skipped and the next provider will be evaluated. Any other error will cause the operation to fail and the set of errors to be thrown.
73
+
74
+
-`FirstMatchStrategy` (default): Evaluates all providers in order and returns the first successful result. Providers that indicate FLAG_NOT_FOUND error will be skipped and the next provider will be evaluated. Any other error will cause the operation to fail and the set of errors to be thrown.
71
75
-`FirstSuccessfulStrategy`: Evaluates all providers in order and returns the first successful result. Any error will cause that provider to be skipped.
72
76
If no successful result is returned, the set of errors will be thrown.
73
77
-`ComparisonStrategy`: Evaluates all providers in parallel. If every provider returns a successful result with the same value, then that result is returned.
@@ -97,8 +101,59 @@ const multiProvider = new MultiProvider(
97
101
```
98
102
The first argument is the "fallback provider" whose value to use in the event that providers do not agree. It should be the same object reference as one of the providers in the list. The second argument is a callback function that will be executed when a mismatch is detected. The callback will be passed an object containing the details of each provider's resolution, including the flag key, the value returned, and any errors that were thrown.
99
103
104
+
## Tracking Support
105
+
106
+
The Multi-Provider supports tracking events across multiple providers. When you call the `track` method, it will by default send the tracking event to all underlying providers that implement the `track` method.
It is also possible to implement your own strategy if the above options do not fit your use case. To do so, create a class which implements the "BaseEvaluationStrategy":
156
+
102
157
```typescript
103
158
exportabstractclassBaseEvaluationStrategy {
104
159
public runMode:'parallel'|'sequential'='sequential';
@@ -111,13 +166,21 @@ export abstract class BaseEvaluationStrategy {
111
166
result:ProviderResolutionResult<T>,
112
167
):boolean;
113
168
169
+
abstract shouldTrackWithThisProvider(
170
+
strategyContext:StrategyPerProviderContext,
171
+
context:EvaluationContext,
172
+
trackingEventName:string,
173
+
trackingEventDetails:TrackingEventDetails,
174
+
):boolean;
175
+
114
176
abstract determineFinalResult<TextendsFlagValue>(
115
177
strategyContext:StrategyEvaluationContext,
116
178
context:EvaluationContext,
117
179
resolutions:ProviderResolutionResult<T>[],
118
180
):FinalResult<T>;
119
181
}
120
182
```
183
+
121
184
The `runMode` property determines whether the list of providers will be evaluated sequentially or in parallel.
122
185
123
186
The `shouldEvaluateThisProvider` method is called just before a provider is evaluated by the Multi-Provider. If the function returns `false`, then
@@ -127,6 +190,8 @@ Check the type definitions for the full list.
127
190
The `shouldEvaluateNextProvider` function is called after a provider is evaluated. If it returns `true`, the next provider in the sequence will be called,
128
191
otherwise no more providers will be evaluated. It is called with the same data as `shouldEvaluateThisProvider` as well as the details about the evaluation result. This function is not called when the `runMode` is `parallel`.
129
192
193
+
The `shouldTrackWithThisProvider` method is called before sending a tracking event to each provider. Return `false` to skip tracking with that provider. By default, it only tracks with providers that are in a ready state (not `NOT_READY` or `FATAL`). Override this method to implement custom tracking logic based on the tracking event name, details, or provider characteristics.
194
+
130
195
The `determineFinalResult` function is called after all providers have been called, or the `shouldEvaluateNextProvider` function returned false. It is called
131
196
with a list of results from all the individual providers' evaluations. It returns the final decision for evaluation result, or throws an error if needed.
0 commit comments