Skip to content

Commit 49d80b6

Browse files
Adding a log if we are not able to send the data to the collector
Signed-off-by: Thomas Poignant <[email protected]>
1 parent 1b323cb commit 49d80b6

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

libs/providers/go-feature-flag/src/lib/go-feature-flag-provider.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {UnknownError} from './errors/unknownError';
1717
import {Unauthorized} from './errors/unauthorized';
1818
import {GoFeatureFlagProvider} from './go-feature-flag-provider';
1919
import {GoFeatureFlagProxyResponse} from './model';
20+
import TestLogger from './test-logger';
2021

2122
describe('GoFeatureFlagProvider', () => {
2223
const endpoint = 'http://go-feature-flag-relay-proxy.local:1031/';
@@ -35,12 +36,15 @@ describe('GoFeatureFlagProvider', () => {
3536
},
3637
cacheable: true,
3738
};
39+
3840
let goff: GoFeatureFlagProvider;
41+
const testLogger = new TestLogger();
3942

4043
afterEach(async () => {
4144
await OpenFeature.close();
4245
await axiosMock.reset();
4346
await axiosMock.resetHistory();
47+
testLogger.reset();
4448
});
4549

4650
beforeEach(async () => {
@@ -907,5 +911,31 @@ describe('GoFeatureFlagProvider', () => {
907911

908912
expect(collectorCalls.length).toBe(0);
909913
});
914+
915+
it('should have a log when data collector is not available', async () => {
916+
const flagName = 'random-flag';
917+
const targetingKey = 'user-key';
918+
const dns = `${endpoint}v1/feature/${flagName}/eval`;
919+
920+
axiosMock.onPost(dns).reply(200, validBoolResponse);
921+
axiosMock.onPost(dataCollectorEndpoint).reply(500, {});
922+
923+
const goff = new GoFeatureFlagProvider({
924+
endpoint,
925+
flagCacheTTL: 3000,
926+
flagCacheSize: 100,
927+
dataFlushInterval: 2000, // in milliseconds
928+
}, testLogger)
929+
const providerName = expect.getState().currentTestName || 'test';
930+
OpenFeature.setProvider(providerName, goff);
931+
const cli = OpenFeature.getClient(providerName);
932+
await cli.getBooleanDetails(flagName, false, {targetingKey});
933+
await cli.getBooleanDetails(flagName, false, {targetingKey});
934+
await OpenFeature.close();
935+
936+
console.log(testLogger)
937+
expect(testLogger.inMemoryLogger['error'].length).toBe(1);
938+
expect(testLogger.inMemoryLogger['error']).toContain('impossible to send the data to the collector: Error: Request failed with status code 500')
939+
});
910940
});
911941
});

libs/providers/go-feature-flag/src/lib/go-feature-flag-provider.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
EvaluationContext,
44
FlagNotFoundError,
55
JsonValue,
6+
Logger,
67
Provider,
78
ResolutionDetails,
89
StandardResolutionReasons,
@@ -59,16 +60,20 @@ export class GoFeatureFlagProvider implements Provider {
5960

6061
// dataFlushInterval interval time (in millisecond) we use to call the relay proxy to collect data.
6162
private readonly dataFlushInterval: number;
62-
63+
6364
// disableDataCollection set to true if you don't want to collect the usage of flags retrieved in the cache.
6465
private readonly disableDataCollection: boolean;
6566

66-
constructor(options: GoFeatureFlagProviderOptions) {
67+
// logger is the Open Feature logger to use
68+
private logger?: Logger;
69+
70+
constructor(options: GoFeatureFlagProviderOptions, logger?: Logger) {
6771
this.timeout = options.timeout || 0; // default is 0 = no timeout
6872
this.endpoint = options.endpoint;
6973
this.cacheTTL = options.flagCacheTTL !== undefined && options.flagCacheTTL !== 0 ? options.flagCacheTTL : 1000 * 60;
7074
this.dataFlushInterval = options.dataFlushInterval || 1000 * 60;
7175
this.disableDataCollection = options.disableDataCollection || false;
76+
this.logger = logger;
7277

7378
// Add API key to the headers
7479
if (options.apiKey) {
@@ -131,7 +136,7 @@ export class GoFeatureFlagProvider implements Provider {
131136
timeout: this.timeout,
132137
});
133138
} catch (e) {
134-
// TODO : add a log here
139+
this.logger?.error(`impossible to send the data to the collector: ${e}`)
135140
// if we have an issue calling the collector we put the data back in the buffer
136141
this.dataCollectorBuffer = [...this.dataCollectorBuffer, ...dataToSend]
137142
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default class TestLogger {
2+
public inMemoryLogger: Record<string, string[]> = {
3+
error: [],
4+
warn: [],
5+
info: [],
6+
debug: [],
7+
};
8+
9+
error(...args: unknown[]): void {
10+
this.inMemoryLogger['error'].push(args.join(' '));
11+
}
12+
13+
warn(...args: unknown[]): void {
14+
this.inMemoryLogger['warn'].push(args.join(' '));
15+
}
16+
17+
info(...args: unknown[]): void {
18+
this.inMemoryLogger['info'].push(args.join(' '));
19+
}
20+
21+
debug(...args: unknown[]): void {
22+
this.inMemoryLogger['debug'].push(args.join(' '));
23+
}
24+
25+
reset() {
26+
this.inMemoryLogger = {
27+
error: [],
28+
warn: [],
29+
info: [],
30+
debug: [],
31+
};
32+
}
33+
}

0 commit comments

Comments
 (0)