Skip to content

Commit

Permalink
fix: use map instead of queue
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Jun 22, 2023
1 parent f0be011 commit 25d30ae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
5 changes: 3 additions & 2 deletions packages/browser/src/profiling/hubextensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 => {
Expand Down
25 changes: 8 additions & 17 deletions packages/browser/src/profiling/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
addProfilesToEnvelope,
createProfilingEvent,
findProfiledTransactionsFromEnvelope,
PROFILE_QUEUE,
PROFILE_MAP,
} from './utils';

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -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');
Expand All @@ -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);
}
Expand Down
18 changes: 9 additions & 9 deletions packages/browser/src/profiling/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ProcessedJSSelfProfile> = 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);
}
}
}

0 comments on commit 25d30ae

Please sign in to comment.