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

ref(flags/v8): rename unleash integration param #15345

Open
wants to merge 2 commits into
base: v8
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ window.UnleashClient = class {
};

window.Sentry = Sentry;
window.sentryUnleashIntegration = Sentry.unleashIntegration({ unleashClientClass: window.UnleashClient });
window.sentryUnleashIntegration = Sentry.unleashIntegration({ featureFlagClientClass: window.UnleashClient });

Sentry.init({
dsn: 'https://[email protected]/1337',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Sentry from '@sentry/browser';

window.UnleashClient = class {
isEnabled(x) {
return x;
}
};

window.Sentry = Sentry;
window.sentryUnleashIntegration = Sentry.unleashIntegration({ unleashClientClass: window.UnleashClient });

Sentry.init({
dsn: 'https://[email protected]/1337',
sampleRate: 1.0,
integrations: [window.sentryUnleashIntegration],
debug: true, // Required to test logging.
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../../utils/fixtures';

import { shouldSkipFeatureFlagsTest } from '../../../../../utils/helpers';

sentryTest('Logs and returns if isEnabled does not match expected signature', async ({ getLocalTestUrl, page }) => {
if (shouldSkipFeatureFlagsTest()) {
sentryTest.skip();
}
const bundleKey = process.env.PW_BUNDLE || '';
const hasDebug = !bundleKey.includes('_min');

await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true });
await page.goto(url);

const errorLogs: string[] = [];
page.on('console', msg => {
if (msg.type() == 'error') {
errorLogs.push(msg.text());
}
});

const results = await page.evaluate(() => {
const unleash = new (window as any).UnleashClient();
const res1 = unleash.isEnabled('my-feature');
const res2 = unleash.isEnabled(999);
const res3 = unleash.isEnabled({});
return [res1, res2, res3];
});

// Test that the expected results are still returned. Note isEnabled is identity function for this test.
expect(results).toEqual(['my-feature', 999, {}]);

// Expected error logs.
if (hasDebug) {
expect(errorLogs).toEqual(
expect.arrayContaining([
expect.stringContaining(
'[Feature Flags] UnleashClient.isEnabled does not match expected signature. arg0: my-feature (string), result: my-feature (string)',
),
expect.stringContaining(
'[Feature Flags] UnleashClient.isEnabled does not match expected signature. arg0: 999 (number), result: 999 (number)',
),
expect.stringContaining(
'[Feature Flags] UnleashClient.isEnabled does not match expected signature. arg0: [object Object] (object), result: [object Object] (object)',
),
]),
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as Sentry from '@sentry/browser';

window.UnleashClient = class {
constructor() {
this._featureToVariant = {
strFeat: { name: 'variant1', enabled: true, feature_enabled: true, payload: { type: 'string', value: 'test' } },
noPayloadFeat: { name: 'eu-west', enabled: true, feature_enabled: true },
jsonFeat: {
name: 'paid-orgs',
enabled: true,
feature_enabled: true,
payload: {
type: 'json',
value: '{"foo": {"bar": "baz"}, "hello": [1, 2, 3]}',
},
},

// Enabled feature with no configured variants.
noVariantFeat: { name: 'disabled', enabled: false, feature_enabled: true },

// Disabled feature.
disabledFeat: { name: 'disabled', enabled: false, feature_enabled: false },
};

// Variant returned for features that don't exist.
// `feature_enabled` may be defined in prod, but we want to test the undefined case.
this._fallbackVariant = {
name: 'disabled',
enabled: false,
};
}

isEnabled(toggleName) {
const variant = this._featureToVariant[toggleName] || this._fallbackVariant;
return variant.feature_enabled || false;
}

getVariant(toggleName) {
return this._featureToVariant[toggleName] || this._fallbackVariant;
}
};

window.Sentry = Sentry;
window.sentryUnleashIntegration = Sentry.unleashIntegration({ unleashClientClass: window.UnleashClient });

Sentry.init({
dsn: 'https://[email protected]/1337',
sampleRate: 1.0,
integrations: [window.sentryUnleashIntegration],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../../utils/fixtures';

import { envelopeRequestParser, shouldSkipFeatureFlagsTest, waitForErrorRequest } from '../../../../../utils/helpers';

const FLAG_BUFFER_SIZE = 100; // Corresponds to constant in featureFlags.ts, in browser utils.

sentryTest('Basic test with eviction, update, and no async tasks', async ({ getLocalTestUrl, page }) => {
if (shouldSkipFeatureFlagsTest()) {
sentryTest.skip();
}

await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true });
await page.goto(url);

await page.evaluate(bufferSize => {
const client = new (window as any).UnleashClient();

client.isEnabled('feat1');
client.isEnabled('strFeat');
client.isEnabled('noPayloadFeat');
client.isEnabled('jsonFeat');
client.isEnabled('noVariantFeat');
client.isEnabled('disabledFeat');

for (let i = 7; i <= bufferSize; i++) {
client.isEnabled(`feat${i}`);
}
client.isEnabled(`feat${bufferSize + 1}`); // eviction
client.isEnabled('noPayloadFeat'); // update (move to tail)
}, FLAG_BUFFER_SIZE);

const reqPromise = waitForErrorRequest(page);
await page.locator('#error').click();
const req = await reqPromise;
const event = envelopeRequestParser(req);

const expectedFlags = [{ flag: 'strFeat', result: true }];
expectedFlags.push({ flag: 'jsonFeat', result: true });
expectedFlags.push({ flag: 'noVariantFeat', result: true });
expectedFlags.push({ flag: 'disabledFeat', result: false });
for (let i = 7; i <= FLAG_BUFFER_SIZE; i++) {
expectedFlags.push({ flag: `feat${i}`, result: false });
}
expectedFlags.push({ flag: `feat${FLAG_BUFFER_SIZE + 1}`, result: false });
expectedFlags.push({ flag: 'noPayloadFeat', result: true });

expect(event.contexts?.flags?.values).toEqual(expectedFlags);
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ window.UnleashClient = class {
};

window.Sentry = Sentry;
window.sentryUnleashIntegration = Sentry.unleashIntegration({ unleashClientClass: window.UnleashClient });
window.sentryUnleashIntegration = Sentry.unleashIntegration({ featureFlagClientClass: window.UnleashClient });

Sentry.init({
dsn: 'https://[email protected]/1337',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as Sentry from '@sentry/browser';

window.UnleashClient = class {
constructor() {
this._featureToVariant = {
strFeat: { name: 'variant1', enabled: true, feature_enabled: true, payload: { type: 'string', value: 'test' } },
noPayloadFeat: { name: 'eu-west', enabled: true, feature_enabled: true },
jsonFeat: {
name: 'paid-orgs',
enabled: true,
feature_enabled: true,
payload: {
type: 'json',
value: '{"foo": {"bar": "baz"}, "hello": [1, 2, 3]}',
},
},

// Enabled feature with no configured variants.
noVariantFeat: { name: 'disabled', enabled: false, feature_enabled: true },

// Disabled feature.
disabledFeat: { name: 'disabled', enabled: false, feature_enabled: false },
};

// Variant returned for features that don't exist.
// `feature_enabled` may be defined in prod, but we want to test the undefined case.
this._fallbackVariant = {
name: 'disabled',
enabled: false,
};
}

isEnabled(toggleName) {
const variant = this._featureToVariant[toggleName] || this._fallbackVariant;
return variant.feature_enabled || false;
}

getVariant(toggleName) {
return this._featureToVariant[toggleName] || this._fallbackVariant;
}
};

window.Sentry = Sentry;
window.sentryUnleashIntegration = Sentry.unleashIntegration({ unleashClientClass: window.UnleashClient });

Sentry.init({
dsn: 'https://[email protected]/1337',
sampleRate: 1.0,
integrations: [window.sentryUnleashIntegration],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../../utils/fixtures';

import { envelopeRequestParser, shouldSkipFeatureFlagsTest, waitForErrorRequest } from '../../../../../utils/helpers';

import type { Scope } from '@sentry/browser';

sentryTest('Flag evaluations in forked scopes are stored separately.', async ({ getLocalTestUrl, page }) => {
if (shouldSkipFeatureFlagsTest()) {
sentryTest.skip();
}

await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true });
await page.goto(url);

const forkedReqPromise = waitForErrorRequest(page, event => !!event.tags && event.tags.isForked === true);
const mainReqPromise = waitForErrorRequest(page, event => !!event.tags && event.tags.isForked === false);

await page.evaluate(() => {
const Sentry = (window as any).Sentry;
const errorButton = document.querySelector('#error') as HTMLButtonElement;
const unleash = new (window as any).UnleashClient();

unleash.isEnabled('strFeat');

Sentry.withScope((scope: Scope) => {
unleash.isEnabled('disabledFeat');
unleash.isEnabled('strFeat');
scope.setTag('isForked', true);
if (errorButton) {
errorButton.click();
}
});

unleash.isEnabled('noPayloadFeat');
Sentry.getCurrentScope().setTag('isForked', false);
errorButton.click();
return true;
});

const forkedReq = await forkedReqPromise;
const forkedEvent = envelopeRequestParser(forkedReq);

const mainReq = await mainReqPromise;
const mainEvent = envelopeRequestParser(mainReq);

expect(forkedEvent.contexts?.flags?.values).toEqual([
{ flag: 'disabledFeat', result: false },
{ flag: 'strFeat', result: true },
]);

expect(mainEvent.contexts?.flags?.values).toEqual([
{ flag: 'strFeat', result: true },
{ flag: 'noPayloadFeat', result: true },
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { DEBUG_BUILD } from '../../../debug-build';
import { copyFlagsFromScopeToEvent, insertFlagToScope } from '../../../utils/featureFlags';
import type { UnleashClient, UnleashClientClass } from './types';

type UnleashIntegrationOptions = {
featureFlagClientClass?: UnleashClientClass;

/**
* @deprecated Use `featureFlagClientClass` instead.
*/
unleashClientClass?: UnleashClientClass;
};

/**
* Sentry integration for capturing feature flag evaluations from the Unleash SDK.
*
Expand All @@ -17,19 +26,24 @@ import type { UnleashClient, UnleashClientClass } from './types';
*
* Sentry.init({
* dsn: '___PUBLIC_DSN___',
* integrations: [Sentry.unleashIntegration({unleashClientClass: UnleashClient})],
* integrations: [Sentry.unleashIntegration({featureFlagClientClass: UnleashClient})],
* });
*
* const unleash = new UnleashClient(...);
* unleash.start();
*
* unleash.isEnabled('my-feature');
* unleash.getVariant('other-feature');
* Sentry.captureException(new Error('something went wrong'));
* ```
*/
export const unleashIntegration = defineIntegration(
({ unleashClientClass }: { unleashClientClass: UnleashClientClass }) => {
// eslint-disable-next-line deprecation/deprecation
({ featureFlagClientClass, unleashClientClass }: UnleashIntegrationOptions) => {
const _unleashClientClass = featureFlagClientClass ?? unleashClientClass;
if (!_unleashClientClass) {
throw new Error('featureFlagClientClass option is required');
}

return {
name: 'Unleash',

Expand All @@ -38,7 +52,7 @@ export const unleashIntegration = defineIntegration(
},

setupOnce() {
const unleashClientPrototype = unleashClientClass.prototype as UnleashClient;
const unleashClientPrototype = _unleashClientClass.prototype as UnleashClient;
fill(unleashClientPrototype, 'isEnabled', _wrappedIsEnabled);
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { unleashIntegration } from '../../../src';

describe('Unleash', () => {
it('Throws error if given empty options', () => {
expect(() => unleashIntegration({})).toThrow('featureFlagClientClass option is required');
});
});
Loading