|
| 1 | +import ApplicationInstance from '@ember/application/instance'; |
| 2 | +import Ember from 'ember'; |
| 3 | +import { scheduleOnce } from '@ember/runloop'; |
| 4 | +import environmentConfig from 'ember-get-config'; |
| 5 | +import Sentry from '@sentry/browser'; |
| 6 | +import { Integration } from '@sentry/types'; |
| 7 | + |
| 8 | +export function initialize(appInstance: ApplicationInstance): void { |
| 9 | + const config = environmentConfig['@sentry/ember']; |
| 10 | + if (config['disablePerformance']) { |
| 11 | + return; |
| 12 | + } |
| 13 | + const performancePromise = instrumentForPerformance(appInstance); |
| 14 | + if (Ember.testing) { |
| 15 | + (<any>window)._sentryPerformanceLoad = performancePromise; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function getTransitionInformation(transition: any, router: any) { |
| 20 | + const fromRoute = transition?.from?.name; |
| 21 | + const toRoute = transition ? transition.to.name : router.currentRouteName; |
| 22 | + return { |
| 23 | + fromRoute, |
| 24 | + toRoute, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +export function _instrumentEmberRouter( |
| 29 | + routerService: any, |
| 30 | + routerMain: any, |
| 31 | + config: typeof environmentConfig['@sentry/ember'], |
| 32 | + startTransaction: Function, |
| 33 | + startTransactionOnPageLoad?: boolean, |
| 34 | +) { |
| 35 | + const { disablePostTransitionRender } = config; |
| 36 | + const location = routerMain.location; |
| 37 | + let activeTransaction: any; |
| 38 | + let transitionSpan: any; |
| 39 | + |
| 40 | + const url = location && location.getURL && location.getURL(); |
| 41 | + |
| 42 | + if (Ember.testing) { |
| 43 | + routerService._sentryInstrumented = true; |
| 44 | + } |
| 45 | + |
| 46 | + if (startTransactionOnPageLoad && url) { |
| 47 | + const routeInfo = routerService.recognize(url); |
| 48 | + activeTransaction = startTransaction({ |
| 49 | + name: `route:${routeInfo.name}`, |
| 50 | + op: 'pageload', |
| 51 | + tags: { |
| 52 | + url, |
| 53 | + toRoute: routeInfo.name, |
| 54 | + 'routing.instrumentation': '@sentry/ember', |
| 55 | + }, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + routerService.on('routeWillChange', (transition: any) => { |
| 60 | + const { fromRoute, toRoute } = getTransitionInformation(transition, routerService); |
| 61 | + activeTransaction = startTransaction({ |
| 62 | + name: `route:${toRoute}`, |
| 63 | + op: 'navigation', |
| 64 | + tags: { |
| 65 | + fromRoute, |
| 66 | + toRoute, |
| 67 | + 'routing.instrumentation': '@sentry/ember', |
| 68 | + }, |
| 69 | + }); |
| 70 | + transitionSpan = activeTransaction.startChild({ |
| 71 | + op: 'ember.transition', |
| 72 | + description: `route:${fromRoute} -> route:${toRoute}`, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + routerService.on('routeDidChange', (transition: any) => { |
| 77 | + const { toRoute } = getTransitionInformation(transition, routerService); |
| 78 | + let renderSpan: any; |
| 79 | + if (!transitionSpan || !activeTransaction) { |
| 80 | + return; |
| 81 | + } |
| 82 | + transitionSpan.finish(); |
| 83 | + |
| 84 | + if (disablePostTransitionRender) { |
| 85 | + activeTransaction.finish(); |
| 86 | + } |
| 87 | + |
| 88 | + function startRenderSpan() { |
| 89 | + renderSpan = activeTransaction.startChild({ |
| 90 | + op: 'ember.runloop.render', |
| 91 | + description: `post-transition render route:${toRoute}`, |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + function finishRenderSpan() { |
| 96 | + renderSpan.finish(); |
| 97 | + activeTransaction.finish(); |
| 98 | + } |
| 99 | + |
| 100 | + scheduleOnce('routerTransitions', null, startRenderSpan); |
| 101 | + scheduleOnce('afterRender', null, finishRenderSpan); |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +export async function instrumentForPerformance(appInstance: ApplicationInstance) { |
| 106 | + const config = environmentConfig['@sentry/ember']; |
| 107 | + const sentryConfig = config.sentry; |
| 108 | + const tracing = await import('@sentry/tracing'); |
| 109 | + |
| 110 | + const idleTimeout = config.transitionTimeout || 5000; |
| 111 | + |
| 112 | + const existingIntegrations = (sentryConfig['integrations'] || []) as Integration[]; |
| 113 | + |
| 114 | + sentryConfig['integrations'] = [ |
| 115 | + ...existingIntegrations, |
| 116 | + new tracing.Integrations.BrowserTracing({ |
| 117 | + routingInstrumentation: (startTransaction, startTransactionOnPageLoad) => { |
| 118 | + const routerMain = appInstance.lookup('router:main'); |
| 119 | + const routerService = appInstance.lookup('service:router'); |
| 120 | + _instrumentEmberRouter(routerService, routerMain, config, startTransaction, startTransactionOnPageLoad); |
| 121 | + }, |
| 122 | + idleTimeout, |
| 123 | + }), |
| 124 | + ]; |
| 125 | + |
| 126 | + Sentry.init(sentryConfig); // Call init again to rebind client with new integration list in addition to the defaults |
| 127 | +} |
| 128 | + |
| 129 | +export default { |
| 130 | + initialize, |
| 131 | +}; |
0 commit comments