1- import type { FeatureFlag } from '@sentry/types' ;
1+ import { getCurrentScope } from '@sentry/core' ;
2+ import type { Event , FeatureFlag } from '@sentry/types' ;
23import { logger } from '@sentry/utils' ;
34import { DEBUG_BUILD } from '../debug-build' ;
45
@@ -13,6 +14,21 @@ import { DEBUG_BUILD } from '../debug-build';
1314 */
1415export const FLAG_BUFFER_SIZE = 100 ;
1516
17+ /**
18+ * Copies feature flags that are in current scope context to the event context
19+ */
20+ export function copyFlagsFromScopeToEvent ( event : Event ) : Event {
21+ const scope = getCurrentScope ( ) ;
22+ const flagContext = scope . getScopeData ( ) . contexts . flags ;
23+ const flagBuffer = flagContext ? flagContext . values : [ ] ;
24+
25+ if ( event . contexts === undefined ) {
26+ event . contexts = { } ;
27+ }
28+ event . contexts . flags = { values : [ ...flagBuffer ] } ;
29+ return event ;
30+ }
31+
1632/**
1733 * Insert into a FeatureFlag array while maintaining ordered LRU properties. Not
1834 * thread-safe. After inserting:
@@ -21,18 +37,23 @@ export const FLAG_BUFFER_SIZE = 100;
2137 * - The length of `flags` does not exceed `maxSize`. The oldest flag is evicted
2238 * as needed.
2339 *
24- * @param flags The array to insert into.
2540 * @param name Name of the feature flag to insert.
2641 * @param value Value of the feature flag.
2742 * @param maxSize Max number of flags the buffer should store. It's recommended
2843 * to keep this consistent across insertions. Default is DEFAULT_MAX_SIZE
2944 */
30- export function insertToFlagBuffer (
31- flags : FeatureFlag [ ] ,
32- name : string ,
33- value : boolean ,
34- maxSize : number = FLAG_BUFFER_SIZE ,
35- ) : void {
45+ export function insertToFlagBuffer ( name : string , value : unknown , maxSize : number = FLAG_BUFFER_SIZE ) : void {
46+ // Currently only accepts boolean values
47+ if ( typeof value !== 'boolean' ) {
48+ return ;
49+ }
50+
51+ const scopeContexts = getCurrentScope ( ) . getScopeData ( ) . contexts ;
52+ if ( ! scopeContexts . flags ) {
53+ scopeContexts . flags = { values : [ ] } ;
54+ }
55+ const flags = scopeContexts . flags . values as FeatureFlag [ ] ;
56+
3657 if ( flags . length > maxSize ) {
3758 DEBUG_BUILD && logger . error ( `[Feature Flags] insertToFlagBuffer called on a buffer larger than maxSize=${ maxSize } ` ) ;
3859 return ;
0 commit comments