forked from open-feature/js-sdk-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-feature-flag-provider.ts
384 lines (347 loc) · 14.9 KB
/
go-feature-flag-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import {
ErrorCode,
EvaluationContext,
FlagNotFoundError,
JsonValue,
Logger,
Provider,
ProviderStatus,
ResolutionDetails,
StandardResolutionReasons,
TypeMismatchError,
} from '@openfeature/js-sdk';
import axios from 'axios';
import {copy} from 'copy-anything';
import {transformContext} from './context-transformer';
import {ProxyNotReady} from './errors/proxyNotReady';
import {ProxyTimeout} from './errors/proxyTimeout';
import {UnknownError} from './errors/unknownError';
import {Unauthorized} from './errors/unauthorized';
import {LRUCache} from 'lru-cache';
import {
DataCollectorRequest,
DataCollectorResponse,
FeatureEvent,
GoFeatureFlagProviderOptions,
GoFeatureFlagProxyRequest,
GoFeatureFlagProxyResponse,
GoFeatureFlagUser,
} from './model';
// GoFeatureFlagProvider is the official Open-feature provider for GO Feature Flag.
export class GoFeatureFlagProvider implements Provider {
metadata = {
name: GoFeatureFlagProvider.name,
};
// endpoint of your go-feature-flag relay proxy instance
private readonly endpoint: string;
// timeout in millisecond before we consider the request as a failure
private readonly timeout: number;
// cache contains the local cache used in the provider to avoid calling the relay-proxy for every evaluation
private readonly cache?: LRUCache<string, ResolutionDetails<any>>;
// bgSchedulerId contains the id of the setInterval that is running.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
private bgScheduler?: NodeJS.Timer;
// dataCollectorBuffer contains all the FeatureEvents that we need to send to the relay-proxy for data collection.
private dataCollectorBuffer?: FeatureEvent<any>[];
// dataCollectorMetadata are the metadata used when calling the data collector endpoint
private readonly dataCollectorMetadata: Record<string, string> = {
provider: 'open-feature-js-sdk',
};
// cacheTTL is the time we keep the evaluation in the cache before we consider it as obsolete.
// If you want to keep the value forever you can set the FlagCacheTTL field to -1
private readonly cacheTTL?: number;
// dataFlushInterval interval time (in millisecond) we use to call the relay proxy to collect data.
private readonly dataFlushInterval: number;
// disableDataCollection set to true if you don't want to collect the usage of flags retrieved in the cache.
private readonly disableDataCollection: boolean;
// logger is the Open Feature logger to use
private logger?: Logger;
private _status: ProviderStatus = ProviderStatus.NOT_READY;
constructor(options: GoFeatureFlagProviderOptions, logger?: Logger) {
this.timeout = options.timeout || 0; // default is 0 = no timeout
this.endpoint = options.endpoint;
this.cacheTTL = options.flagCacheTTL !== undefined && options.flagCacheTTL !== 0 ? options.flagCacheTTL : 1000 * 60;
this.dataFlushInterval = options.dataFlushInterval || 1000 * 60;
this.disableDataCollection = options.disableDataCollection || false;
this.logger = logger;
// Add API key to the headers
if (options.apiKey) {
axios.defaults.headers.common['Authorization'] = `Bearer ${options.apiKey}`;
}
if (!options.disableCache) {
const cacheSize = options.flagCacheSize !== undefined && options.flagCacheSize !== 0 ? options.flagCacheSize : 10000;
this.cache = new LRUCache({maxSize: cacheSize, sizeCalculation: () => 1});
}
}
/**
* initialize is called everytime the provider is instanced inside GO Feature Flag.
* It will start the background process for data collection to be able to run every X ms.
*/
async initialize() {
if (!this.disableDataCollection) {
this.bgScheduler = setInterval(async () => await this.callGoffDataCollection(), this.dataFlushInterval)
this.dataCollectorBuffer = []
}
this._status = ProviderStatus.READY;
}
get status(){
return this._status;
}
/**
* onClose is called everytime OpenFeature.Close() function is called.
* It will terminate gracefully the provider and ensure that all the data are send to the relay-proxy.
*/
async onClose() {
if (this.cache !== undefined && this.bgScheduler !== undefined) {
// we stop the background task to call the data collector endpoint
clearInterval(this.bgScheduler);
// We call the data collector with what is still in the buffer.
await this.callGoffDataCollection()
}
}
/**
* callGoffDataCollection is a function called periodically to send the usage of the flag to the
* central service in charge of collecting the data.
*/
async callGoffDataCollection() {
if (this.dataCollectorBuffer?.length === 0) {
return;
}
const dataToSend = copy(this.dataCollectorBuffer) || [];
this.dataCollectorBuffer = [];
const request: DataCollectorRequest<boolean> = {events: dataToSend, meta: this.dataCollectorMetadata,}
const endpointURL = new URL(this.endpoint);
endpointURL.pathname = 'v1/data/collector';
try {
await axios.post<DataCollectorResponse>(endpointURL.toString(), request, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
timeout: this.timeout,
});
} catch (e) {
this.logger?.error(`impossible to send the data to the collector: ${e}`)
// if we have an issue calling the collector we put the data back in the buffer
this.dataCollectorBuffer = [...this.dataCollectorBuffer, ...dataToSend];
}
}
/**
* resolveBooleanEvaluation is calling the GO Feature Flag relay-proxy API and return a boolean value.
* @param flagKey - name of your feature flag key.
* @param defaultValue - default value is used if we are not able to evaluate the flag for this user.
* @param context - the context used for flag evaluation.
* @return {Promise<ResolutionDetails<boolean>>} An object containing the result of the flag evaluation by GO Feature Flag.
* @throws {ProxyNotReady} When we are not able to communicate with the relay-proxy
* @throws {ProxyTimeout} When the HTTP call is timing out
* @throws {UnknownError} When an unknown error occurs
* @throws {TypeMismatchError} When the type of the variation is not the one expected
* @throws {FlagNotFoundError} When the flag does not exist
*/
async resolveBooleanEvaluation(
flagKey: string,
defaultValue: boolean,
context: EvaluationContext
): Promise<ResolutionDetails<boolean>> {
return this.resolveEvaluationGoFeatureFlagProxy<boolean>(
flagKey,
defaultValue,
transformContext(context),
'boolean'
);
}
/**
* resolveStringEvaluation is calling the GO Feature Flag relay-proxy API and return a string value.
* @param flagKey - name of your feature flag key.
* @param defaultValue - default value is used if we are not able to evaluate the flag for this user.
* @param context - the context used for flag evaluation.
* @return {Promise<ResolutionDetails<string>>} An object containing the result of the flag evaluation by GO Feature Flag.
* @throws {ProxyNotReady} When we are not able to communicate with the relay-proxy
* @throws {ProxyTimeout} When the HTTP call is timing out
* @throws {UnknownError} When an unknown error occurs
* @throws {TypeMismatchError} When the type of the variation is not the one expected
* @throws {FlagNotFoundError} When the flag does not exist
*/
async resolveStringEvaluation(
flagKey: string,
defaultValue: string,
context: EvaluationContext
): Promise<ResolutionDetails<string>> {
return this.resolveEvaluationGoFeatureFlagProxy<string>(
flagKey,
defaultValue,
transformContext(context),
'string'
);
}
/**
* resolveNumberEvaluation is calling the GO Feature Flag relay-proxy API and return a number value.
* @param flagKey - name of your feature flag key.
* @param defaultValue - default value is used if we are not able to evaluate the flag for this user.
* @param context - the context used for flag evaluation.
* @return {Promise<ResolutionDetails<number>>} An object containing the result of the flag evaluation by GO Feature Flag.
* @throws {ProxyNotReady} When we are not able to communicate with the relay-proxy
* @throws {ProxyTimeout} When the HTTP call is timing out
* @throws {UnknownError} When an unknown error occurs
* @throws {TypeMismatchError} When the type of the variation is not the one expected
* @throws {FlagNotFoundError} When the flag does not exist
*/
async resolveNumberEvaluation(
flagKey: string,
defaultValue: number,
context: EvaluationContext
): Promise<ResolutionDetails<number>> {
return this.resolveEvaluationGoFeatureFlagProxy<number>(
flagKey,
defaultValue,
transformContext(context),
'number'
);
}
/**
* resolveObjectEvaluation is calling the GO Feature Flag relay-proxy API and return an object.
* @param flagKey - name of your feature flag key.
* @param defaultValue - default value is used if we are not able to evaluate the flag for this user.
* @param context - the context used for flag evaluation.
* @return {Promise<ResolutionDetails<U extends JsonValue>>} An object containing the result of the flag evaluation by GO Feature Flag.
* @throws {ProxyNotReady} When we are not able to communicate with the relay-proxy
* @throws {ProxyTimeout} When the HTTP call is timing out
* @throws {UnknownError} When an unknown error occurs
* @throws {TypeMismatchError} When the type of the variation is not the one expected
* @throws {FlagNotFoundError} When the flag does not exist
*/
async resolveObjectEvaluation<U extends JsonValue>(
flagKey: string,
defaultValue: U,
context: EvaluationContext
): Promise<ResolutionDetails<U>> {
return this.resolveEvaluationGoFeatureFlagProxy<U>(
flagKey,
defaultValue,
transformContext(context),
'object'
);
}
/**
* resolveEvaluationGoFeatureFlagProxy is a generic function the call the GO Feature Flag relay-proxy API
* to evaluate the flag.
* This is the same call for all types of flags so this function also checks if the return call is the one expected.
* @param flagKey - name of your feature flag key.
* @param defaultValue - default value is used if we are not able to evaluate the flag for this user.
* @param user - the user against who we will evaluate the flag.
* @param expectedType - the type we expect the result to be
* @return {Promise<ResolutionDetails<T>>} An object containing the result of the flag evaluation by GO Feature Flag.
* @throws {ProxyNotReady} When we are not able to communicate with the relay-proxy
* @throws {ProxyTimeout} When the HTTP call is timing out
* @throws {UnknownError} When an unknown error occurs
* @throws {TypeMismatchError} When the type of the variation is not the one expected
* @throws {FlagNotFoundError} When the flag does not exist
*/
async resolveEvaluationGoFeatureFlagProxy<T>(
flagKey: string,
defaultValue: T,
user: GoFeatureFlagUser,
expectedType: string
): Promise<ResolutionDetails<T>> {
const cacheKey = `${flagKey}-${user.key}`;
// check if flag is available in the cache
if (this.cache !== undefined) {
const cacheValue = this.cache.get(cacheKey);
if (cacheValue !== undefined) {
// Building and inserting an event to the data collector buffer,
// so we will be able to bulk send these events to GO Feature Flag.
const dataCollectorEvent: FeatureEvent<T> = {
contextKind: user.anonymous ? 'anonymousUser' : 'user',
kind: 'feature',
creationDate: Math.round(Date.now() / 1000),
default: false,
key: flagKey,
value: cacheValue.value,
variation: cacheValue.variant || 'SdkDefault',
userKey: user.key,
}
this.dataCollectorBuffer?.push(dataCollectorEvent);
cacheValue.reason = StandardResolutionReasons.CACHED;
return cacheValue;
}
}
const request: GoFeatureFlagProxyRequest<T> = {user, defaultValue};
// build URL to access to the endpoint
const endpointURL = new URL(this.endpoint);
endpointURL.pathname = `v1/feature/${flagKey}/eval`;
let apiResponseData: GoFeatureFlagProxyResponse<T>;
try {
const response = await axios.post<GoFeatureFlagProxyResponse<T>>(endpointURL.toString(), request, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
timeout: this.timeout,
});
apiResponseData = response.data;
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status == 401) {
throw new Unauthorized('invalid token used to contact GO Feature Flag relay proxy instance');
}
// Impossible to contact the relay-proxy
if (
axios.isAxiosError(error) &&
(error.code === 'ECONNREFUSED' || error.response?.status === 404)
) {
throw new ProxyNotReady(
`impossible to call go-feature-flag relay proxy on ${endpointURL}`,
error
);
}
// Timeout when calling the API
if (axios.isAxiosError(error) && error.code === 'ECONNABORTED') {
throw new ProxyTimeout(
`impossible to retrieve the ${flagKey} on time`,
error
);
}
throw new UnknownError(
`unknown error while retrieving flag ${flagKey} for user ${user.key}`,
error
);
}
// Check that we received the expectedType
if (typeof apiResponseData.value !== expectedType) {
throw new TypeMismatchError(
`Flag value ${flagKey} had unexpected type ${typeof apiResponseData.value}, expected ${expectedType}.`
);
}
// Case of the flag is not found
if (apiResponseData.errorCode === ErrorCode.FLAG_NOT_FOUND) {
throw new FlagNotFoundError(
`Flag ${flagKey} was not found in your configuration`
);
}
// Case of the flag is disabled
if (apiResponseData.reason === StandardResolutionReasons.DISABLED) {
// we don't set a variant since we are using the default value, and we are not able to know
// which variant it is.
return {value: defaultValue, reason: apiResponseData.reason};
}
const sdkResponse: ResolutionDetails<T> = {
value: apiResponseData.value,
variant: apiResponseData.variationType,
reason: apiResponseData.reason?.toString() || 'UNKNOWN',
flagMetadata: apiResponseData.metadata || undefined,
};
if (Object.values(ErrorCode).includes(apiResponseData.errorCode as ErrorCode)) {
sdkResponse.errorCode = ErrorCode[apiResponseData.errorCode as ErrorCode];
} else if (apiResponseData.errorCode) {
sdkResponse.errorCode = ErrorCode.GENERAL;
}
if (this.cache !== undefined && apiResponseData.cacheable) {
if (this.cacheTTL === -1) {
this.cache.set(cacheKey, sdkResponse)
} else {
this.cache.set(cacheKey, sdkResponse, {ttl: this.cacheTTL})
}
}
return sdkResponse;
}
}