11import ApplicationInstance from '@ember/application/instance' ;
2- import Ember from 'ember' ;
32import { run , _backburner , scheduleOnce } from '@ember/runloop' ;
3+ import { subscribe } from '@ember/instrumentation' ;
44import * as Sentry from '@sentry/browser' ;
55import { ExtendedBackburner } from '@sentry/ember/runloop' ;
66import { Span , Transaction , Integration } from '@sentry/types' ;
@@ -9,6 +9,7 @@ import { getActiveTransaction } from '..';
99import { browserPerformanceTimeOrigin , GLOBAL_OBJ , timestampWithMs } from '@sentry/utils' ;
1010import { macroCondition , isTesting , getOwnConfig } from '@embroider/macros' ;
1111import { EmberSentryConfig , GlobalConfig , OwnConfig } from '../types' ;
12+ import RouterService from '@ember/routing/router-service' ;
1213
1314function getSentryConfig ( ) {
1415 const _global = GLOBAL_OBJ as typeof GLOBAL_OBJ & GlobalConfig ;
@@ -279,7 +280,6 @@ function _instrumentComponents(config: EmberSentryConfig) {
279280 const beforeEntries = { } as RenderEntries ;
280281 const beforeComponentDefinitionEntries = { } as RenderEntries ;
281282
282- const subscribe = Ember . subscribe ;
283283 function _subscribeToRenderEvents ( ) {
284284 subscribe ( 'render.component' , {
285285 before ( _name : string , _timestamp : number , payload : Payload ) {
@@ -309,22 +309,21 @@ function _instrumentInitialLoad(config: EmberSentryConfig) {
309309 const startName = '@sentry/ember:initial-load-start' ;
310310 const endName = '@sentry/ember:initial-load-end' ;
311311
312- const { performance } = window ;
313- const HAS_PERFORMANCE = performance && performance . clearMarks && performance . clearMeasures ;
312+ let { HAS_PERFORMANCE , HAS_PERFORMANCE_TIMING } = _hasPerformanceSupport ( ) ;
314313
315314 if ( ! HAS_PERFORMANCE ) {
316315 return ;
317316 }
318317
318+ const { performance } = window ;
319+
319320 if ( config . disableInitialLoadInstrumentation ) {
320321 performance . clearMarks ( startName ) ;
321322 performance . clearMarks ( endName ) ;
322323 return ;
323324 }
324325
325326 // Split performance check in two so clearMarks still happens even if timeOrigin isn't available.
326- const HAS_PERFORMANCE_TIMING =
327- performance . measure && performance . getEntriesByName && browserPerformanceTimeOrigin !== undefined ;
328327 if ( ! HAS_PERFORMANCE_TIMING ) {
329328 return ;
330329 }
@@ -355,6 +354,26 @@ function _instrumentInitialLoad(config: EmberSentryConfig) {
355354 performance . clearMeasures ( measureName ) ;
356355}
357356
357+ function _hasPerformanceSupport ( ) {
358+ // TS says that all of these methods are always available, but some of them may not be supported in older browsers
359+ // So we "pretend" they are all optional in order to be able to check this properly without TS complaining
360+ const _performance = window . performance as {
361+ clearMarks ?: Performance [ 'clearMarks' ] ;
362+ clearMeasures ?: Performance [ 'clearMeasures' ] ;
363+ measure ?: Performance [ 'measure' ] ;
364+ getEntriesByName ?: Performance [ 'getEntriesByName' ] ;
365+ } ;
366+ const HAS_PERFORMANCE = Boolean ( _performance && _performance . clearMarks && _performance . clearMeasures ) ;
367+ const HAS_PERFORMANCE_TIMING = Boolean (
368+ _performance . measure && _performance . getEntriesByName && browserPerformanceTimeOrigin !== undefined ,
369+ ) ;
370+
371+ return {
372+ HAS_PERFORMANCE ,
373+ HAS_PERFORMANCE_TIMING ,
374+ } ;
375+ }
376+
358377export async function instrumentForPerformance ( appInstance : ApplicationInstance ) {
359378 const config = getSentryConfig ( ) ;
360379 const sentryConfig = config . sentry ;
@@ -372,7 +391,9 @@ export async function instrumentForPerformance(appInstance: ApplicationInstance)
372391 new tracing . Integrations . BrowserTracing ( {
373392 routingInstrumentation : ( customStartTransaction , startTransactionOnPageLoad ) => {
374393 const routerMain = appInstance . lookup ( 'router:main' ) ;
375- let routerService = appInstance . lookup ( 'service:router' ) ;
394+ let routerService = appInstance . lookup ( 'service:router' ) as
395+ | RouterService & { externalRouter ?: RouterService ; _hasMountedSentryPerformanceRouting ?: boolean } ;
396+
376397 if ( routerService . externalRouter ) {
377398 // Using ember-engines-router-service in an engine.
378399 routerService = routerService . externalRouter ;
0 commit comments