Skip to content

Commit c7078fb

Browse files
committed
ref: cleanup types and drop separate profile_id
1 parent 25d30ae commit c7078fb

File tree

4 files changed

+31
-50
lines changed

4 files changed

+31
-50
lines changed

packages/browser/src/profiling/hubextensions.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
JSSelfProfile,
99
JSSelfProfiler,
1010
JSSelfProfilerConstructor,
11-
ProcessedJSSelfProfile,
1211
} from './jsSelfProfiling';
1312
import { addProfileToMap,isValidSampleRate } from './utils';
1413

@@ -163,7 +162,7 @@ export function wrapTransactionWithProfiling(transaction: Transaction): Transact
163162
// event of an error or user mistake (calling transaction.finish multiple times), it is important that the behavior of onProfileHandler
164163
// is idempotent as we do not want any timings or profiles to be overriden by the last call to onProfileHandler.
165164
// After the original finish method is called, the event will be reported through the integration and delegated to transport.
166-
const processedProfile: ProcessedJSSelfProfile | null = null;
165+
const processedProfile: JSSelfProfile | null = null;
167166

168167
/**
169168
* Idempotent handler for profile stop
@@ -211,19 +210,7 @@ export function wrapTransactionWithProfiling(transaction: Transaction): Transact
211210
return null;
212211
}
213212

214-
// If a profile has less than 2 samples, it is not useful and should be discarded.
215-
if (p.samples.length < 2) {
216-
return null;
217-
}
218-
219-
// Discard a profile if it has no frames - happens if code was idling or sampling
220-
// did not collect any frames.
221-
if(!p.frames.length){
222-
return null;
223-
}
224-
225-
226-
addProfileToMap({...p, profile_id: profileId });
213+
addProfileToMap(profileId, p);
227214
return null;
228215
})
229216
.catch(error => {

packages/browser/src/profiling/integration.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ export class BrowserProfilingIntegration implements Integration {
5858
profiledTransaction.contexts['profile']['profile_id'] as string
5959

6060
if (!profile_id) {
61-
throw new TypeError('[Profiling] cannot find profile for a transaction without a profile context');
61+
__DEBUG_BUILD__ && logger.log('[Profiling] cannot find profile for a transaction without a profile context');
62+
continue;
6263
}
6364

6465
// Remove the profile from the transaction context before sending, relay will take care of the rest.
65-
if (profiledTransaction && profiledTransaction.contexts && profiledTransaction.contexts['.profile']) {
66+
if (profiledTransaction && profiledTransaction.contexts && profiledTransaction.contexts['profile']) {
6667
delete profiledTransaction.contexts.profile;
6768
}
6869

@@ -73,8 +74,8 @@ export class BrowserProfilingIntegration implements Integration {
7374
}
7475

7576
PROFILE_MAP.delete(profile_id);
76-
const profileEvent = createProfilingEvent(profile, profiledTransaction as ProfiledEvent);
77-
77+
const profileEvent = createProfilingEvent(profile_id, profile, profiledTransaction as ProfiledEvent);
78+
7879
if (profileEvent) {
7980
profilesToAddToEnvelope.push(profileEvent);
8081
}

packages/browser/src/profiling/jsSelfProfiling.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ export type JSSelfProfile = {
2626
samples: JSSelfProfileSample[];
2727
};
2828

29-
export interface ProcessedJSSelfProfile extends JSSelfProfile {
30-
profile_id: string;
31-
}
32-
3329
type BufferFullCallback = (trace: JSSelfProfile) => void;
3430

3531
export interface JSSelfProfiler {
@@ -50,6 +46,3 @@ declare global {
5046
}
5147
}
5248

53-
export interface RawThreadCpuProfile extends JSSelfProfile {
54-
profile_id: string;
55-
}

packages/browser/src/profiling/utils.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Profile, ThreadCpuProfile } from '@sentry/types/src/profiling';
66
import { forEachEnvelopeItem, GLOBAL_OBJ, logger, uuid4 } from '@sentry/utils';
77

88
import { WINDOW } from '../helpers';
9-
import type { JSSelfProfile, JSSelfProfileStack, ProcessedJSSelfProfile } from './jsSelfProfiling';
9+
import type { JSSelfProfile, JSSelfProfileStack } from './jsSelfProfiling';
1010

1111
const MS_TO_NS = 1e6;
1212
// Use 0 as main thread id which is identical to threadId in node:worker_threads
@@ -64,9 +64,7 @@ if (isUserAgentData(userAgentData)) {
6464
.catch(e => void e);
6565
}
6666

67-
function isProcessedJSSelfProfile(
68-
profile: ThreadCpuProfile | ProcessedJSSelfProfile,
69-
): profile is ProcessedJSSelfProfile {
67+
function isProcessedJSSelfProfile(profile: ThreadCpuProfile | JSSelfProfile): profile is JSSelfProfile {
7068
return !('thread_metadata' in profile);
7169
}
7270

@@ -75,7 +73,7 @@ function isProcessedJSSelfProfile(
7573
/**
7674
*
7775
*/
78-
export function enrichWithThreadInformation(profile: ThreadCpuProfile | ProcessedJSSelfProfile): ThreadCpuProfile {
76+
export function enrichWithThreadInformation(profile: ThreadCpuProfile | JSSelfProfile): ThreadCpuProfile {
7977
if (!isProcessedJSSelfProfile(profile)) {
8078
return profile;
8179
}
@@ -87,7 +85,7 @@ export function enrichWithThreadInformation(profile: ThreadCpuProfile | Processe
8785
// by the integration before the event is processed by other integrations.
8886
export interface ProfiledEvent extends Event {
8987
sdkProcessingMetadata: {
90-
profile?: ProcessedJSSelfProfile;
88+
profile?: JSSelfProfile;
9189
};
9290
}
9391

@@ -120,7 +118,11 @@ function getTraceId(event: Event): string {
120118
/**
121119
* Creates a profiling event envelope from a Sentry event.
122120
*/
123-
export function createProfilePayload(event: ProfiledEvent, processedProfile: ProcessedJSSelfProfile): Profile {
121+
export function createProfilePayload(
122+
event: ProfiledEvent,
123+
processedProfile: JSSelfProfile,
124+
profile_id: string,
125+
): Profile {
124126
if (event.type !== 'transaction') {
125127
// createProfilingEventEnvelope should only be called for transactions,
126128
// we type guard this behavior with isProfiledTransactionEvent.
@@ -133,17 +135,13 @@ export function createProfilePayload(event: ProfiledEvent, processedProfile: Pro
133135
);
134136
}
135137

136-
if (!processedProfile.profile_id) {
137-
throw new TypeError('Profile is missing profile_id');
138-
}
139-
140138
const traceId = getTraceId(event);
141139
const enrichedThreadProfile = enrichWithThreadInformation(processedProfile);
142140
const transactionStartMs = typeof event.start_timestamp === 'number' ? event.start_timestamp * 1000 : Date.now();
143141
const transactionEndMs = typeof event.timestamp === 'number' ? event.timestamp * 1000 : Date.now();
144142

145143
const profile: Profile = {
146-
event_id: processedProfile.profile_id,
144+
event_id: profile_id,
147145
timestamp: new Date(transactionStartMs).toISOString(),
148146
platform: 'javascript',
149147
version: '1',
@@ -420,8 +418,8 @@ export function isValidSampleRate(rate: unknown): boolean {
420418
return true;
421419
}
422420

423-
function isValidProfile(profile: ProcessedJSSelfProfile): profile is ProcessedJSSelfProfile & { profile_id: string } {
424-
if (profile.samples.length <= 1) {
421+
function isValidProfile(profile: JSSelfProfile): profile is JSSelfProfile & { profile_id: string } {
422+
if (profile.samples.length < 2) {
425423
if (__DEBUG_BUILD__) {
426424
// Log a warning if the profile has less than 2 samples so users can know why
427425
// they are not seeing any profiling data and we cant avoid the back and forth
@@ -431,7 +429,10 @@ function isValidProfile(profile: ProcessedJSSelfProfile): profile is ProcessedJS
431429
return false;
432430
}
433431

434-
if (!profile.profile_id) {
432+
if (!profile.frames.length) {
433+
if(__DEBUG_BUILD__) {
434+
logger.log('[Profiling] Discarding profile because it contains no frames');
435+
}
435436
return false;
436437
}
437438

@@ -443,24 +444,23 @@ function isValidProfile(profile: ProcessedJSSelfProfile): profile is ProcessedJS
443444
* @param event
444445
* @returns {Profile | null}
445446
*/
446-
export function createProfilingEvent(profile: ProcessedJSSelfProfile, event: ProfiledEvent): Profile | null {
447+
export function createProfilingEvent(profile_id: string, profile: JSSelfProfile, event: ProfiledEvent): Profile | null {
447448
if (!isValidProfile(profile)) {
448449
return null;
449450
}
450451

451-
return createProfilePayload(event, profile);
452+
return createProfilePayload(event, profile, profile_id);
452453
}
453454

454-
455-
export const PROFILE_MAP: Map<string, ProcessedJSSelfProfile> = new Map();
455+
export const PROFILE_MAP: Map<string, JSSelfProfile> = new Map();
456456
/**
457457
*
458458
*/
459-
export function addProfileToMap(profile: ProcessedJSSelfProfile): void{
460-
PROFILE_MAP.set(profile.profile_id, profile);
459+
export function addProfileToMap(profile_id: string, profile: JSSelfProfile): void {
460+
PROFILE_MAP.set(profile_id, profile);
461461

462-
if(PROFILE_MAP.size > 30){
463-
const last: string = [...PROFILE_MAP.keys()].pop() as string;
462+
if (PROFILE_MAP.size > 30) {
463+
const last: string = PROFILE_MAP.keys().next().value;
464464
PROFILE_MAP.delete(last);
465465
}
466-
}
466+
}

0 commit comments

Comments
 (0)