@@ -5,7 +5,7 @@ import type Transition from '@ember/routing/-private/transition';
55import type RouterService from '@ember/routing/router-service' ;
66import { _backburner , run , scheduleOnce } from '@ember/runloop' ;
77import type { EmberRunQueues } from '@ember/runloop/-private/types' ;
8- import { getOwnConfig , isTesting , macroCondition } from '@embroider/macros' ;
8+ import { getOwnConfig , importSync , isTesting , macroCondition } from '@embroider/macros' ;
99import type {
1010 BrowserClient ,
1111 startBrowserTracingNavigationSpan as startBrowserTracingNavigationSpanType ,
@@ -22,6 +22,7 @@ import type { Span } from '@sentry/core';
2222import { addIntegration , browserPerformanceTimeOrigin , GLOBAL_OBJ , timestampInSeconds } from '@sentry/core' ;
2323import type { ExtendedBackburner } from '@sentry/ember/runloop' ;
2424import type { EmberRouterMain , EmberSentryConfig , GlobalConfig , OwnConfig } from '../types' ;
25+ import type { browserTracingIntegration as browserTracingIntegrationType } from './browserTracingIntegration' ;
2526
2627function getSentryConfig ( ) : EmberSentryConfig {
2728 const _global = GLOBAL_OBJ as typeof GLOBAL_OBJ & GlobalConfig ;
@@ -84,17 +85,15 @@ export function _getLocationURL(location: EmberRouterMain['location']): string {
8485export function _instrumentEmberRouter (
8586 routerService : RouterService ,
8687 routerMain : EmberRouterMain ,
87- config : EmberSentryConfig ,
88+ config : { disableRunloopPerformance ?: boolean ; instrumentPageLoad ?: boolean ; instrumentNavigation ?: boolean } ,
8889 startBrowserTracingPageLoadSpan : typeof startBrowserTracingPageLoadSpanType ,
8990 startBrowserTracingNavigationSpan : typeof startBrowserTracingNavigationSpanType ,
9091) : void {
91- const { disableRunloopPerformance } = config ;
92+ const { disableRunloopPerformance, instrumentPageLoad , instrumentNavigation } = config ;
9293 const location = routerMain . location ;
9394 let activeRootSpan : Span | undefined ;
9495 let transitionSpan : Span | undefined ;
9596
96- // Maintaining backwards compatibility with config.browserTracingOptions, but passing it with Sentry options is preferred.
97- const browserTracingOptions = config . browserTracingOptions || config . sentry . browserTracingOptions || { } ;
9897 const url = _getLocationURL ( location ) ;
9998
10099 const client = getClient < BrowserClient > ( ) ;
@@ -103,7 +102,7 @@ export function _instrumentEmberRouter(
103102 return ;
104103 }
105104
106- if ( url && browserTracingOptions . instrumentPageLoad !== false ) {
105+ if ( url && instrumentPageLoad !== false ) {
107106 const routeInfo = routerService . recognize ( url ) ;
108107 activeRootSpan = startBrowserTracingPageLoadSpan ( client , {
109108 name : `route:${ routeInfo . name } ` ,
@@ -124,7 +123,7 @@ export function _instrumentEmberRouter(
124123 getBackburner ( ) . off ( 'end' , finishActiveTransaction ) ;
125124 } ;
126125
127- if ( browserTracingOptions . instrumentNavigation === false ) {
126+ if ( instrumentNavigation === false ) {
128127 return ;
129128 }
130129
@@ -407,32 +406,31 @@ function _hasPerformanceSupport(): { HAS_PERFORMANCE: boolean; HAS_PERFORMANCE_T
407406 } ;
408407}
409408
410- export async function instrumentForPerformance ( appInstance : ApplicationInstance ) : Promise < void > {
409+ export function instrumentForPerformance ( appInstance : ApplicationInstance ) : void {
411410 const config = getSentryConfig ( ) ;
412411 // Maintaining backwards compatibility with config.browserTracingOptions, but passing it with Sentry options is preferred.
413412 const browserTracingOptions = config . browserTracingOptions || config . sentry . browserTracingOptions || { } ;
414413
415- const { browserTracingIntegration, startBrowserTracingNavigationSpan, startBrowserTracingPageLoadSpan } =
416- await import ( '@sentry/browser' ) ;
414+ const { browserTracingIntegration } = importSync ( './browserTracingIntegration' ) as {
415+ browserTracingIntegration : typeof browserTracingIntegrationType ;
416+ } ;
417417
418418 const idleTimeout = config . transitionTimeout || 5000 ;
419419
420420 const browserTracing = browserTracingIntegration ( {
421+ appInstance,
421422 idleTimeout,
422423 ...browserTracingOptions ,
423- instrumentNavigation : false ,
424- instrumentPageLoad : false ,
425424 } ) ;
426425
427426 const client = getClient < BrowserClient > ( ) ;
428- const isAlreadyInitialized = macroCondition ( isTesting ( ) ) ? ! ! client ?. getIntegrationByName ( 'BrowserTracing' ) : false ;
427+ const isAlreadyInitialized = macroCondition ( isTesting ( ) ) && client ?. getIntegrationByName ( 'BrowserTracing' ) ;
429428 addIntegration ( browserTracing ) ;
430429
431- // We _always_ call this, as it triggers the page load & navigation spans
432- _instrumentNavigation ( appInstance , config , startBrowserTracingPageLoadSpan , startBrowserTracingNavigationSpan ) ;
433-
434430 // Skip instrumenting the stuff below again in tests, as these are not reset between tests
435- if ( isAlreadyInitialized ) {
431+ if ( isAlreadyInitialized && client ) {
432+ // Ensure this is re-run in tests even if the integration is already initialized
433+ browserTracing . afterAllSetup ?.( client ) ;
436434 return ;
437435 }
438436
@@ -441,9 +439,9 @@ export async function instrumentForPerformance(appInstance: ApplicationInstance)
441439 _instrumentInitialLoad ( config ) ;
442440}
443441
444- function _instrumentNavigation (
442+ export function instrumentEmberAppInstanceForPerformance (
445443 appInstance : ApplicationInstance ,
446- config : EmberSentryConfig ,
444+ config : { disableRunloopPerformance ?: boolean ; instrumentPageLoad ?: boolean ; instrumentNavigation ?: boolean } ,
447445 startBrowserTracingPageLoadSpan : typeof startBrowserTracingPageLoadSpanType ,
448446 startBrowserTracingNavigationSpan : typeof startBrowserTracingNavigationSpanType ,
449447) : void {
0 commit comments