Skip to content

Commit 94c120e

Browse files
committed
Move api to top-level, rename addFlag
1 parent 8570801 commit 94c120e

File tree

7 files changed

+37
-37
lines changed

7 files changed

+37
-37
lines changed

dev-packages/browser-integration-tests/suites/integrations/featureFlags/featureFlags/basic/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ sentryTest('Basic test with eviction, update, and no async tasks', async ({ getL
2323
await page.goto(url);
2424

2525
await page.evaluate(bufferSize => {
26-
const flagsIntegration = (window as any).Sentry.getClient().getIntegrationByName('FeatureFlags');
26+
const Sentry = (window as any).Sentry;
2727
for (let i = 1; i <= bufferSize; i++) {
28-
flagsIntegration.setFlag(`feat${i}`, false);
28+
Sentry.addFlag(`feat${i}`, false);
2929
}
30-
flagsIntegration.setFlag(`feat${bufferSize + 1}`, true); // eviction
31-
flagsIntegration.setFlag('feat3', true); // update
30+
Sentry.addFlag(`feat${bufferSize + 1}`, true); // eviction
31+
Sentry.addFlag('feat3', true); // update
3232
return true;
3333
}, FLAG_BUFFER_SIZE);
3434

dev-packages/browser-integration-tests/suites/integrations/featureFlags/featureFlags/init.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import * as Sentry from '@sentry/browser';
22

33
window.Sentry = Sentry;
44

5-
// Not using this as we want to test the getIntegrationByName() approach
6-
// window.sentryFeatureFlagsIntegration = Sentry.featureFlagsIntegration();
7-
85
Sentry.init({
96
dsn: 'https://[email protected]/1337',
107
sampleRate: 1.0,

dev-packages/browser-integration-tests/suites/integrations/featureFlags/featureFlags/withScope/test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,19 @@ sentryTest('Flag evaluations in forked scopes are stored separately.', async ({
2828
await page.evaluate(() => {
2929
const Sentry = (window as any).Sentry;
3030
const errorButton = document.querySelector('#error') as HTMLButtonElement;
31-
const flagsIntegration = (window as any).Sentry.getClient().getIntegrationByName('FeatureFlags');
3231

33-
flagsIntegration.setFlag('shared', true);
32+
Sentry.addFlag('shared', true);
3433

3534
Sentry.withScope((scope: Scope) => {
36-
flagsIntegration.setFlag('forked', true);
37-
flagsIntegration.setFlag('shared', false);
35+
Sentry.addFlag('forked', true);
36+
Sentry.addFlag('shared', false);
3837
scope.setTag('isForked', true);
3938
if (errorButton) {
4039
errorButton.click();
4140
}
4241
});
4342

44-
flagsIntegration.setFlag('main', true);
43+
Sentry.addFlag('main', true);
4544
Sentry.getCurrentScope().setTag('isForked', false);
4645
errorButton.click();
4746
return true;

packages/browser/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export { spotlightBrowserIntegration } from './integrations/spotlight';
7171
export { browserSessionIntegration } from './integrations/browsersession';
7272
export {
7373
featureFlagsIntegration,
74-
type FeatureFlagsIntegration,
74+
addFlag,
7575
} from './integrations/featureFlags';
7676
export { launchDarklyIntegration, buildLaunchDarklyFlagUsedHandler } from './integrations/featureFlags/launchdarkly';
7777
export { openFeatureIntegration, OpenFeatureIntegrationHook } from './integrations/featureFlags/openfeature';
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import type { Client, Event, EventHint, Integration, IntegrationFn } from '@sentry/core';
1+
import type { Client, Event, EventHint, IntegrationFn } from '@sentry/core';
22

3-
import { defineIntegration } from '@sentry/core';
3+
import { defineIntegration, getClient, logger } from '@sentry/core';
44
import { copyFlagsFromScopeToEvent, insertFlagToScope } from '../../utils/featureFlags';
5-
6-
export interface FeatureFlagsIntegration extends Integration {
7-
setFlag: (name: string, value: unknown) => void;
8-
}
5+
import { DEBUG_BUILD } from '../../debug-build';
96

107
/**
118
* Sentry integration for buffering feature flags manually with an API, and
@@ -17,19 +14,11 @@ export interface FeatureFlagsIntegration extends Integration {
1714
* @example
1815
* ```
1916
* import * as Sentry from '@sentry/browser';
20-
* import { type FeatureFlagsIntegration } from '@sentry/browser';
2117
*
22-
* // Setup
23-
* Sentry.init(..., integrations: [Sentry.featureFlagsIntegration()])
18+
* Sentry.init(..., integrations: [Sentry.featureFlagsIntegration()]);
2419
*
25-
* // Verify
26-
* const flagsIntegration = Sentry.getClient()?.getIntegrationByName<FeatureFlagsIntegration>('FeatureFlags');
27-
* if (flagsIntegration) {
28-
* flagsIntegration.setFlag('my-flag', true);
29-
* } else {
30-
* // check your setup
31-
* }
32-
* Sentry.captureException(Exception('broke')); // 'my-flag' should be captured to this Sentry event.
20+
* Sentry.addFlag('my-flag', true);
21+
* Sentry.captureException(Exception('broke')); // 'my-flag' should be captured on this Sentry event.
3322
* ```
3423
*/
3524
export const featureFlagsIntegration = defineIntegration(() => {
@@ -38,10 +27,21 @@ export const featureFlagsIntegration = defineIntegration(() => {
3827

3928
processEvent(event: Event, _hint: EventHint, _client: Client): Event {
4029
return copyFlagsFromScopeToEvent(event);
41-
},
42-
43-
setFlag(name: string, value: unknown): void {
44-
insertFlagToScope(name, value);
45-
},
30+
}
4631
};
47-
}) as IntegrationFn<FeatureFlagsIntegration>;
32+
}) as IntegrationFn;
33+
34+
35+
/**
36+
* Records a flag and its value to be sent on subsequent error events. We
37+
* recommend you do this on flag evaluations. Flags are buffered per Sentry
38+
* scope and limited to 100 per event.
39+
*/
40+
export function addFlag(name: string, value: unknown): void {
41+
const client = getClient();
42+
if (!client || !client.getIntegrationByName('FeatureFlags')) {
43+
DEBUG_BUILD && logger.error('Must enable the Feature Flags Integration to use the addFlag function.');
44+
return;
45+
}
46+
insertFlagToScope(name, value);
47+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { featureFlagsIntegration, type FeatureFlagsIntegration } from './featureFlagsIntegration';
1+
export { featureFlagsIntegration, addFlag } from './featureFlagsIntegration';

test-results/.last-run.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"status": "failed",
3+
"failedTests": []
4+
}

0 commit comments

Comments
 (0)