Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AXON-194] Allow easy override of feature flags and experiments #165

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ ATLASCODE_FX3_API_KEY="totally-real-api-key"
ATLASCODE_FX3_ENVIRONMENT="production"
ATLASCODE_FX3_TARGET_APP="atlascode_web"
ATLASCODE_FX3_TIMEOUT="2000"

ATLASCODE_FF_OVERRIDES="feature_flag=true"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really appreciate a comment here about comma being the expected delimiter. Or, maybe, something like f1=true,f2=false in the example value?


ATLASCODE_EXP_OVERRIDES_BOOL="exp1_name=true"
ATLASCODE_EXP_OVERRIDES_STRING="exp2_name=something"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline 😛

45 changes: 38 additions & 7 deletions src/util/featureFlags/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// tests for client.ts
enum MockFeatures {
TestFeature = 'some-very-real-feature',
}
Expand All @@ -7,10 +6,10 @@ enum MockExperiments {
TestExperiment = 'some-very-real-experiment',
}

const MockExperimentGates = {
const MockExperimentGates: Record<string, any> = {
[MockExperiments.TestExperiment]: {
parameter: 'isEnabled',
defaultValue: false,
defaultValue: 'a default value',
},
};

Expand All @@ -27,22 +26,26 @@ jest.mock('@atlaskit/feature-gate-js-client', () => {
...jest.requireActual('@atlaskit/feature-gate-js-client'),
default: {
initialize: jest.fn(() => Promise.resolve()),
checkGate: jest.fn(() => Promise.resolve(false)),
getExperimentValue: jest.fn(() => Promise.resolve(false)),
checkGate: jest.fn(() => false),
getExperimentValue: jest.fn((key) => MockExperimentGates[key].defaultValue),
},
};
});

import FeatureGates from '@atlaskit/feature-gate-js-client';
import { FeatureFlagClient, FeatureFlagClientOptions } from './client';
import { EventBuilderInterface } from './analytics';
import { Experiments, Features } from './features';

class MockEventBuilder implements EventBuilderInterface {
public featureFlagClientInitializedEvent = jest.fn(() => Promise.resolve({}));
public featureFlagClientInitializationFailedEvent = jest.fn(() => Promise.resolve({}));
}

describe('FeatureFlagClient', () => {
const featureName = MockFeatures.TestFeature as unknown as Features;
const experimentName = MockExperiments.TestExperiment as unknown as Experiments;

let analyticsClient: any;
let options: FeatureFlagClientOptions;
const originalEnv = process.env;
Expand Down Expand Up @@ -76,14 +79,42 @@ describe('FeatureFlagClient', () => {
it('should initialize the feature flag client', async () => {
await FeatureFlagClient.initialize(options);
expect(FeatureGates.initialize).toHaveBeenCalled();
expect(FeatureGates.checkGate).toHaveBeenCalled();
expect(FeatureGates.getExperimentValue).toHaveBeenCalled();
});

it('should catch an error when the feature flag client fails to initialize', async () => {
FeatureGates.initialize = jest.fn(() => Promise.reject('error'));
await FeatureFlagClient.initialize(options);
expect(FeatureGates.initialize).toHaveBeenCalled();
});

it('feature flags default values are correctly assigned', async () => {
await FeatureFlagClient.initialize(options);
expect(FeatureGates.checkGate).toHaveBeenCalled();
expect(Object.keys(FeatureFlagClient.featureGates).length).toBe(1);
expect(FeatureFlagClient.featureGates[featureName]).toBeDefined();
expect(FeatureFlagClient.featureGates[featureName]).toBe(false);
});

it('feature flags overrides are correctly applied', async () => {
process.env.ATLASCODE_FF_OVERRIDES = `${featureName}=true`;
await FeatureFlagClient.initialize(options);
expect(FeatureFlagClient.featureGates[featureName]).toBeDefined();
expect(FeatureFlagClient.featureGates[featureName]).toBe(true);
});

it('experiments default values are correctly assigned', async () => {
await FeatureFlagClient.initialize(options);
expect(FeatureGates.getExperimentValue).toHaveBeenCalled();
expect(Object.keys(FeatureFlagClient.experimentValues).length).toBe(1);
expect(FeatureFlagClient.experimentValues[experimentName]).toBeDefined();
expect(FeatureFlagClient.experimentValues[experimentName]).toBe('a default value');
});

it('experiments overrides are correctly applied', async () => {
process.env.ATLASCODE_EXP_OVERRIDES_STRING = `${experimentName}=another value`;
await FeatureFlagClient.initialize(options);
expect(FeatureFlagClient.experimentValues[experimentName]).toBeDefined();
expect(FeatureFlagClient.experimentValues[experimentName]).toBe('another value');
});
});
});
94 changes: 69 additions & 25 deletions src/util/featureFlags/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AnalyticsClient } from '../../analytics-node-client/src/client.min';

import FeatureGates, { FeatureGateEnvironment, Identifiers } from '@atlaskit/feature-gate-js-client';
import { AnalyticsClient } from '../../analytics-node-client/src/client.min';
import { AnalyticsClientMapper, EventBuilderInterface } from './analytics';
import { ExperimentGates, ExperimentGateValues, Experiments, FeatureGateValues, Features } from './features';

Expand All @@ -13,6 +12,7 @@ export type FeatureFlagClientOptions = {
export class FeatureFlagClient {
private static analyticsClient: AnalyticsClientMapper;
private static eventBuilder: EventBuilderInterface;

private static _featureGates: FeatureGateValues;
static get featureGates(): FeatureGateValues {
return this._featureGates;
Expand All @@ -30,8 +30,7 @@ export class FeatureFlagClient {
const timeout = process.env.ATLASCODE_FX3_TIMEOUT;

if (!targetApp || !environment || !apiKey || !timeout) {
this._featureGates = this.evaluateFeatures();
this._experimentValues = this.evaluateExperiments();
this.finalizeInit();
return;
}

Expand All @@ -54,11 +53,6 @@ export class FeatureFlagClient {
this.eventBuilder.featureFlagClientInitializedEvent().then((e) => {
options.analyticsClient.sendTrackEvent(e);
});

// console log all feature gates and values
for (const feat of Object.values(Features)) {
console.log(`FeatureGates: ${feat} -> ${FeatureGates.checkGate(feat)}`);
}
})
.catch((err) => {
console.warn(`FeatureGates: Failed to initialize client. ${err}`);
Expand All @@ -67,11 +61,69 @@ export class FeatureFlagClient {
.then((e) => options.analyticsClient.sendTrackEvent(e));
})
.finally(() => {
this._featureGates = this.evaluateFeatures();
this._experimentValues = this.evaluateExperiments();
this.finalizeInit();

// console log all feature gates and values
for (const feat of Object.values(Features)) {
console.log(`FeatureGates: ${feat} -> ${FeatureGates.checkGate(feat)}`);
}
});
}

private static finalizeInit(): void {
this._featureGates = this.evaluateFeatures();
this._experimentValues = this.evaluateExperiments();

const ffSplit = (process.env.ATLASCODE_FF_OVERRIDES || '')
.split(',')
.map(this.parseBoolOverride<Features>)
.filter((x) => !!x);
for (const { key, value } of ffSplit) {
this._featureGates[key] = value;
}

const boolExpSplit = (process.env.ATLASCODE_EXP_OVERRIDES_BOOL || '')
.split(',')
.map(this.parseBoolOverride<Experiments>)
.filter((x) => !!x);
for (const { key, value } of boolExpSplit) {
this._experimentValues[key] = value;
}

const strExpSplit = (process.env.ATLASCODE_EXP_OVERRIDES_STRING || '')
.split(',')
.map(this.parseStringOverride)
.filter((x) => !!x);
for (const { key, value } of strExpSplit) {
this._experimentValues[key] = value;
}
}

private static parseBoolOverride<T>(setting: string): { key: T; value: boolean } | undefined {
const [key, valueRaw] = setting
.trim()
.split('=', 2)
.map((x) => x.trim());
if (key) {
const value = valueRaw.toLowerCase() === 'true';
return { key: key as T, value };
} else {
return undefined;
}
}

private static parseStringOverride(setting: string): { key: Experiments; value: string } | undefined {
const [key, value] = setting
.trim()
.split('=', 2)
.map((x) => x.trim());
if (key) {
return { key: key as Experiments, value };
} else {
return undefined;
}
}

private static checkGate(gate: Features): boolean {
let gateValue = false;
if (!FeatureGates) {
Expand Down Expand Up @@ -105,23 +157,15 @@ export class FeatureFlagClient {
}

private static evaluateFeatures(): FeatureGateValues {
const featureFlags = Object.values(Features).map(async (feature) => {
return {
[feature]: this.checkGate(feature),
};
});

return featureFlags.reduce((acc, val) => ({ ...acc, ...val }), {}) as FeatureGateValues;
const featureFlags = {} as FeatureGateValues;
Object.values(Features).forEach((feature) => (featureFlags[feature] = this.checkGate(feature)));
return featureFlags;
}

private static evaluateExperiments(): ExperimentGateValues {
const experimentGates = Object.values(Experiments).map(async (experiment) => {
return {
[experiment]: this.checkExperimentValue(experiment),
};
});

return experimentGates.reduce((acc, val) => ({ ...acc, ...val }), {}) as ExperimentGateValues;
const experimentGates = {} as ExperimentGateValues;
Object.values(Experiments).forEach((exp) => (experimentGates[exp] = this.checkExperimentValue(exp)));
return experimentGates;
}

static dispose() {
Expand Down
4 changes: 1 addition & 3 deletions src/util/featureFlags/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum Experiments {
AtlascodeAA = 'atlascode_aa_experiment',
}

export const ExperimentGates: ExperimentGate = {
export const ExperimentGates: Record<Experiments, ExperimentPayload> = {
[Experiments.NewAuthUI]: {
parameter: 'isEnabled',
defaultValue: false,
Expand All @@ -20,8 +20,6 @@ export const ExperimentGates: ExperimentGate = {
};

type ExperimentPayload = { parameter: string; defaultValue: any };
type ExperimentGate = Record<Experiments, ExperimentPayload>;

export type FeatureGateValues = Record<Features, boolean>;

export type ExperimentGateValues = Record<Experiments, any>;
4 changes: 2 additions & 2 deletions src/util/featureFlags/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FeatureFlagClient } from './client';
import { Features } from './features';
import { Features, Experiments } from './features';

export { FeatureFlagClient, Features };
export { FeatureFlagClient, Features, Experiments };
3 changes: 3 additions & 0 deletions webpack.extension.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ module.exports = [
'process.env.ATLASCODE_FX3_TARGET_APP': JSON.stringify(process.env.ATLASCODE_FX3_TARGET_APP),
'process.env.ATLASCODE_FX3_TIMEOUT': JSON.stringify(process.env.ATLASCODE_FX3_TIMEOUT),
'process.env.ATLASCODE_TEST_USER_API_TOKEN': JSON.stringify(process.env.ATLASCODE_TEST_USER_API_TOKEN),
'process.env.ATLASCODE_FF_OVERRIDES': JSON.stringify(process.env.ATLASCODE_FF_OVERRIDES),
'process.env.ATLASCODE_EXP_OVERRIDES_BOOL': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_BOOL),
'process.env.ATLASCODE_EXP_OVERRIDES_STRING': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_STRING),
'process.env.CI': JSON.stringify(process.env.CI),
}),
],
Expand Down
3 changes: 3 additions & 0 deletions webpack.extension.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ module.exports = [
'process.env.ATLASCODE_FX3_TARGET_APP': JSON.stringify(process.env.ATLASCODE_FX3_TARGET_APP),
'process.env.ATLASCODE_FX3_TIMEOUT': JSON.stringify(process.env.ATLASCODE_FX3_TIMEOUT),
'process.env.ATLASCODE_TEST_USER_API_TOKEN': JSON.stringify(process.env.ATLASCODE_TEST_USER_API_TOKEN),
'process.env.ATLASCODE_FF_OVERRIDES': JSON.stringify(process.env.ATLASCODE_FF_OVERRIDES),
'process.env.ATLASCODE_EXP_OVERRIDES_BOOL': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_BOOL),
'process.env.ATLASCODE_EXP_OVERRIDES_STRING': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_STRING),
'process.env.CI': JSON.stringify(process.env.CI),
}),
],
Expand Down
3 changes: 3 additions & 0 deletions webpack.mui.webview.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ module.exports = {
'process.env.ATLASCODE_FX3_TARGET_APP': JSON.stringify(process.env.ATLASCODE_FX3_TARGET_APP),
'process.env.ATLASCODE_FX3_TIMEOUT': JSON.stringify(process.env.ATLASCODE_FX3_TIMEOUT),
'process.env.ATLASCODE_TEST_USER_API_TOKEN': JSON.stringify(process.env.ATLASCODE_TEST_USER_API_TOKEN),
'process.env.ATLASCODE_FF_OVERRIDES': JSON.stringify(process.env.ATLASCODE_FF_OVERRIDES),
'process.env.ATLASCODE_EXP_OVERRIDES_BOOL': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_BOOL),
'process.env.ATLASCODE_EXP_OVERRIDES_STRING': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_STRING),
'process.env.CI': JSON.stringify(process.env.CI),
}),
],
Expand Down
3 changes: 3 additions & 0 deletions webpack.react.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ module.exports = {
'process.env.ATLASCODE_FX3_TARGET_APP': JSON.stringify(process.env.ATLASCODE_FX3_TARGET_APP),
'process.env.ATLASCODE_FX3_TIMEOUT': JSON.stringify(process.env.ATLASCODE_FX3_TIMEOUT),
'process.env.ATLASCODE_TEST_USER_API_TOKEN': JSON.stringify(process.env.ATLASCODE_TEST_USER_API_TOKEN),
'process.env.ATLASCODE_FF_OVERRIDES': JSON.stringify(process.env.ATLASCODE_FF_OVERRIDES),
'process.env.ATLASCODE_EXP_OVERRIDES_BOOL': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_BOOL),
'process.env.ATLASCODE_EXP_OVERRIDES_STRING': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_STRING),
'process.env.CI': JSON.stringify(process.env.CI),
}),
],
Expand Down
3 changes: 3 additions & 0 deletions webpack.react.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ module.exports = {
'process.env.ATLASCODE_FX3_TARGET_APP': JSON.stringify(process.env.ATLASCODE_FX3_TARGET_APP),
'process.env.ATLASCODE_FX3_TIMEOUT': JSON.stringify(process.env.ATLASCODE_FX3_TIMEOUT),
'process.env.ATLASCODE_TEST_USER_API_TOKEN': JSON.stringify(process.env.ATLASCODE_TEST_USER_API_TOKEN),
'process.env.ATLASCODE_FF_OVERRIDES': JSON.stringify(process.env.ATLASCODE_FF_OVERRIDES),
'process.env.ATLASCODE_EXP_OVERRIDES_BOOL': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_BOOL),
'process.env.ATLASCODE_EXP_OVERRIDES_STRING': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_STRING),
'process.env.CI': JSON.stringify(process.env.CI),
}),
],
Expand Down
3 changes: 3 additions & 0 deletions webpack.react.webview.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ module.exports = {
'process.env.ATLASCODE_FX3_TARGET_APP': JSON.stringify(process.env.ATLASCODE_FX3_TARGET_APP),
'process.env.ATLASCODE_FX3_TIMEOUT': JSON.stringify(process.env.ATLASCODE_FX3_TIMEOUT),
'process.env.ATLASCODE_TEST_USER_API_TOKEN': JSON.stringify(process.env.ATLASCODE_TEST_USER_API_TOKEN),
'process.env.ATLASCODE_FF_OVERRIDES': JSON.stringify(process.env.ATLASCODE_FF_OVERRIDES),
'process.env.ATLASCODE_EXP_OVERRIDES_BOOL': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_BOOL),
'process.env.ATLASCODE_EXP_OVERRIDES_STRING': JSON.stringify(process.env.ATLASCODE_EXP_OVERRIDES_STRING),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random thought, maybe for one of those Friday engineering excellence things - we might be at a point where it could be really good to extract some common webpack logic :D Not in scope of this PR though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, I guess, we've been at that point for quite a while now and just failed to act xD My bad :D

'process.env.CI': JSON.stringify(process.env.CI),
}),
],
Expand Down