1
1
import { getCurrentScope } from '../currentScopes' ;
2
2
import { DEBUG_BUILD } from '../debug-build' ;
3
3
import { type Event } from '../types-hoist/event' ;
4
- import { type Span } from '../types-hoist/span' ;
5
4
import { debug } from '../utils/logger' ;
6
- import { GLOBAL_OBJ } from '../utils/worldwide' ;
7
- import { getActiveSpan } from './spanUtils' ;
5
+ import { getActiveSpan , spanToJSON } from './spanUtils' ;
8
6
9
7
/**
10
8
* Ordered LRU cache for storing feature flags in the scope context. The name
@@ -24,9 +22,6 @@ export const _INTERNAL_FLAG_BUFFER_SIZE = 100;
24
22
*/
25
23
export const _INTERNAL_MAX_FLAGS_PER_SPAN = 10 ;
26
24
27
- // Global map of spans to feature flag buffers. Populated by feature flag integrations.
28
- GLOBAL_OBJ . _spanToFlagBufferMap = new WeakMap < Span , Set < string > > ( ) ;
29
-
30
25
const SPAN_FLAG_ATTRIBUTE_PREFIX = 'flag.evaluation.' ;
31
26
32
27
/**
@@ -133,20 +128,26 @@ export function _INTERNAL_addFeatureFlagToActiveSpan(
133
128
value : unknown ,
134
129
maxFlagsPerSpan : number = _INTERNAL_MAX_FLAGS_PER_SPAN ,
135
130
) : void {
136
- const spanFlagMap = GLOBAL_OBJ . _spanToFlagBufferMap ;
137
- if ( ! spanFlagMap || typeof value !== 'boolean' ) {
131
+ if ( typeof value !== 'boolean' ) {
138
132
return ;
139
133
}
140
134
141
135
const span = getActiveSpan ( ) ;
142
- if ( span ) {
143
- const flags = spanFlagMap . get ( span ) || new Set < string > ( ) ;
144
- if ( flags . has ( name ) ) {
145
- span . setAttribute ( `${ SPAN_FLAG_ATTRIBUTE_PREFIX } ${ name } ` , value ) ;
146
- } else if ( flags . size < maxFlagsPerSpan ) {
147
- flags . add ( name ) ;
148
- span . setAttribute ( `${ SPAN_FLAG_ATTRIBUTE_PREFIX } ${ name } ` , value ) ;
149
- }
150
- spanFlagMap . set ( span , flags ) ;
136
+ if ( ! span ) {
137
+ return ;
138
+ }
139
+
140
+ const attributes = spanToJSON ( span ) . data ;
141
+
142
+ // If the flag already exists, always update it
143
+ if ( attributes [ `${ SPAN_FLAG_ATTRIBUTE_PREFIX } ${ name } ` ] ) {
144
+ span . setAttribute ( `${ SPAN_FLAG_ATTRIBUTE_PREFIX } ${ name } ` , value ) ;
145
+ return ;
146
+ }
147
+
148
+ // Else, add the flag to the span if we have not reached the max number of flags
149
+ const numOfAddedFlags = Object . keys ( attributes ) . filter ( key => key . startsWith ( SPAN_FLAG_ATTRIBUTE_PREFIX ) ) . length ;
150
+ if ( numOfAddedFlags < maxFlagsPerSpan ) {
151
+ span . setAttribute ( `${ SPAN_FLAG_ATTRIBUTE_PREFIX } ${ name } ` , value ) ;
151
152
}
152
153
}
0 commit comments