-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathutils.ts
572 lines (498 loc) · 18.2 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* eslint-disable max-lines */
import { DEFAULT_ENVIRONMENT, getClient, spanToJSON } from '@sentry/core';
import type { DebugImage, Envelope, Event, EventEnvelope, Profile, Span, ThreadCpuProfile } from '@sentry/types';
import {
browserPerformanceTimeOrigin,
forEachEnvelopeItem,
getDebugImagesForResources,
logger,
timestampInSeconds,
uuid4,
} from '@sentry/utils';
import { DEBUG_BUILD } from '../debug-build';
import { WINDOW } from '../helpers';
import type { JSSelfProfile, JSSelfProfileStack, JSSelfProfiler, JSSelfProfilerConstructor } from './jsSelfProfiling';
const MS_TO_NS = 1e6;
// Use 0 as main thread id which is identical to threadId in node:worker_threads
// where main logs 0 and workers seem to log in increments of 1
const THREAD_ID_STRING = String(0);
const THREAD_NAME = 'main';
// Machine properties (eval only once)
let OS_PLATFORM = '';
let OS_PLATFORM_VERSION = '';
let OS_ARCH = '';
let OS_BROWSER = (WINDOW.navigator && WINDOW.navigator.userAgent) || '';
let OS_MODEL = '';
const OS_LOCALE =
(WINDOW.navigator && WINDOW.navigator.language) ||
(WINDOW.navigator && WINDOW.navigator.languages && WINDOW.navigator.languages[0]) ||
'';
type UAData = {
platform?: string;
architecture?: string;
model?: string;
platformVersion?: string;
fullVersionList?: {
brand: string;
version: string;
}[];
};
interface UserAgentData {
getHighEntropyValues: (keys: string[]) => Promise<UAData>;
}
function isUserAgentData(data: unknown): data is UserAgentData {
return typeof data === 'object' && data !== null && 'getHighEntropyValues' in data;
}
// @ts-expect-error userAgentData is not part of the navigator interface yet
const userAgentData = WINDOW.navigator && WINDOW.navigator.userAgentData;
if (isUserAgentData(userAgentData)) {
userAgentData
.getHighEntropyValues(['architecture', 'model', 'platform', 'platformVersion', 'fullVersionList'])
.then((ua: UAData) => {
OS_PLATFORM = ua.platform || '';
OS_ARCH = ua.architecture || '';
OS_MODEL = ua.model || '';
OS_PLATFORM_VERSION = ua.platformVersion || '';
if (ua.fullVersionList && ua.fullVersionList.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const firstUa = ua.fullVersionList[ua.fullVersionList.length - 1]!;
OS_BROWSER = `${firstUa.brand} ${firstUa.version}`;
}
})
.catch(e => void e);
}
function isProcessedJSSelfProfile(profile: ThreadCpuProfile | JSSelfProfile): profile is JSSelfProfile {
return !('thread_metadata' in profile);
}
// Enriches the profile with threadId of the current thread.
// This is done in node as we seem to not be able to get the info from C native code.
/**
*
*/
export function enrichWithThreadInformation(profile: ThreadCpuProfile | JSSelfProfile): ThreadCpuProfile {
if (!isProcessedJSSelfProfile(profile)) {
return profile;
}
return convertJSSelfProfileToSampledFormat(profile);
}
// Profile is marked as optional because it is deleted from the metadata
// by the integration before the event is processed by other integrations.
export interface ProfiledEvent extends Event {
sdkProcessingMetadata: {
profile?: JSSelfProfile;
};
}
function getTraceId(event: Event): string {
const traceId: unknown = event && event.contexts && event.contexts['trace'] && event.contexts['trace']['trace_id'];
// Log a warning if the profile has an invalid traceId (should be uuidv4).
// All profiles and transactions are rejected if this is the case and we want to
// warn users that this is happening if they enable debug flag
if (typeof traceId === 'string' && traceId.length !== 32) {
if (DEBUG_BUILD) {
logger.log(`[Profiling] Invalid traceId: ${traceId} on profiled event`);
}
}
if (typeof traceId !== 'string') {
return '';
}
return traceId;
}
/**
* Creates a profiling event envelope from a Sentry event. If profile does not pass
* validation, returns null.
* @param event
* @param dsn
* @param metadata
* @param tunnel
* @returns {EventEnvelope | null}
*/
/**
* Creates a profiling event envelope from a Sentry event.
*/
export function createProfilePayload(
profile_id: string,
start_timestamp: number | undefined,
processed_profile: JSSelfProfile,
event: ProfiledEvent,
): Profile {
if (event.type !== 'transaction') {
// createProfilingEventEnvelope should only be called for transactions,
// we type guard this behavior with isProfiledTransactionEvent.
throw new TypeError('Profiling events may only be attached to transactions, this should never occur.');
}
if (processed_profile === undefined || processed_profile === null) {
throw new TypeError(
`Cannot construct profiling event envelope without a valid profile. Got ${processed_profile} instead.`,
);
}
const traceId = getTraceId(event);
const enrichedThreadProfile = enrichWithThreadInformation(processed_profile);
const transactionStartMs = start_timestamp
? start_timestamp
: typeof event.start_timestamp === 'number'
? event.start_timestamp * 1000
: timestampInSeconds() * 1000;
const transactionEndMs = typeof event.timestamp === 'number' ? event.timestamp * 1000 : timestampInSeconds() * 1000;
const profile: Profile = {
event_id: profile_id,
timestamp: new Date(transactionStartMs).toISOString(),
platform: 'javascript',
version: '1',
release: event.release || '',
environment: event.environment || DEFAULT_ENVIRONMENT,
runtime: {
name: 'javascript',
version: WINDOW.navigator.userAgent,
},
os: {
name: OS_PLATFORM,
version: OS_PLATFORM_VERSION,
build_number: OS_BROWSER,
},
device: {
locale: OS_LOCALE,
model: OS_MODEL,
manufacturer: OS_BROWSER,
architecture: OS_ARCH,
is_emulator: false,
},
debug_meta: {
images: applyDebugMetadata(processed_profile.resources),
},
profile: enrichedThreadProfile,
transactions: [
{
name: event.transaction || '',
id: event.event_id || uuid4(),
trace_id: traceId,
active_thread_id: THREAD_ID_STRING,
relative_start_ns: '0',
relative_end_ns: ((transactionEndMs - transactionStartMs) * 1e6).toFixed(0),
},
],
};
return profile;
}
/**
*
*/
export function isProfiledTransactionEvent(event: Event): event is ProfiledEvent {
return !!(event.sdkProcessingMetadata && event.sdkProcessingMetadata['profile']);
}
/*
See packages/browser-utils/src/browser/router.ts
*/
/**
*
*/
export function isAutomatedPageLoadSpan(span: Span): boolean {
return spanToJSON(span).op === 'pageload';
}
/**
* Converts a JSSelfProfile to a our sampled format.
* Does not currently perform stack indexing.
*/
export function convertJSSelfProfileToSampledFormat(input: JSSelfProfile): Profile['profile'] {
let EMPTY_STACK_ID: undefined | number = undefined;
let STACK_ID = 0;
// Initialize the profile that we will fill with data
const profile: Profile['profile'] = {
samples: [],
stacks: [],
frames: [],
thread_metadata: {
[THREAD_ID_STRING]: { name: THREAD_NAME },
},
};
const firstSample = input.samples[0];
if (!firstSample) {
return profile;
}
// We assert samples.length > 0 above and timestamp should always be present
const start = firstSample.timestamp;
// The JS SDK might change it's time origin based on some heuristic (see See packages/utils/src/time.ts)
// when that happens, we need to ensure we are correcting the profile timings so the two timelines stay in sync.
// Since JS self profiling time origin is always initialized to performance.timeOrigin, we need to adjust for
// the drift between the SDK selected value and our profile time origin.
const origin =
typeof performance.timeOrigin === 'number' ? performance.timeOrigin : browserPerformanceTimeOrigin || 0;
const adjustForOriginChange = origin - (browserPerformanceTimeOrigin || origin);
input.samples.forEach((jsSample, i) => {
// If sample has no stack, add an empty sample
if (jsSample.stackId === undefined) {
if (EMPTY_STACK_ID === undefined) {
EMPTY_STACK_ID = STACK_ID;
profile.stacks[EMPTY_STACK_ID] = [];
STACK_ID++;
}
profile['samples'][i] = {
// convert ms timestamp to ns
elapsed_since_start_ns: ((jsSample.timestamp + adjustForOriginChange - start) * MS_TO_NS).toFixed(0),
stack_id: EMPTY_STACK_ID,
thread_id: THREAD_ID_STRING,
};
return;
}
let stackTop: JSSelfProfileStack | undefined = input.stacks[jsSample.stackId];
// Functions in top->down order (root is last)
// We follow the stackTop.parentId trail and collect each visited frameId
const stack: number[] = [];
while (stackTop) {
stack.push(stackTop.frameId);
const frame = input.frames[stackTop.frameId];
// If our frame has not been indexed yet, index it
if (frame && profile.frames[stackTop.frameId] === undefined) {
profile.frames[stackTop.frameId] = {
function: frame.name,
abs_path: typeof frame.resourceId === 'number' ? input.resources[frame.resourceId] : undefined,
lineno: frame.line,
colno: frame.column,
};
}
stackTop = stackTop.parentId === undefined ? undefined : input.stacks[stackTop.parentId];
}
const sample: Profile['profile']['samples'][0] = {
// convert ms timestamp to ns
elapsed_since_start_ns: ((jsSample.timestamp + adjustForOriginChange - start) * MS_TO_NS).toFixed(0),
stack_id: STACK_ID,
thread_id: THREAD_ID_STRING,
};
profile['stacks'][STACK_ID] = stack;
profile['samples'][i] = sample;
STACK_ID++;
});
return profile;
}
/**
* Adds items to envelope if they are not already present - mutates the envelope.
* @param envelope
*/
export function addProfilesToEnvelope(envelope: EventEnvelope, profiles: Profile[]): Envelope {
if (!profiles.length) {
return envelope;
}
for (const profile of profiles) {
envelope[1].push([{ type: 'profile' }, profile]);
}
return envelope;
}
/**
* Finds transactions with profile_id context in the envelope
* @param envelope
* @returns
*/
export function findProfiledTransactionsFromEnvelope(envelope: Envelope): Event[] {
const events: Event[] = [];
forEachEnvelopeItem(envelope, (item, type) => {
if (type !== 'transaction') {
return;
}
for (let j = 1; j < item.length; j++) {
const event = item[j] as Event;
if (event && event.contexts && event.contexts['profile'] && event.contexts['profile']['profile_id']) {
events.push(item[j] as Event);
}
}
});
return events;
}
/**
* Applies debug meta data to an event from a list of paths to resources (sourcemaps)
*/
export function applyDebugMetadata(resource_paths: ReadonlyArray<string>): DebugImage[] {
const client = getClient();
const options = client && client.getOptions();
const stackParser = options && options.stackParser;
if (!stackParser) {
return [];
}
return getDebugImagesForResources(stackParser, resource_paths);
}
/**
* Checks the given sample rate to make sure it is valid type and value (a boolean, or a number between 0 and 1).
*/
export function isValidSampleRate(rate: unknown): boolean {
// we need to check NaN explicitly because it's of type 'number' and therefore wouldn't get caught by this typecheck
if ((typeof rate !== 'number' && typeof rate !== 'boolean') || (typeof rate === 'number' && isNaN(rate))) {
DEBUG_BUILD &&
logger.warn(
`[Profiling] Invalid sample rate. Sample rate must be a boolean or a number between 0 and 1. Got ${JSON.stringify(
rate,
)} of type ${JSON.stringify(typeof rate)}.`,
);
return false;
}
// Boolean sample rates are always valid
if (rate === true || rate === false) {
return true;
}
// in case sampleRate is a boolean, it will get automatically cast to 1 if it's true and 0 if it's false
if (rate < 0 || rate > 1) {
DEBUG_BUILD && logger.warn(`[Profiling] Invalid sample rate. Sample rate must be between 0 and 1. Got ${rate}.`);
return false;
}
return true;
}
function isValidProfile(profile: JSSelfProfile): profile is JSSelfProfile & { profile_id: string } {
if (profile.samples.length < 2) {
if (DEBUG_BUILD) {
// Log a warning if the profile has less than 2 samples so users can know why
// they are not seeing any profiling data and we cant avoid the back and forth
// of asking them to provide us with a dump of the profile data.
logger.log('[Profiling] Discarding profile because it contains less than 2 samples');
}
return false;
}
if (!profile.frames.length) {
if (DEBUG_BUILD) {
logger.log('[Profiling] Discarding profile because it contains no frames');
}
return false;
}
return true;
}
// Keep a flag value to avoid re-initializing the profiler constructor. If it fails
// once, it will always fail and this allows us to early return.
let PROFILING_CONSTRUCTOR_FAILED: boolean = false;
export const MAX_PROFILE_DURATION_MS = 30_000;
/**
* Check if profiler constructor is available.
* @param maybeProfiler
*/
function isJSProfilerSupported(maybeProfiler: unknown): maybeProfiler is typeof JSSelfProfilerConstructor {
return typeof maybeProfiler === 'function';
}
/**
* Starts the profiler and returns the profiler instance.
*/
export function startJSSelfProfile(): JSSelfProfiler | undefined {
// Feature support check first
const JSProfilerConstructor = WINDOW.Profiler;
if (!isJSProfilerSupported(JSProfilerConstructor)) {
if (DEBUG_BUILD) {
logger.log(
'[Profiling] Profiling is not supported by this browser, Profiler interface missing on window object.',
);
}
return;
}
// From initial testing, it seems that the minimum value for sampleInterval is 10ms.
const samplingIntervalMS = 10;
// Start the profiler
const maxSamples = Math.floor(MAX_PROFILE_DURATION_MS / samplingIntervalMS);
// Attempt to initialize the profiler constructor, if it fails, we disable profiling for the current user session.
// This is likely due to a missing 'Document-Policy': 'js-profiling' header. We do not want to throw an error if this happens
// as we risk breaking the user's application, so just disable profiling and log an error.
try {
return new JSProfilerConstructor({ sampleInterval: samplingIntervalMS, maxBufferSize: maxSamples });
} catch (e) {
if (DEBUG_BUILD) {
logger.log(
"[Profiling] Failed to initialize the Profiling constructor, this is likely due to a missing 'Document-Policy': 'js-profiling' header.",
);
logger.log('[Profiling] Disabling profiling for current user session.');
}
PROFILING_CONSTRUCTOR_FAILED = true;
}
return;
}
/**
* Determine if a profile should be profiled.
*/
export function shouldProfileSpan(span: Span): boolean {
// If constructor failed once, it will always fail, so we can early return.
if (PROFILING_CONSTRUCTOR_FAILED) {
if (DEBUG_BUILD) {
logger.log('[Profiling] Profiling has been disabled for the duration of the current user session.');
}
return false;
}
if (!span.isRecording()) {
if (DEBUG_BUILD) {
logger.log('[Profiling] Discarding profile because transaction was not sampled.');
}
return false;
}
const client = getClient();
const options = client && client.getOptions();
if (!options) {
DEBUG_BUILD && logger.log('[Profiling] Profiling disabled, no options found.');
return false;
}
// @ts-expect-error profilesSampleRate is not part of the browser options yet
const profilesSampleRate: number | boolean | undefined = options.profilesSampleRate;
// Since this is coming from the user (or from a function provided by the user), who knows what we might get. (The
// only valid values are booleans or numbers between 0 and 1.)
if (!isValidSampleRate(profilesSampleRate)) {
DEBUG_BUILD && logger.warn('[Profiling] Discarding profile because of invalid sample rate.');
return false;
}
// if the function returned 0 (or false), or if `profileSampleRate` is 0, it's a sign the profile should be dropped
if (!profilesSampleRate) {
DEBUG_BUILD &&
logger.log(
'[Profiling] Discarding profile because a negative sampling decision was inherited or profileSampleRate is set to 0',
);
return false;
}
// Now we roll the dice. Math.random is inclusive of 0, but not of 1, so strict < is safe here. In case sampleRate is
// a boolean, the < comparison will cause it to be automatically cast to 1 if it's true and 0 if it's false.
const sampled = profilesSampleRate === true ? true : Math.random() < profilesSampleRate;
// Check if we should sample this profile
if (!sampled) {
DEBUG_BUILD &&
logger.log(
`[Profiling] Discarding profile because it's not included in the random sample (sampling rate = ${Number(
profilesSampleRate,
)})`,
);
return false;
}
return true;
}
/**
* Creates a profiling envelope item, if the profile does not pass validation, returns null.
* @param event
* @returns {Profile | null}
*/
export function createProfilingEvent(
profile_id: string,
start_timestamp: number | undefined,
profile: JSSelfProfile,
event: ProfiledEvent,
): Profile | null {
if (!isValidProfile(profile)) {
return null;
}
return createProfilePayload(profile_id, start_timestamp, profile, event);
}
// TODO (v8): We need to obtain profile ids in @sentry-internal/tracing,
// but we don't have access to this map because importing this map would
// cause a circular dependency. We need to resolve this in v8.
const PROFILE_MAP: Map<string, JSSelfProfile> = new Map();
/**
*
*/
export function getActiveProfilesCount(): number {
return PROFILE_MAP.size;
}
/**
* Retrieves profile from global cache and removes it.
*/
export function takeProfileFromGlobalCache(profile_id: string): JSSelfProfile | undefined {
const profile = PROFILE_MAP.get(profile_id);
if (profile) {
PROFILE_MAP.delete(profile_id);
}
return profile;
}
/**
* Adds profile to global cache and evicts the oldest profile if the cache is full.
*/
export function addProfileToGlobalCache(profile_id: string, profile: JSSelfProfile): void {
PROFILE_MAP.set(profile_id, profile);
if (PROFILE_MAP.size > 30) {
const last: string = PROFILE_MAP.keys().next().value;
PROFILE_MAP.delete(last);
}
}