1
- import type { FeatureFlag } from '@sentry/types' ;
1
+ import { getCurrentScope } from '@sentry/core' ;
2
+ import type { Event , FeatureFlag } from '@sentry/types' ;
2
3
import { logger } from '@sentry/utils' ;
3
4
import { DEBUG_BUILD } from '../debug-build' ;
4
5
@@ -13,6 +14,21 @@ import { DEBUG_BUILD } from '../debug-build';
13
14
*/
14
15
export const FLAG_BUFFER_SIZE = 100 ;
15
16
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
+
16
32
/**
17
33
* Insert into a FeatureFlag array while maintaining ordered LRU properties. Not
18
34
* thread-safe. After inserting:
@@ -21,18 +37,23 @@ export const FLAG_BUFFER_SIZE = 100;
21
37
* - The length of `flags` does not exceed `maxSize`. The oldest flag is evicted
22
38
* as needed.
23
39
*
24
- * @param flags The array to insert into.
25
40
* @param name Name of the feature flag to insert.
26
41
* @param value Value of the feature flag.
27
42
* @param maxSize Max number of flags the buffer should store. It's recommended
28
43
* to keep this consistent across insertions. Default is DEFAULT_MAX_SIZE
29
44
*/
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
+
36
57
if ( flags . length > maxSize ) {
37
58
DEBUG_BUILD && logger . error ( `[Feature Flags] insertToFlagBuffer called on a buffer larger than maxSize=${ maxSize } ` ) ;
38
59
return ;
0 commit comments