Skip to content

ref(flags): rewrite span flag tracking to not use global map #16651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions packages/core/src/utils/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { getCurrentScope } from '../currentScopes';
import { DEBUG_BUILD } from '../debug-build';
import { type Event } from '../types-hoist/event';
import { type Span } from '../types-hoist/span';
import { logger } from '../utils/logger';
import { GLOBAL_OBJ } from '../utils/worldwide';
import { getActiveSpan } from './spanUtils';
import { getActiveSpan, spanToJSON } from './spanUtils';

/**
* Ordered LRU cache for storing feature flags in the scope context. The name
Expand All @@ -24,9 +22,6 @@ export const _INTERNAL_FLAG_BUFFER_SIZE = 100;
*/
export const _INTERNAL_MAX_FLAGS_PER_SPAN = 10;

// Global map of spans to feature flag buffers. Populated by feature flag integrations.
GLOBAL_OBJ._spanToFlagBufferMap = new WeakMap<Span, Set<string>>();

const SPAN_FLAG_ATTRIBUTE_PREFIX = 'flag.evaluation.';

/**
Expand Down Expand Up @@ -133,20 +128,16 @@ export function _INTERNAL_addFeatureFlagToActiveSpan(
value: unknown,
maxFlagsPerSpan: number = _INTERNAL_MAX_FLAGS_PER_SPAN,
): void {
const spanFlagMap = GLOBAL_OBJ._spanToFlagBufferMap;
if (!spanFlagMap || typeof value !== 'boolean') {
if (typeof value !== 'boolean') {
return;
}

const span = getActiveSpan();
if (span) {
const flags = spanFlagMap.get(span) || new Set<string>();
if (flags.has(name)) {
span.setAttribute(`${SPAN_FLAG_ATTRIBUTE_PREFIX}${name}`, value);
} else if (flags.size < maxFlagsPerSpan) {
flags.add(name);
const attributes = spanToJSON(span).data;
const flags = Object.keys(attributes).filter(key => key.startsWith(SPAN_FLAG_ATTRIBUTE_PREFIX));
if (flags.length < maxFlagsPerSpan || flags.find(flag => flag === `${SPAN_FLAG_ATTRIBUTE_PREFIX}${name}`)) {
span.setAttribute(`${SPAN_FLAG_ATTRIBUTE_PREFIX}${name}`, value);
}
spanFlagMap.set(span, flags);
}
}
5 changes: 0 additions & 5 deletions packages/core/src/utils/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import type { Carrier } from '../carrier';
import type { Client } from '../client';
import type { SerializedLog } from '../types-hoist/log';
import type { Span } from '../types-hoist/span';
import type { SdkSource } from './env';

/** Internal global with common properties and Sentry extensions */
Expand Down Expand Up @@ -57,10 +56,6 @@ export type InternalGlobal = {
*/
_sentryModuleMetadata?: Record<string, any>;
_sentryEsmLoaderHookRegistered?: boolean;
/**
* A map of spans to evaluated feature flags. Populated by feature flag integrations.
*/
_spanToFlagBufferMap?: WeakMap<Span, Set<string>>;
} & Carrier;

/** Get's the global object for the current JavaScript runtime */
Expand Down
Loading