Skip to content

Commit

Permalink
Move api to top-level, rename addFlag
Browse files Browse the repository at this point in the history
  • Loading branch information
aliu39 committed Dec 6, 2024
1 parent 8570801 commit 94c120e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ sentryTest('Basic test with eviction, update, and no async tasks', async ({ getL
await page.goto(url);

await page.evaluate(bufferSize => {
const flagsIntegration = (window as any).Sentry.getClient().getIntegrationByName('FeatureFlags');
const Sentry = (window as any).Sentry;
for (let i = 1; i <= bufferSize; i++) {
flagsIntegration.setFlag(`feat${i}`, false);
Sentry.addFlag(`feat${i}`, false);
}
flagsIntegration.setFlag(`feat${bufferSize + 1}`, true); // eviction
flagsIntegration.setFlag('feat3', true); // update
Sentry.addFlag(`feat${bufferSize + 1}`, true); // eviction
Sentry.addFlag('feat3', true); // update
return true;
}, FLAG_BUFFER_SIZE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

// Not using this as we want to test the getIntegrationByName() approach
// window.sentryFeatureFlagsIntegration = Sentry.featureFlagsIntegration();

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
sampleRate: 1.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ sentryTest('Flag evaluations in forked scopes are stored separately.', async ({
await page.evaluate(() => {
const Sentry = (window as any).Sentry;
const errorButton = document.querySelector('#error') as HTMLButtonElement;
const flagsIntegration = (window as any).Sentry.getClient().getIntegrationByName('FeatureFlags');

flagsIntegration.setFlag('shared', true);
Sentry.addFlag('shared', true);

Sentry.withScope((scope: Scope) => {
flagsIntegration.setFlag('forked', true);
flagsIntegration.setFlag('shared', false);
Sentry.addFlag('forked', true);
Sentry.addFlag('shared', false);
scope.setTag('isForked', true);
if (errorButton) {
errorButton.click();
}
});

flagsIntegration.setFlag('main', true);
Sentry.addFlag('main', true);
Sentry.getCurrentScope().setTag('isForked', false);
errorButton.click();
return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export { spotlightBrowserIntegration } from './integrations/spotlight';
export { browserSessionIntegration } from './integrations/browsersession';
export {
featureFlagsIntegration,
type FeatureFlagsIntegration,
addFlag,
} from './integrations/featureFlags';
export { launchDarklyIntegration, buildLaunchDarklyFlagUsedHandler } from './integrations/featureFlags/launchdarkly';
export { openFeatureIntegration, OpenFeatureIntegrationHook } from './integrations/featureFlags/openfeature';
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { Client, Event, EventHint, Integration, IntegrationFn } from '@sentry/core';
import type { Client, Event, EventHint, IntegrationFn } from '@sentry/core';

import { defineIntegration } from '@sentry/core';
import { defineIntegration, getClient, logger } from '@sentry/core';
import { copyFlagsFromScopeToEvent, insertFlagToScope } from '../../utils/featureFlags';

export interface FeatureFlagsIntegration extends Integration {
setFlag: (name: string, value: unknown) => void;
}
import { DEBUG_BUILD } from '../../debug-build';

/**
* Sentry integration for buffering feature flags manually with an API, and
Expand All @@ -17,19 +14,11 @@ export interface FeatureFlagsIntegration extends Integration {
* @example
* ```
* import * as Sentry from '@sentry/browser';
* import { type FeatureFlagsIntegration } from '@sentry/browser';
*
* // Setup
* Sentry.init(..., integrations: [Sentry.featureFlagsIntegration()])
* Sentry.init(..., integrations: [Sentry.featureFlagsIntegration()]);
*
* // Verify
* const flagsIntegration = Sentry.getClient()?.getIntegrationByName<FeatureFlagsIntegration>('FeatureFlags');
* if (flagsIntegration) {
* flagsIntegration.setFlag('my-flag', true);
* } else {
* // check your setup
* }
* Sentry.captureException(Exception('broke')); // 'my-flag' should be captured to this Sentry event.
* Sentry.addFlag('my-flag', true);
* Sentry.captureException(Exception('broke')); // 'my-flag' should be captured on this Sentry event.
* ```
*/
export const featureFlagsIntegration = defineIntegration(() => {
Expand All @@ -38,10 +27,21 @@ export const featureFlagsIntegration = defineIntegration(() => {

processEvent(event: Event, _hint: EventHint, _client: Client): Event {
return copyFlagsFromScopeToEvent(event);
},

setFlag(name: string, value: unknown): void {
insertFlagToScope(name, value);
},
}
};
}) as IntegrationFn<FeatureFlagsIntegration>;
}) as IntegrationFn;


/**
* Records a flag and its value to be sent on subsequent error events. We
* recommend you do this on flag evaluations. Flags are buffered per Sentry
* scope and limited to 100 per event.
*/
export function addFlag(name: string, value: unknown): void {
const client = getClient();
if (!client || !client.getIntegrationByName('FeatureFlags')) {
DEBUG_BUILD && logger.error('Must enable the Feature Flags Integration to use the addFlag function.');
return;
}
insertFlagToScope(name, value);
}
2 changes: 1 addition & 1 deletion packages/browser/src/integrations/featureFlags/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { featureFlagsIntegration, type FeatureFlagsIntegration } from './featureFlagsIntegration';
export { featureFlagsIntegration, addFlag } from './featureFlagsIntegration';
4 changes: 4 additions & 0 deletions test-results/.last-run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "failed",
"failedTests": []
}

0 comments on commit 94c120e

Please sign in to comment.