66 * - OpenFeature.getClient().addHooks(new OpenFeatureIntegrationHook());
77 */
88import type { Client , Event , EventHint , IntegrationFn } from '@sentry/types' ;
9- import type { EvaluationDetails , FlagValue , HookContext , HookHints , JsonValue , OpenFeatureHook } from './types' ;
9+ import type { EvaluationDetails , HookContext , HookHints , JsonValue , OpenFeatureHook } from './types' ;
1010
11- import { defineIntegration , getCurrentScope } from '@sentry/core' ;
12- import { insertToFlagBuffer } from '../../../utils/featureFlags' ;
11+ import { defineIntegration } from '@sentry/core' ;
12+ import { copyFlagsFromScopeToEvent , insertFlagToScope } from '../../../utils/featureFlags' ;
1313
1414export const openFeatureIntegration = defineIntegration ( ( ) => {
1515 return {
1616 name : 'OpenFeature' ,
1717
1818 processEvent ( event : Event , _hint : EventHint , _client : Client ) : Event {
19- const scope = getCurrentScope ( ) ;
20- const flagContext = scope . getScopeData ( ) . contexts . flags ;
21- const flagBuffer = flagContext ? flagContext . values : [ ] ;
22-
23- if ( event . contexts === undefined ) {
24- event . contexts = { } ;
25- }
26- event . contexts . flags = { values : [ ...flagBuffer ] } ;
27- return event ;
19+ return copyFlagsFromScopeToEvent ( event ) ;
2820 } ,
2921 } ;
3022} ) satisfies IntegrationFn ;
@@ -37,25 +29,13 @@ export class OpenFeatureIntegrationHook implements OpenFeatureHook {
3729 * Successful evaluation result.
3830 */
3931 public after ( _hookContext : Readonly < HookContext < JsonValue > > , evaluationDetails : EvaluationDetails < JsonValue > ) : void {
40- processEvent ( evaluationDetails . flagKey , evaluationDetails . value ) ;
32+ insertFlagToScope ( evaluationDetails . flagKey , evaluationDetails . value ) ;
4133 }
4234
4335 /**
4436 * On error evaluation result.
4537 */
4638 public error ( hookContext : Readonly < HookContext < JsonValue > > , _error : unknown , _hookHints ?: HookHints ) : void {
47- processEvent ( hookContext . flagKey , hookContext . defaultValue ) ;
48- }
49- }
50-
51- function processEvent ( key : string , value : FlagValue ) : void {
52- if ( typeof value === 'boolean' ) {
53- const scopeContexts = getCurrentScope ( ) . getScopeData ( ) . contexts ;
54- if ( ! scopeContexts . flags ) {
55- scopeContexts . flags = { values : [ ] } ;
56- }
57- const flagBuffer = scopeContexts . flags . values ;
58- insertToFlagBuffer ( flagBuffer , key , value ) ;
39+ insertFlagToScope ( hookContext . flagKey , hookContext . defaultValue ) ;
5940 }
60- return ;
6141}
0 commit comments