-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(flags): Adds Growthbook integration #17440
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
Open
madhuchavva
wants to merge
9
commits into
getsentry:develop
Choose a base branch
from
madhuchavva:feat/growthbook-integration
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.
+400
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e6ce870
add growthbook integration
madhuchavva 7d2bec8
add handler
madhuchavva ff684eb
add handler, types and shim
madhuchavva b50ef20
capture evalFeature method
madhuchavva a6c3d73
add integration tests
madhuchavva 92ba49c
fix fixture imports
madhuchavva 0b38a7b
use withNestedSpans
madhuchavva 819b5cb
fill buffer and assert ordered list
madhuchavva e07bab4
Merge branch 'develop' into feat/growthbook-integration
madhuchavva 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
50 changes: 50 additions & 0 deletions
50
...owser-integration-tests/suites/integrations/featureFlags/growthbook/onError/basic/test.ts
This file contains hidden or 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,50 @@ | ||
import { expect } from '@playwright/test'; | ||
import { _INTERNAL_FLAG_BUFFER_SIZE as FLAG_BUFFER_SIZE } from '@sentry/core'; | ||
import { sentryTest } from '../../../../../../utils/fixtures'; | ||
import { | ||
envelopeRequestParser, | ||
shouldSkipFeatureFlagsTest, | ||
waitForErrorRequest, | ||
} from '../../../../../../utils/helpers'; | ||
|
||
sentryTest('GrowthBook onError: basic 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 gb = new (window as any).GrowthBook(); | ||
|
||
for (let i = 1; i <= bufferSize; i++) { | ||
gb.isOn(`feat${i}`); | ||
} | ||
|
||
gb.__setOn(`feat${bufferSize + 1}`, true); | ||
gb.isOn(`feat${bufferSize + 1}`); // eviction | ||
|
||
gb.__setOn('feat3', true); | ||
gb.isOn('feat3'); // update | ||
}, FLAG_BUFFER_SIZE); | ||
|
||
const reqPromise = waitForErrorRequest(page); | ||
await page.locator('#error').click(); | ||
const req = await reqPromise; | ||
const event = envelopeRequestParser(req); | ||
|
||
const values = event.contexts?.flags?.values || []; | ||
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(values).toEqual(expectedFlags); | ||
}); |
37 changes: 37 additions & 0 deletions
37
...ges/browser-integration-tests/suites/integrations/featureFlags/growthbook/onError/init.js
This file contains hidden or 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,37 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
// Minimal mock GrowthBook class for tests | ||
window.GrowthBook = class { | ||
constructor() { | ||
this._onFlags = Object.create(null); | ||
this._featureValues = Object.create(null); | ||
} | ||
|
||
isOn(featureKey) { | ||
return !!this._onFlags[featureKey]; | ||
} | ||
|
||
getFeatureValue(featureKey, defaultValue) { | ||
return Object.prototype.hasOwnProperty.call(this._featureValues, featureKey) | ||
? this._featureValues[featureKey] | ||
: defaultValue; | ||
} | ||
|
||
// Helpers for tests | ||
__setOn(featureKey, value) { | ||
this._onFlags[featureKey] = !!value; | ||
} | ||
|
||
__setFeatureValue(featureKey, value) { | ||
this._featureValues[featureKey] = value; | ||
} | ||
}; | ||
|
||
window.Sentry = Sentry; | ||
window.sentryGrowthBookIntegration = Sentry.growthbookIntegration({ growthbookClass: window.GrowthBook }); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
sampleRate: 1.0, | ||
integrations: [window.sentryGrowthBookIntegration], | ||
}); |
3 changes: 3 additions & 0 deletions
3
.../browser-integration-tests/suites/integrations/featureFlags/growthbook/onError/subject.js
This file contains hidden or 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'); | ||
}); |
12 changes: 12 additions & 0 deletions
12
...owser-integration-tests/suites/integrations/featureFlags/growthbook/onError/template.html
This file contains hidden or 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,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
</head> | ||
<body> | ||
<button id="error">Throw Error</button> | ||
</body> | ||
<script src="./subject.js"></script> | ||
</html> | ||
|
||
|
65 changes: 65 additions & 0 deletions
65
...r-integration-tests/suites/integrations/featureFlags/growthbook/onError/withScope/test.ts
This file contains hidden or 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 type { Scope } from '@sentry/browser'; | ||
import { sentryTest } from '../../../../../../utils/fixtures'; | ||
import { | ||
envelopeRequestParser, | ||
shouldSkipFeatureFlagsTest, | ||
waitForErrorRequest, | ||
} from '../../../../../../utils/helpers'; | ||
|
||
sentryTest('GrowthBook onError: forked scopes are isolated', 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 gb = new (window as any).GrowthBook(); | ||
|
||
gb.__setOn('shared', true); | ||
gb.__setOn('main', true); | ||
|
||
gb.isOn('shared'); | ||
|
||
Sentry.withScope((scope: Scope) => { | ||
gb.__setOn('forked', true); | ||
gb.__setOn('shared', false); | ||
gb.isOn('forked'); | ||
gb.isOn('shared'); | ||
scope.setTag('isForked', true); | ||
errorButton.click(); | ||
}); | ||
|
||
gb.isOn('main'); | ||
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 }, | ||
]); | ||
}); |
39 changes: 39 additions & 0 deletions
39
...ages/browser-integration-tests/suites/integrations/featureFlags/growthbook/onSpan/init.js
This file contains hidden or 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,39 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.GrowthBook = class { | ||
constructor() { | ||
this._onFlags = Object.create(null); | ||
this._featureValues = Object.create(null); | ||
} | ||
|
||
isOn(featureKey) { | ||
return !!this._onFlags[featureKey]; | ||
} | ||
|
||
getFeatureValue(featureKey, defaultValue) { | ||
return Object.prototype.hasOwnProperty.call(this._featureValues, featureKey) | ||
? this._featureValues[featureKey] | ||
: defaultValue; | ||
} | ||
|
||
__setOn(featureKey, value) { | ||
this._onFlags[featureKey] = !!value; | ||
} | ||
|
||
__setFeatureValue(featureKey, value) { | ||
this._featureValues[featureKey] = value; | ||
} | ||
}; | ||
|
||
window.Sentry = Sentry; | ||
window.sentryGrowthBookIntegration = Sentry.growthbookIntegration({ growthbookClass: window.GrowthBook }); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
sampleRate: 1.0, | ||
tracesSampleRate: 1.0, | ||
integrations: [ | ||
window.sentryGrowthBookIntegration, | ||
Sentry.browserTracingIntegration({ instrumentNavigation: false, instrumentPageLoad: false }), | ||
], | ||
}); |
16 changes: 16 additions & 0 deletions
16
...s/browser-integration-tests/suites/integrations/featureFlags/growthbook/onSpan/subject.js
This file contains hidden or 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,16 @@ | ||
const btnStartSpan = document.getElementById('btnStartSpan'); | ||
const btnEndSpan = document.getElementById('btnEndSpan'); | ||
const btnStartNestedSpan = document.getElementById('btnStartNestedSpan'); | ||
const btnEndNestedSpan = document.getElementById('btnEndNestedSpan'); | ||
|
||
window.withNestedSpans = callback => { | ||
window.Sentry.startSpan({ name: 'test-root-span' }, rootSpan => { | ||
window.traceId = rootSpan.spanContext().traceId; | ||
|
||
window.Sentry.startSpan({ name: 'test-span' }, _span => { | ||
window.Sentry.startSpan({ name: 'test-nested-span' }, _nestedSpan => { | ||
callback(); | ||
}); | ||
}); | ||
}); | ||
}; |
15 changes: 15 additions & 0 deletions
15
...rowser-integration-tests/suites/integrations/featureFlags/growthbook/onSpan/template.html
This file contains hidden or 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 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
</head> | ||
<body> | ||
<button id="btnStartSpan">Start Span</button> | ||
<button id="btnEndSpan">End Span</button> | ||
<button id="btnStartNestedSpan">Start Nested Span</button> | ||
<button id="btnEndNestedSpan">End Nested Span</button> | ||
</body> | ||
<script src="./subject.js"></script> | ||
</html> | ||
|
||
|
65 changes: 65 additions & 0 deletions
65
...ages/browser-integration-tests/suites/integrations/featureFlags/growthbook/onSpan/test.ts
This file contains hidden or 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 { _INTERNAL_MAX_FLAGS_PER_SPAN as MAX_FLAGS_PER_SPAN } from '@sentry/core'; | ||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { | ||
type EventAndTraceHeader, | ||
eventAndTraceHeaderRequestParser, | ||
getMultipleSentryEnvelopeRequests, | ||
shouldSkipFeatureFlagsTest, | ||
shouldSkipTracingTest, | ||
} from '../../../../../utils/helpers'; | ||
|
||
sentryTest( | ||
"GrowthBook onSpan: flags are added to active span's attributes on span end", | ||
async ({ getLocalTestUrl, page }) => { | ||
if (shouldSkipFeatureFlagsTest() || shouldSkipTracingTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
await page.route('https://dsn.ingest.sentry.io/**/*', route => { | ||
return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({}) }); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true }); | ||
await page.goto(url); | ||
|
||
const envelopeRequestPromise = getMultipleSentryEnvelopeRequests<EventAndTraceHeader>( | ||
page, | ||
1, | ||
{}, | ||
eventAndTraceHeaderRequestParser, | ||
); | ||
|
||
await page.evaluate(maxFlags => { | ||
(window as any).withNestedSpans(() => { | ||
const gb = new (window as any).GrowthBook(); | ||
for (let i = 1; i <= maxFlags; i++) { | ||
gb.isOn(`feat${i}`); | ||
} | ||
gb.__setOn(`feat${maxFlags + 1}`, true); | ||
gb.isOn(`feat${maxFlags + 1}`); // dropped | ||
gb.__setOn('feat3', true); | ||
gb.isOn('feat3'); // update | ||
}); | ||
return true; | ||
}, MAX_FLAGS_PER_SPAN); | ||
|
||
const event = (await envelopeRequestPromise)[0][0]; | ||
const innerSpan = event.spans?.[0]; | ||
const outerSpan = event.spans?.[1]; | ||
const outerSpanFlags = Object.entries(outerSpan?.data ?? {}).filter(([key, _val]) => | ||
key.startsWith('flag.evaluation'), | ||
); | ||
const innerSpanFlags = Object.entries(innerSpan?.data ?? {}).filter(([key, _val]) => | ||
key.startsWith('flag.evaluation'), | ||
); | ||
|
||
expect(innerSpanFlags).toEqual([]); | ||
|
||
const expectedOuterSpanFlags = [] as Array<[string, unknown]>; | ||
for (let i = 1; i <= MAX_FLAGS_PER_SPAN; i++) { | ||
expectedOuterSpanFlags.push([`flag.evaluation.feat${i}`, i === 3]); | ||
} | ||
expect(outerSpanFlags.sort()).toEqual(expectedOuterSpanFlags.sort()); | ||
}, | ||
); |
This file contains hidden or 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/growthbook/index.ts
This file contains hidden or 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 { growthbookIntegration } from './integration'; |
70 changes: 70 additions & 0 deletions
70
packages/browser/src/integrations/featureFlags/growthbook/integration.ts
This file contains hidden or 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,70 @@ | ||
import type { Client, Event, EventHint, IntegrationFn } from '@sentry/core'; | ||
import { | ||
_INTERNAL_addFeatureFlagToActiveSpan, | ||
_INTERNAL_copyFlagsFromScopeToEvent, | ||
_INTERNAL_insertFlagToScope, | ||
defineIntegration, | ||
fill, | ||
} from '@sentry/core'; | ||
import type { GrowthBook, GrowthBookClass } from './types'; | ||
|
||
/** | ||
* Sentry integration for capturing feature flag evaluations from GrowthBook. | ||
* | ||
* See the feature flag documentation: https://develop.sentry.dev/sdk/expected-features/#feature-flags | ||
* | ||
* @example | ||
* ``` | ||
* import { GrowthBook } from '@growthbook/growthbook'; | ||
* import * as Sentry from '@sentry/browser'; | ||
* | ||
* Sentry.init({ | ||
* dsn: '___PUBLIC_DSN___', | ||
* integrations: [Sentry.growthbookIntegration({ growthbookClass: GrowthBook })], | ||
* }); | ||
* | ||
* const gb = new GrowthBook(); | ||
* gb.isOn('my-feature'); | ||
* Sentry.captureException(new Error('something went wrong')); | ||
* ``` | ||
*/ | ||
export const growthbookIntegration = defineIntegration(({ growthbookClass }: { growthbookClass: GrowthBookClass }) => { | ||
return { | ||
name: 'GrowthBook', | ||
|
||
setupOnce() { | ||
const proto = growthbookClass.prototype as GrowthBook; | ||
fill(proto, 'isOn', _wrapBooleanReturningMethod); | ||
fill(proto, 'getFeatureValue', _wrapBooleanReturningMethod); | ||
// Also capture evalFeature when present. Not all versions have it, so guard. | ||
if (typeof (proto as unknown as Record<string, unknown>).evalFeature === 'function') { | ||
fill(proto as any, 'evalFeature', _wrapBooleanReturningMethod as any); | ||
} | ||
}, | ||
|
||
processEvent(event: Event, _hint: EventHint, _client: Client): Event { | ||
return _INTERNAL_copyFlagsFromScopeToEvent(event); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
function _wrapBooleanReturningMethod( | ||
original: (this: GrowthBook, ...args: unknown[]) => unknown, | ||
): (this: GrowthBook, ...args: unknown[]) => unknown { | ||
return function (this: GrowthBook, ...args: unknown[]): unknown { | ||
const flagName = args[0]; | ||
const result = original.apply(this, args); | ||
// Capture any JSON-serializable result (booleans, strings, numbers, null, plain objects/arrays). | ||
// Skip functions/symbols/undefined. | ||
if ( | ||
typeof flagName === 'string' && | ||
typeof result !== 'undefined' && | ||
typeof result !== 'function' && | ||
typeof result !== 'symbol' | ||
) { | ||
_INTERNAL_insertFlagToScope(flagName, result); | ||
_INTERNAL_addFeatureFlagToActiveSpan(flagName, result); | ||
} | ||
return result; | ||
}; | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/browser/src/integrations/featureFlags/growthbook/types.ts
This file contains hidden or 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,7 @@ | ||
export interface GrowthBook { | ||
isOn(this: GrowthBook, featureKey: string): boolean; | ||
getFeatureValue(this: GrowthBook, featureKey: string, defaultValue: unknown): unknown; | ||
} | ||
|
||
// We only depend on the surface we wrap; constructor args are irrelevant here. | ||
export type GrowthBookClass = new (...args: unknown[]) => GrowthBook; |
Oops, something went wrong.
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.