Skip to content

Commit 7f24233

Browse files
committed
ref(flags): abstract OpenFeatureHook away from setup and track a single OpenFeatureClient
1 parent 3693a5d commit 7f24233

File tree

10 files changed

+52
-34
lines changed

10 files changed

+52
-34
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ 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 client = (window as any).initialize();
26+
const client = (window as any).openFeatureClient;
2727
for (let i = 1; i <= bufferSize; i++) {
2828
client.getBooleanValue(`feat${i}`, false);
2929
}

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import * as Sentry from '@sentry/browser';
22

3+
window.openFeatureClient = {
4+
_hooks: [],
5+
6+
getBooleanValue(flag, value) {
7+
this._hooks.forEach(hook => {
8+
hook.error({ flagKey: flag, defaultValue: false }, new Error('flag eval error'));
9+
});
10+
return value;
11+
},
12+
13+
addHooks(hooks) {
14+
this._hooks = [...this._hooks, ...hooks];
15+
},
16+
};
17+
318
window.Sentry = Sentry;
4-
window.sentryOpenFeatureIntegration = Sentry.openFeatureIntegration();
19+
window.sentryOpenFeatureIntegration = Sentry.openFeatureIntegration(window.openFeatureClient);
520

621
Sentry.init({
722
dsn: 'https://[email protected]/1337',
823
sampleRate: 1.0,
924
integrations: [window.sentryOpenFeatureIntegration],
1025
});
11-
12-
window.initialize = () => {
13-
return {
14-
getBooleanValue(flag, value) {
15-
let hook = new Sentry.OpenFeatureIntegrationHook();
16-
hook.error({ flagKey: flag, defaultValue: false }, new Error('flag eval error'));
17-
return value;
18-
},
19-
};
20-
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ sentryTest('Flag evaluation error hook', async ({ getLocalTestUrl, page }) => {
2323
await page.goto(url);
2424

2525
await page.evaluate(bufferSize => {
26-
const client = (window as any).initialize();
26+
const client = (window as any).openFeatureClient;
2727
for (let i = 1; i <= bufferSize; i++) {
2828
client.getBooleanValue(`feat${i}`, false);
2929
}

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import * as Sentry from '@sentry/browser';
22

3+
window.openFeatureClient = {
4+
_hooks: [],
5+
6+
getBooleanValue(flag, value) {
7+
this._hooks.forEach(hook => {
8+
hook.after(null, { flagKey: flag, value: value });
9+
});
10+
return value;
11+
},
12+
13+
addHooks(hooks) {
14+
this._hooks = [...this._hooks, ...hooks];
15+
},
16+
};
17+
318
window.Sentry = Sentry;
4-
window.sentryOpenFeatureIntegration = Sentry.openFeatureIntegration();
19+
window.sentryOpenFeatureIntegration = Sentry.openFeatureIntegration(window.openFeatureClient);
520

621
Sentry.init({
722
dsn: 'https://[email protected]/1337',
823
sampleRate: 1.0,
924
integrations: [window.sentryOpenFeatureIntegration],
1025
});
11-
12-
window.initialize = () => {
13-
return {
14-
getBooleanValue(flag, value) {
15-
let hook = new Sentry.OpenFeatureIntegrationHook();
16-
hook.after(null, { flagKey: flag, value: value });
17-
return value;
18-
},
19-
};
20-
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ 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 client = (window as any).initialize();
31+
const client = (window as any).openFeatureClient;
3232

3333
client.getBooleanValue('shared', true);
3434

packages/browser/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ export {
7474
type FeatureFlagsIntegration,
7575
} from './integrations/featureFlags';
7676
export { launchDarklyIntegration, buildLaunchDarklyFlagUsedHandler } from './integrations/featureFlags/launchdarkly';
77-
export { openFeatureIntegration, OpenFeatureIntegrationHook } from './integrations/featureFlags/openfeature';
77+
export { openFeatureIntegration } from './integrations/featureFlags/openfeature';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { openFeatureIntegration, OpenFeatureIntegrationHook } from './integration';
1+
export { openFeatureIntegration } from './integration';

packages/browser/src/integrations/featureFlags/openfeature/integration.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,29 @@
66
* - OpenFeature.getClient().addHooks(new OpenFeatureIntegrationHook());
77
*/
88
import type { Client, Event, EventHint, IntegrationFn } from '@sentry/core';
9-
import type { EvaluationDetails, HookContext, HookHints, JsonValue, OpenFeatureHook } from './types';
9+
import type { EvaluationDetails, HookContext, HookHints, JsonValue, OpenFeatureHook, OpenFeatureClient } from './types';
1010

1111
import { defineIntegration } from '@sentry/core';
1212
import { copyFlagsFromScopeToEvent, insertFlagToScope } from '../../../utils/featureFlags';
1313

14-
export const openFeatureIntegration = defineIntegration(() => {
14+
export const openFeatureIntegration = defineIntegration((openFeatureClient: OpenFeatureClient) => {
1515
return {
1616
name: 'OpenFeature',
1717

1818
processEvent(event: Event, _hint: EventHint, _client: Client): Event {
1919
return copyFlagsFromScopeToEvent(event);
2020
},
21+
22+
setupOnce() {
23+
openFeatureClient.addHooks([new OpenFeatureIntegrationHook()]);
24+
}
2125
};
2226
}) satisfies IntegrationFn;
2327

2428
/**
2529
* OpenFeature Hook class implementation.
2630
*/
27-
export class OpenFeatureIntegrationHook implements OpenFeatureHook {
31+
class OpenFeatureIntegrationHook implements OpenFeatureHook {
2832
/**
2933
* Successful evaluation result.
3034
*/

packages/browser/src/integrations/featureFlags/openfeature/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ export interface BaseHook<T extends FlagValue = FlagValue, BeforeHookReturn = un
8787
finally?(hookContext: Readonly<HookContext<T>>, hookHints?: HookHints): HooksReturn;
8888
}
8989
export type OpenFeatureHook = BaseHook<FlagValue, void, void>;
90+
91+
export interface OpenFeatureClient {
92+
addHooks(hooks: OpenFeatureHook[]): OpenFeatureClient;
93+
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8060,10 +8060,10 @@
80608060
resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.28.tgz#d45e01c4a56f143ee69c54dd6b12eade9e270a73"
80618061
integrity sha512-8LduaNlMZGwdZ6qWrKlfa+2M4gahzFkprZiAt2TF8uS0qQgBizKXpXURqvTJ4WtmupWxaLqjRb2UCTe72mu+Aw==
80628062

8063-
"@prisma/client@5.9.1":
8064-
version "5.9.1"
8065-
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.9.1.tgz#d92bd2f7f006e0316cb4fda9d73f235965cf2c64"
8066-
integrity sha512-caSOnG4kxcSkhqC/2ShV7rEoWwd3XrftokxJqOCMVvia4NYV/TPtJlS9C2os3Igxw/Qyxumj9GBQzcStzECvtQ==
8063+
"@prisma/client@5.22.0":
8064+
version "5.22.0"
8065+
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.22.0.tgz#da1ca9c133fbefe89e0da781c75e1c59da5f8802"
8066+
integrity sha512-M0SVXfyHnQREBKxCgyo7sffrKttwE6R8PMq330MIUF0pTwjUhLbW84pFDlf06B27XyCR++VtjugEnIHdr07SVA==
80678067

80688068
80698069
version "5.22.0"

0 commit comments

Comments
 (0)