|
| 1 | +import type ApplicationInstance from '@ember/application/instance'; |
| 2 | +import type Transition from '@ember/routing/-private/transition'; |
| 3 | +import type RouterService from '@ember/routing/router-service'; |
| 4 | +import type { |
| 5 | + BrowserClient, |
| 6 | + startBrowserTracingNavigationSpan as startBrowserTracingNavigationSpanType, |
| 7 | + startBrowserTracingPageLoadSpan as startBrowserTracingPageLoadSpanType, |
| 8 | +} from '@sentry/browser'; |
| 9 | +import { |
| 10 | + getClient, |
| 11 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 12 | + SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
| 13 | + startInactiveSpan, |
| 14 | +} from '@sentry/browser'; |
| 15 | +import type { Span } from '@sentry/core'; |
| 16 | +import type { EmberRouterMain } from '../types'; |
| 17 | +import { getBackburner } from './performance'; |
| 18 | + |
| 19 | +export function instrumentEmberAppInstanceForPerformance( |
| 20 | + appInstance: ApplicationInstance, |
| 21 | + config: { disableRunloopPerformance?: boolean; instrumentPageLoad?: boolean; instrumentNavigation?: boolean }, |
| 22 | + startBrowserTracingPageLoadSpan: typeof startBrowserTracingPageLoadSpanType, |
| 23 | + startBrowserTracingNavigationSpan: typeof startBrowserTracingNavigationSpanType, |
| 24 | +): void { |
| 25 | + // eslint-disable-next-line ember/no-private-routing-service |
| 26 | + const routerMain = appInstance.lookup('router:main') as EmberRouterMain; |
| 27 | + let routerService = appInstance.lookup('service:router') as RouterService & { |
| 28 | + externalRouter?: RouterService; |
| 29 | + _hasMountedSentryPerformanceRouting?: boolean; |
| 30 | + }; |
| 31 | + |
| 32 | + if (routerService.externalRouter) { |
| 33 | + // Using ember-engines-router-service in an engine. |
| 34 | + routerService = routerService.externalRouter; |
| 35 | + } |
| 36 | + if (routerService._hasMountedSentryPerformanceRouting) { |
| 37 | + // Routing listens to route changes on the main router, and should not be initialized multiple times per page. |
| 38 | + return; |
| 39 | + } |
| 40 | + if (!routerService.recognize) { |
| 41 | + // Router is missing critical functionality to limit cardinality of the transaction names. |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + routerService._hasMountedSentryPerformanceRouting = true; |
| 46 | + _instrumentEmberRouter( |
| 47 | + routerService, |
| 48 | + routerMain, |
| 49 | + config, |
| 50 | + startBrowserTracingPageLoadSpan, |
| 51 | + startBrowserTracingNavigationSpan, |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function getTransitionInformation( |
| 56 | + transition: Transition | undefined, |
| 57 | + router: RouterService, |
| 58 | +): { fromRoute?: string; toRoute?: string } { |
| 59 | + const fromRoute = transition?.from?.name; |
| 60 | + const toRoute = transition?.to?.name || router.currentRouteName; |
| 61 | + return { |
| 62 | + fromRoute, |
| 63 | + toRoute, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +// Only exported for testing |
| 68 | +export function _getLocationURL(location: EmberRouterMain['location']): string { |
| 69 | + if (!location?.getURL || !location?.formatURL) { |
| 70 | + return ''; |
| 71 | + } |
| 72 | + const url = location.formatURL(location.getURL()); |
| 73 | + |
| 74 | + // `implementation` is optional in Ember's predefined location types, so we also check if the URL starts with '#'. |
| 75 | + if (location.implementation === 'hash' || url.startsWith('#')) { |
| 76 | + return `${location.rootURL}${url}`; |
| 77 | + } |
| 78 | + return url; |
| 79 | +} |
| 80 | + |
| 81 | +function _instrumentEmberRouter( |
| 82 | + routerService: RouterService, |
| 83 | + routerMain: EmberRouterMain, |
| 84 | + config: { disableRunloopPerformance?: boolean; instrumentPageLoad?: boolean; instrumentNavigation?: boolean }, |
| 85 | + startBrowserTracingPageLoadSpan: typeof startBrowserTracingPageLoadSpanType, |
| 86 | + startBrowserTracingNavigationSpan: typeof startBrowserTracingNavigationSpanType, |
| 87 | +): void { |
| 88 | + const { disableRunloopPerformance, instrumentPageLoad, instrumentNavigation } = config; |
| 89 | + const location = routerMain.location; |
| 90 | + let activeRootSpan: Span | undefined; |
| 91 | + let transitionSpan: Span | undefined; |
| 92 | + |
| 93 | + const url = _getLocationURL(location); |
| 94 | + |
| 95 | + const client = getClient<BrowserClient>(); |
| 96 | + |
| 97 | + if (!client) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + if (url && instrumentPageLoad !== false) { |
| 102 | + const routeInfo = routerService.recognize(url); |
| 103 | + activeRootSpan = startBrowserTracingPageLoadSpan(client, { |
| 104 | + name: `route:${routeInfo.name}`, |
| 105 | + attributes: { |
| 106 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', |
| 107 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.ember', |
| 108 | + url, |
| 109 | + toRoute: routeInfo.name, |
| 110 | + }, |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + const finishActiveTransaction = (_: unknown, nextInstance: unknown): void => { |
| 115 | + if (nextInstance) { |
| 116 | + return; |
| 117 | + } |
| 118 | + activeRootSpan?.end(); |
| 119 | + getBackburner().off('end', finishActiveTransaction); |
| 120 | + }; |
| 121 | + |
| 122 | + if (instrumentNavigation === false) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + routerService.on('routeWillChange', (transition: Transition) => { |
| 127 | + const { fromRoute, toRoute } = getTransitionInformation(transition, routerService); |
| 128 | + |
| 129 | + // We want to ignore loading && error routes |
| 130 | + if (transitionIsIntermediate(transition)) { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + activeRootSpan?.end(); |
| 135 | + |
| 136 | + activeRootSpan = startBrowserTracingNavigationSpan(client, { |
| 137 | + name: `route:${toRoute}`, |
| 138 | + attributes: { |
| 139 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', |
| 140 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.ember', |
| 141 | + fromRoute, |
| 142 | + toRoute, |
| 143 | + }, |
| 144 | + }); |
| 145 | + |
| 146 | + transitionSpan = startInactiveSpan({ |
| 147 | + attributes: { |
| 148 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.ember', |
| 149 | + }, |
| 150 | + op: 'ui.ember.transition', |
| 151 | + name: `route:${fromRoute} -> route:${toRoute}`, |
| 152 | + onlyIfParent: true, |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + routerService.on('routeDidChange', transition => { |
| 157 | + if (!transitionSpan || !activeRootSpan || transitionIsIntermediate(transition)) { |
| 158 | + return; |
| 159 | + } |
| 160 | + transitionSpan.end(); |
| 161 | + |
| 162 | + if (disableRunloopPerformance) { |
| 163 | + activeRootSpan.end(); |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + getBackburner().on('end', finishActiveTransaction); |
| 168 | + }); |
| 169 | +} |
| 170 | + |
| 171 | +function transitionIsIntermediate(transition: Transition): boolean { |
| 172 | + // We want to use ignore, as this may actually be defined on new versions |
| 173 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 174 | + // @ts-ignore This actually exists on newer versions |
| 175 | + const isIntermediate: boolean | undefined = transition.isIntermediate; |
| 176 | + |
| 177 | + if (typeof isIntermediate === 'boolean') { |
| 178 | + return isIntermediate; |
| 179 | + } |
| 180 | + |
| 181 | + // For versions without this, we look if the route is a `.loading` or `.error` route |
| 182 | + // This is not perfect and may false-positive in some cases, but it's the best we can do |
| 183 | + return transition.to?.localName === 'loading' || transition.to?.localName === 'error'; |
| 184 | +} |
0 commit comments