123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import { spanToJSON } from '@sentry/core';
- import { logger, timestampInSeconds, uuid4 } from '@sentry/utils';
- import { DEBUG_BUILD } from '../debug-build.js';
- import { WINDOW } from '../helpers.js';
- import { shouldProfileTransaction, isAutomatedPageLoadTransaction, startJSSelfProfile, MAX_PROFILE_DURATION_MS, addProfileToGlobalCache } from './utils.js';
- function onProfilingStartRouteTransaction(transaction) {
- if (!transaction) {
- if (DEBUG_BUILD) {
- logger.log('[Profiling] Transaction is undefined, skipping profiling');
- }
- return transaction;
- }
- if (shouldProfileTransaction(transaction)) {
- return startProfileForTransaction(transaction);
- }
- return transaction;
- }
- function startProfileForTransaction(transaction) {
-
- let startTimestamp;
- if (isAutomatedPageLoadTransaction(transaction)) {
- startTimestamp = timestampInSeconds() * 1000;
- }
- const profiler = startJSSelfProfile();
-
-
- if (!profiler) {
- return transaction;
- }
- if (DEBUG_BUILD) {
- logger.log(`[Profiling] started profiling transaction: ${spanToJSON(transaction).description}`);
- }
-
-
-
- const profileId = uuid4();
-
- async function onProfileHandler() {
-
- if (!transaction) {
- return null;
- }
-
- if (!profiler) {
- return null;
- }
- return profiler
- .stop()
- .then((profile) => {
- if (maxDurationTimeoutID) {
- WINDOW.clearTimeout(maxDurationTimeoutID);
- maxDurationTimeoutID = undefined;
- }
- if (DEBUG_BUILD) {
- logger.log(`[Profiling] stopped profiling of transaction: ${spanToJSON(transaction).description}`);
- }
-
- if (!profile) {
- if (DEBUG_BUILD) {
- logger.log(
- `[Profiling] profiler returned null profile for: ${spanToJSON(transaction).description}`,
- 'this may indicate an overlapping transaction or a call to stopProfiling with a profile title that was never started',
- );
- }
- return null;
- }
- addProfileToGlobalCache(profileId, profile);
- return null;
- })
- .catch(error => {
- if (DEBUG_BUILD) {
- logger.log('[Profiling] error while stopping profiler:', error);
- }
- return null;
- });
- }
-
- let maxDurationTimeoutID = WINDOW.setTimeout(() => {
- if (DEBUG_BUILD) {
- logger.log(
- '[Profiling] max profile duration elapsed, stopping profiling for:',
- spanToJSON(transaction).description,
- );
- }
-
-
- onProfileHandler();
- }, MAX_PROFILE_DURATION_MS);
-
- const originalEnd = transaction.end.bind(transaction);
-
- function profilingWrappedTransactionEnd() {
- if (!transaction) {
- return originalEnd();
- }
-
-
- void onProfileHandler().then(
- () => {
-
-
- transaction.setContext('profile', { profile_id: profileId, start_timestamp: startTimestamp });
- originalEnd();
- },
- () => {
-
- originalEnd();
- },
- );
- return transaction;
- }
- transaction.end = profilingWrappedTransactionEnd;
- return transaction;
- }
- export { onProfilingStartRouteTransaction, startProfileForTransaction };
|