import { addIntegration, browserTracingIntegration as originalBrowserTracingIntegration, startBrowserTracingNavigationSpan, startBrowserTracingPageLoadSpan, } from '@sentry/browser'; import type { Integration } from '@sentry/core'; import { defineIntegration } from '@sentry/core'; import type ApplicationInstance from '@ember/application/instance'; import { instrumentEmberAppInstanceForPerformance } from './instrumentEmberAppInstanceForPerformance.ts'; import { instrumentGlobalsForPerformance } from './instrumentEmberGlobals.ts'; import { isTesting, macroCondition } from '@embroider/macros'; type EmberBrowserTracingIntegrationOptions = Parameters[0] & { appInstance: ApplicationInstance; disableRunloopPerformance?: boolean; minimumRunloopQueueDuration?: number; disableInstrumentComponents?: boolean; minimumComponentRenderDuration?: number; enableComponentDefinitions?: boolean; disableInitialLoadInstrumentation?: boolean; }; let _initialized = false; function _browserTracingIntegration(options: EmberBrowserTracingIntegrationOptions): Integration { const { appInstance } = options; const instrumentNavigation = options.instrumentNavigation ?? true; const instrumentPageLoad = options.instrumentPageLoad ?? true; const integration = originalBrowserTracingIntegration({ ...options, instrumentNavigation: false, instrumentPageLoad: false, }); const appInstancePerformanceConfig = { disableRunloopPerformance: options.disableRunloopPerformance ?? false, instrumentPageLoad, instrumentNavigation, }; const globalsPerformanceConfig = { disableRunloopPerformance: options.disableRunloopPerformance ?? false, minimumRunloopQueueDuration: options.minimumRunloopQueueDuration, disableInstrumentComponents: options.disableInstrumentComponents ?? false, minimumComponentRenderDuration: options.minimumComponentRenderDuration, enableComponentDefinitions: options.enableComponentDefinitions ?? false, disableInitialLoadInstrumentation: options.disableInitialLoadInstrumentation ?? false, }; return { ...integration, afterAllSetup(client) { integration.afterAllSetup(client); instrumentEmberAppInstanceForPerformance( client, appInstance, appInstancePerformanceConfig, startBrowserTracingPageLoadSpan, startBrowserTracingNavigationSpan, ); // We only want to run this once in tests! if (macroCondition(isTesting())) { if (_initialized) { return; } } instrumentGlobalsForPerformance(globalsPerformanceConfig); _initialized = true; }, }; } export const browserTracingIntegration: typeof _browserTracingIntegration = defineIntegration(_browserTracingIntegration); /** * Utility to simplify adding the browser tracing integration to an app instance. */ export function instrumentAppInstancePerformance( appInstance: ApplicationInstance, options?: Partial[0]>, ): void { // Disable in FastBoot — we only want to run Sentry client-side const fastboot = appInstance.lookup('service:fastboot') as { isFastBoot: boolean } | undefined; if (fastboot?.isFastBoot) { return; } addIntegration( browserTracingIntegration({ ...options, appInstance, }), ); }