-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(flags): add Statsig browser integration #15319
Open
aliu39
wants to merge
4
commits into
develop
Choose a base branch
from
aliu/statsig
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...packages/browser-integration-tests/suites/integrations/featureFlags/statsig/basic/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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 = (window as any).statsigClient; | ||
for (let i = 1; i <= bufferSize; i++) { | ||
client.checkGate(`feat${i}`, false); | ||
} | ||
client.checkGate(`feat${bufferSize + 1}`, true); // eviction | ||
client.checkGate('feat3', true); // update | ||
}, FLAG_BUFFER_SIZE); | ||
|
||
const reqPromise = waitForErrorRequest(page); | ||
await page.locator('#error').click(); | ||
const req = await reqPromise; | ||
const event = envelopeRequestParser(req); | ||
|
||
const expectedFlags = [{ flag: 'feat2', result: false }]; | ||
for (let i = 4; i <= FLAG_BUFFER_SIZE; i++) { | ||
expectedFlags.push({ flag: `feat${i}`, result: false }); | ||
} | ||
expectedFlags.push({ flag: `feat${FLAG_BUFFER_SIZE + 1}`, result: true }); | ||
expectedFlags.push({ flag: 'feat3', result: true }); | ||
|
||
expect(event.contexts?.flags?.values).toEqual(expectedFlags); | ||
}); |
30 changes: 30 additions & 0 deletions
30
dev-packages/browser-integration-tests/suites/integrations/featureFlags/statsig/init.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
class MockStatsigClient { | ||
constructor() { | ||
this._gateEvaluationListeners = []; | ||
} | ||
|
||
on(event, listener) { | ||
this._gateEvaluationListeners.push(listener); | ||
} | ||
|
||
checkGate(name, defaultVal) { | ||
// Note the actual StatsigClient.checkGate does not take a defaultVal. | ||
this._gateEvaluationListeners.forEach(listener => { | ||
listener({ gate: { name, value: defaultVal } }); | ||
}); | ||
return defaultVal; | ||
} | ||
} | ||
|
||
window.statsigClient = new MockStatsigClient(); | ||
|
||
window.Sentry = Sentry; | ||
window.sentryStatsigIntegration = Sentry.statsigIntegration({ statsigClient: window.statsigClient }); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
sampleRate: 1.0, | ||
integrations: [window.sentryStatsigIntegration], | ||
}); |
3 changes: 3 additions & 0 deletions
3
dev-packages/browser-integration-tests/suites/integrations/featureFlags/statsig/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
document.getElementById('error').addEventListener('click', () => { | ||
throw new Error('Button triggered error'); | ||
}); |
9 changes: 9 additions & 0 deletions
9
...packages/browser-integration-tests/suites/integrations/featureFlags/statsig/template.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
</head> | ||
<body> | ||
<button id="error">Throw Error</button> | ||
</body> | ||
</html> |
65 changes: 65 additions & 0 deletions
65
...ages/browser-integration-tests/suites/integrations/featureFlags/statsig/withScope/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?.isForked === true); | ||
const mainReqPromise = waitForErrorRequest(page, event => !!event.tags?.isForked === false); | ||
|
||
await page.evaluate(() => { | ||
const Sentry = (window as any).Sentry; | ||
const errorButton = document.querySelector('#error') as HTMLButtonElement; | ||
const client = (window as any).statsigClient; | ||
|
||
client.checkGate('shared', true); | ||
|
||
Sentry.withScope((scope: Scope) => { | ||
client.checkGate('forked', true); | ||
client.checkGate('shared', false); | ||
scope.setTag('isForked', true); | ||
if (errorButton) { | ||
errorButton.click(); | ||
} | ||
}); | ||
|
||
client.checkGate('main', true); | ||
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: 'forked', result: true }, | ||
{ flag: 'shared', result: false }, | ||
]); | ||
|
||
expect(mainEvent.contexts?.flags?.values).toEqual([ | ||
{ flag: 'shared', result: true }, | ||
{ flag: 'main', result: true }, | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/browser/src/integrations/featureFlags/statsig/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { statsigIntegration } from './integration'; |
44 changes: 44 additions & 0 deletions
44
packages/browser/src/integrations/featureFlags/statsig/integration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Client, Event, EventHint, IntegrationFn } from '@sentry/core'; | ||
|
||
import { defineIntegration } from '@sentry/core'; | ||
import { copyFlagsFromScopeToEvent, insertFlagToScope } from '../../../utils/featureFlags'; | ||
import type { FeatureGate, StatsigClient } from './types'; | ||
|
||
/** | ||
* Sentry integration for capturing feature flag evaluations from the Statsig js-client SDK. | ||
* | ||
* See the [feature flag documentation](https://develop.sentry.dev/sdk/expected-features/#feature-flags) for more information. | ||
* | ||
* @example | ||
* ``` | ||
* import { StatsigClient } from ''; | ||
* import * as Sentry from '@sentry/browser'; | ||
* | ||
* const statsigClient = new StatsigClient(); | ||
* | ||
* Sentry.init({ | ||
* dsn: '___PUBLIC_DSN___', | ||
* integrations: [Sentry.statsigIntegration({statsigClient})], | ||
* }); | ||
* | ||
* await statsigClient.initializeAsync(); // or statsigClient.initializeSync(); | ||
* | ||
* const result = statsigClient.checkGate('my-feature-gate'); | ||
* Sentry.captureException(new Error('something went wrong')); | ||
* ``` | ||
*/ | ||
export const statsigIntegration = defineIntegration(({ statsigClient }: { statsigClient: StatsigClient }) => { | ||
return { | ||
name: 'Statsig', | ||
|
||
processEvent(event: Event, _hint: EventHint, _client: Client): Event { | ||
return copyFlagsFromScopeToEvent(event); | ||
}, | ||
|
||
setup() { | ||
statsigClient.on('gate_evaluation', (event: { gate: FeatureGate }) => { | ||
insertFlagToScope(event.gate.name, event.gate.value); | ||
}); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; |
15 changes: 15 additions & 0 deletions
15
packages/browser/src/integrations/featureFlags/statsig/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export type FeatureGate = { | ||
readonly name: string; | ||
readonly value: boolean; | ||
}; | ||
|
||
type EventNameToEventDataMap = { | ||
gate_evaluation: { gate: FeatureGate }; | ||
Comment on lines
+6
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}; | ||
|
||
export interface StatsigClient { | ||
on( | ||
event: keyof EventNameToEventDataMap, | ||
callback: (data: EventNameToEventDataMap[keyof EventNameToEventDataMap]) => void, | ||
): void; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excluded props:
// readonly ruleID: string;
// readonly details: EvaluationDetails;
// readonly __evaluation: GateEvaluation | null;