diff --git a/packages/browser/src/profiling/hubextensions.ts b/packages/browser/src/profiling/hubextensions.ts index e39568cc183b..c42438103b2c 100644 --- a/packages/browser/src/profiling/hubextensions.ts +++ b/packages/browser/src/profiling/hubextensions.ts @@ -10,7 +10,7 @@ import type { JSSelfProfilerConstructor, ProcessedJSSelfProfile, } from './jsSelfProfiling'; -import { addToProfileQueue, isValidSampleRate } from './utils'; +import { addProfileToMap,isValidSampleRate } from './utils'; export const MAX_PROFILE_DURATION_MS = 30_000; // Keep a flag value to avoid re-initializing the profiler constructor. If it fails @@ -222,7 +222,8 @@ export function wrapTransactionWithProfiling(transaction: Transaction): Transact return null; } - addToProfileQueue({ ...p, profile_id: profileId }); + + addProfileToMap({...p, profile_id: profileId }); return null; }) .catch(error => { diff --git a/packages/browser/src/profiling/integration.ts b/packages/browser/src/profiling/integration.ts index 05bd15e84d96..d04795481662 100644 --- a/packages/browser/src/profiling/integration.ts +++ b/packages/browser/src/profiling/integration.ts @@ -9,7 +9,7 @@ import { addProfilesToEnvelope, createProfilingEvent, findProfiledTransactionsFromEnvelope, - PROFILE_QUEUE, + PROFILE_MAP, } from './utils'; /** @@ -39,8 +39,7 @@ export class BrowserProfilingIntegration implements Integration { client.on('beforeEnvelope', (envelope): void => { // if not profiles are in queue, there is nothing to add to the envelope. - - if (!PROFILE_QUEUE.length) { + if (!PROFILE_MAP.size) { return; } @@ -56,7 +55,7 @@ export class BrowserProfilingIntegration implements Integration { profiledTransaction && profiledTransaction.contexts && profiledTransaction.contexts['profile'] && - profiledTransaction.contexts['profile']['profile_id']; + profiledTransaction.contexts['profile']['profile_id'] as string if (!profile_id) { throw new TypeError('[Profiling] cannot find profile for a transaction without a profile context'); @@ -67,23 +66,15 @@ export class BrowserProfilingIntegration implements Integration { delete profiledTransaction.contexts.profile; } - // We need to find both a profile and a transaction event for the same profile_id. - const profileIndex = PROFILE_QUEUE.findIndex(p => p.profile_id === profile_id); - if (profileIndex === -1) { - __DEBUG_BUILD__ && logger.log(`[Profiling] Could not retrieve profile for transaction: ${profile_id}`); - continue; - } - - const cpuProfile = PROFILE_QUEUE[profileIndex]; - if (!cpuProfile) { + const profile = PROFILE_MAP.get(profile_id); + if (!profile) { __DEBUG_BUILD__ && logger.log(`[Profiling] Could not retrieve profile for transaction: ${profile_id}`); continue; } - // Remove the profile from the queue. - PROFILE_QUEUE.splice(profileIndex, 1); - const profileEvent = createProfilingEvent(cpuProfile, profiledTransaction as ProfiledEvent); - + PROFILE_MAP.delete(profile_id); + const profileEvent = createProfilingEvent(profile, profiledTransaction as ProfiledEvent); + if (profileEvent) { profilesToAddToEnvelope.push(profileEvent); } diff --git a/packages/browser/src/profiling/utils.ts b/packages/browser/src/profiling/utils.ts index bb4a0f0171cf..2aa6293ef435 100644 --- a/packages/browser/src/profiling/utils.ts +++ b/packages/browser/src/profiling/utils.ts @@ -451,16 +451,16 @@ export function createProfilingEvent(profile: ProcessedJSSelfProfile, event: Pro return createProfilePayload(event, profile); } -const MAX_PROFILE_QUEUE_SIZE = 50; -export const PROFILE_QUEUE: ProcessedJSSelfProfile[] = []; + +export const PROFILE_MAP: Map = new Map(); /** - * Adds the profile to the queue of profiles to be sent + * */ -export function addToProfileQueue(profile: ProcessedJSSelfProfile): void { - PROFILE_QUEUE.push(profile); +export function addProfileToMap(profile: ProcessedJSSelfProfile): void{ + PROFILE_MAP.set(profile.profile_id, profile); - // We only want to keep the last n profiles in the queue. - if (PROFILE_QUEUE.length > MAX_PROFILE_QUEUE_SIZE) { - PROFILE_QUEUE.shift(); + if(PROFILE_MAP.size > 30){ + const last: string = [...PROFILE_MAP.keys()].pop() as string; + PROFILE_MAP.delete(last); } -} +} \ No newline at end of file