11import { getCurrentScope } from '../currentScopes' ;
22import { DEBUG_BUILD } from '../debug-build' ;
33import { type Event } from '../types-hoist/event' ;
4- import { type Span } from '../types-hoist/span' ;
54import { debug } from '../utils/logger' ;
6- import { GLOBAL_OBJ } from '../utils/worldwide' ;
7- import { getActiveSpan } from './spanUtils' ;
5+ import { getActiveSpan , spanToJSON } from './spanUtils' ;
86
97/**
108 * Ordered LRU cache for storing feature flags in the scope context. The name
@@ -24,9 +22,6 @@ export const _INTERNAL_FLAG_BUFFER_SIZE = 100;
2422 */
2523export const _INTERNAL_MAX_FLAGS_PER_SPAN = 10 ;
2624
27- // Global map of spans to feature flag buffers. Populated by feature flag integrations.
28- GLOBAL_OBJ . _spanToFlagBufferMap = new WeakMap < Span , Set < string > > ( ) ;
29-
3025const SPAN_FLAG_ATTRIBUTE_PREFIX = 'flag.evaluation.' ;
3126
3227/**
@@ -133,20 +128,26 @@ export function _INTERNAL_addFeatureFlagToActiveSpan(
133128 value : unknown ,
134129 maxFlagsPerSpan : number = _INTERNAL_MAX_FLAGS_PER_SPAN ,
135130) : void {
136- const spanFlagMap = GLOBAL_OBJ . _spanToFlagBufferMap ;
137- if ( ! spanFlagMap || typeof value !== 'boolean' ) {
131+ if ( typeof value !== 'boolean' ) {
138132 return ;
139133 }
140134
141135 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 ) ;
151152 }
152153}
0 commit comments