From d92ab4bca657cd907717cf473bb1ea8dd8c4d6f2 Mon Sep 17 00:00:00 2001 From: teodosii Date: Thu, 22 Dec 2022 00:02:11 +0200 Subject: [PATCH] make api key optional to follow appo11y config convention --- grafana-plugin/src/utils/faro.test.ts | 33 +++++++++++++++------------ grafana-plugin/src/utils/faro.ts | 12 +++++++--- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/grafana-plugin/src/utils/faro.test.ts b/grafana-plugin/src/utils/faro.test.ts index 7a1b3fca..6a3c0553 100644 --- a/grafana-plugin/src/utils/faro.test.ts +++ b/grafana-plugin/src/utils/faro.test.ts @@ -30,20 +30,23 @@ describe('Faro', () => { beforeEach(() => { jest.resetModules(); process.env = { ...OLD_ENV }; + FaroHelper.faro = undefined; + jest.clearAllMocks(); }); - afterAll(() => { - process.env = OLD_ENV; + const getDefaultValues = () => ({ + faroUrl: 'localhost:12345/collect', + apiKey: 'secret', + enabled: 'true', }); - const getProcessEnv = ( - config: { faroUrl?: string; apiKey?: string; enabled?: string } = { - faroUrl: 'localhost:12345/collect', - apiKey: 'secret', - enabled: 'true', - } - ) => { - const { faroUrl, apiKey, enabled } = config; + const getProcessEnv = (config: { faroUrl?: string; apiKey?: string; enabled?: string } = {}) => { + const configObject = { + ...getDefaultValues(), + ...config, + }; + + const { faroUrl, apiKey, enabled } = configObject; return { FARO_URL: faroUrl, @@ -52,6 +55,12 @@ describe('Faro', () => { }; }; + test('It initializes without api key', () => { + process.env = getProcessEnv({ apiKey: '' }); + const faro = FaroHelper.initializeFaro(); + expect(faro).toBeDefined(); + }); + test('It initializes faro ENABLED === true', () => { process.env = getProcessEnv(); const faro = FaroHelper.initializeFaro(); @@ -73,10 +82,6 @@ describe('Faro', () => { faro = FaroHelper.initializeFaro(); expect(faro).toBeUndefined(); - process.env = getProcessEnv({ apiKey: undefined }); - faro = FaroHelper.initializeFaro(); - expect(faro).toBeUndefined(); - process.env = getProcessEnv({ enabled: undefined }); faro = FaroHelper.initializeFaro(); expect(faro).toBeUndefined(); diff --git a/grafana-plugin/src/utils/faro.ts b/grafana-plugin/src/utils/faro.ts index 7bbf64ac..15d8883f 100644 --- a/grafana-plugin/src/utils/faro.ts +++ b/grafana-plugin/src/utils/faro.ts @@ -32,12 +32,12 @@ class FaroHelper { environment: FARO_ENV ? `${ONCALL} ${FARO_ENV}` : ONCALL, }; - if (!faroConfig?.enabled || !faroConfig?.url || !faroConfig?.apiKey || this.faro) { + if (!faroConfig?.enabled || !faroConfig?.url || this.faro) { return undefined; } try { - this.faro = initializeFaro({ + const faroOptions = { url: faroConfig.url, apiKey: faroConfig.apiKey, isolate: true, @@ -59,7 +59,13 @@ class FaroHelper { name: faroConfig.environment, version: plugin?.version, }, - }); + }; + + if (!faroConfig.apiKey) { + delete faroOptions.apiKey; // appo11y has the key in the API instead + } + + this.faro = initializeFaro(faroOptions); this.faro.api.pushLog([`Faro was initialized for ${faroConfig.environment}`]); } catch (ex) {}