Skip to content

Commit 32b29e9

Browse files
mydeamsonnb
authored andcommitted
ref(core): Avoid keeping span-FF map on GLOBAL_OBJ (#16924)
Instead, we can just check the required things directly on the span. This is likely a bit more expensive than checking a map, but I would not assume this operation should be happening too excessively, so IMHO this should be fine. This is also part of #16846 @aliu39 do you see any problems with this change?
1 parent 487ee5d commit 32b29e9

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

packages/core/src/utils/featureFlags.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { getCurrentScope } from '../currentScopes';
22
import { DEBUG_BUILD } from '../debug-build';
33
import { type Event } from '../types-hoist/event';
4-
import { type Span } from '../types-hoist/span';
54
import { 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
*/
2523
export 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-
3025
const 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 (`${SPAN_FLAG_ATTRIBUTE_PREFIX}${name}` in attributes) {
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
}

packages/core/src/utils/worldwide.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
/* eslint-disable @typescript-eslint/no-explicit-any */
1414

1515
import type { Carrier } from '../carrier';
16-
import type { Span } from '../types-hoist/span';
1716
import type { SdkSource } from './env';
1817

1918
/** Internal global with common properties and Sentry extensions */
@@ -49,10 +48,6 @@ export type InternalGlobal = {
4948
*/
5049
_sentryModuleMetadata?: Record<string, any>;
5150
_sentryEsmLoaderHookRegistered?: boolean;
52-
/**
53-
* A map of spans to evaluated feature flags. Populated by feature flag integrations.
54-
*/
55-
_spanToFlagBufferMap?: WeakMap<Span, Set<string>>;
5651
} & Carrier;
5752

5853
/** Get's the global object for the current JavaScript runtime */

0 commit comments

Comments
 (0)