Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
Ben Rohlfs | 94fcbbc | 2022-05-27 10:45:03 +0200 | [diff] [blame] | 3 | * Copyright 2016 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 5 | */ |
Ben Rohlfs | 5b5475e | 2024-07-02 18:32:31 +0200 | [diff] [blame] | 6 | import {FlagsService, KnownExperimentId} from '../flags/flags'; |
Ben Rohlfs | a7ab950 | 2021-02-15 17:45:45 +0100 | [diff] [blame] | 7 | import {EventValue, ReportingService, Timer} from './gr-reporting'; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 8 | import {hasOwnProperty} from '../../utils/common-util'; |
Milutin Kristofic | 3a84452 | 2021-01-21 10:01:55 +0100 | [diff] [blame] | 9 | import {NumericChangeId} from '../../types/common'; |
Ben Rohlfs | bd4c91b | 2021-12-22 11:37:41 +0100 | [diff] [blame] | 10 | import {Deduping, EventDetails, ReportingOptions} from '../../api/reporting'; |
Ben Rohlfs | 94f34a1 | 2021-02-26 18:37:32 +0100 | [diff] [blame] | 11 | import {PluginApi} from '../../api/plugin'; |
Milutin Kristofic | a7c24c1 | 2021-06-07 23:09:26 +0200 | [diff] [blame] | 12 | import { |
| 13 | Execution, |
| 14 | Interaction, |
| 15 | LifeCycle, |
| 16 | Timing, |
| 17 | } from '../../constants/reporting'; |
Ben Rohlfs | ed456c1 | 2024-08-14 09:19:44 +0200 | [diff] [blame^] | 18 | import {onCLS, onLCP, Metric, onINP} from 'web-vitals'; |
Ben Rohlfs | f431330 | 2023-02-23 11:57:05 +0100 | [diff] [blame] | 19 | import {getEventPath, isElementTarget} from '../../utils/dom-util'; |
Dhruv Srivastava | 7cfec74 | 2023-09-28 10:25:47 +0200 | [diff] [blame] | 20 | import {Finalizable} from '../../types/types'; |
Viktar Donich | 5ad4228 | 2016-08-26 16:07:24 -0700 | [diff] [blame] | 21 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 22 | // Latency reporting constants. |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 23 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 24 | const TIMING = { |
| 25 | TYPE: 'timing-report', |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 26 | CATEGORY: { |
| 27 | UI_LATENCY: 'UI Latency', |
| 28 | RPC: 'RPC Timing', |
| 29 | }, |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 30 | }; |
Viktar Donich | c1c5f77 | 2018-05-04 15:04:44 -0700 | [diff] [blame] | 31 | |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 32 | const LIFECYCLE = { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 33 | TYPE: 'lifecycle', |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 34 | CATEGORY: { |
| 35 | DEFAULT: 'Default', |
| 36 | EXTENSION_DETECTED: 'Extension detected', |
| 37 | PLUGINS_INSTALLED: 'Plugins installed', |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 38 | VISIBILITY: 'Visibility', |
Ben Rohlfs | bad48d4 | 2020-12-17 15:03:33 +0100 | [diff] [blame] | 39 | EXECUTION: 'Execution', |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 40 | }, |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 41 | }; |
Viktar Donich | 761ce28e | 2018-05-04 15:34:10 -0700 | [diff] [blame] | 42 | |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 43 | const INTERACTION = { |
| 44 | TYPE: 'interaction', |
| 45 | CATEGORY: { |
| 46 | DEFAULT: 'Default', |
| 47 | VISIBILITY: 'Visibility', |
| 48 | }, |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 49 | }; |
Viktar Donich | 5ad4228 | 2016-08-26 16:07:24 -0700 | [diff] [blame] | 50 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 51 | const NAVIGATION = { |
| 52 | TYPE: 'nav-report', |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 53 | CATEGORY: { |
| 54 | LOCATION_CHANGED: 'Location Changed', |
| 55 | }, |
| 56 | EVENT: { |
| 57 | PAGE: 'Page', |
| 58 | }, |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 59 | }; |
Viktar Donich | 1c97ee7 | 2017-01-17 11:13:20 -0800 | [diff] [blame] | 60 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 61 | const ERROR = { |
| 62 | TYPE: 'error', |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 63 | CATEGORY: { |
| 64 | EXCEPTION: 'exception', |
| 65 | ERROR_DIALOG: 'Error Dialog', |
| 66 | }, |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 67 | }; |
Viktar Donich | d155b8b | 2018-04-19 22:18:31 -0700 | [diff] [blame] | 68 | |
Milutin Kristofic | e1a562f | 2021-06-01 19:51:11 +0200 | [diff] [blame] | 69 | const PLUGIN = { |
| 70 | TYPE: 'plugin-log', |
| 71 | CATEGORY: { |
| 72 | LIFECYCLE: 'lifecycle', |
| 73 | INTERACTION: 'interaction', |
| 74 | }, |
| 75 | }; |
| 76 | |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 77 | const STARTUP_TIMERS: {[name: string]: number} = { |
| 78 | [Timing.PLUGINS_LOADED]: 0, |
| 79 | [Timing.METRICS_PLUGIN_LOADED]: 0, |
| 80 | [Timing.STARTUP_CHANGE_DISPLAYED]: 0, |
| 81 | [Timing.STARTUP_CHANGE_LOAD_FULL]: 0, |
| 82 | [Timing.STARTUP_DASHBOARD_DISPLAYED]: 0, |
| 83 | [Timing.STARTUP_DIFF_VIEW_CONTENT_DISPLAYED]: 0, |
| 84 | [Timing.STARTUP_DIFF_VIEW_DISPLAYED]: 0, |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 85 | [Timing.STARTUP_FILE_LIST_DISPLAYED]: 0, |
| 86 | [Timing.APP_STARTED]: 0, |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 87 | // WebComponentsReady timer is triggered from gr-router. |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 88 | [Timing.WEB_COMPONENTS_READY]: 0, |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 89 | }; |
Wyatt Allen | f87244c | 2017-02-24 15:35:57 -0800 | [diff] [blame] | 90 | |
Ben Rohlfs | 055d4a4 | 2023-05-03 13:55:08 +0200 | [diff] [blame] | 91 | // List of timers that should NOT be reset before a location change. |
| 92 | const LOCATION_CHANGE_OK_TIMERS: (string | Timing)[] = [Timing.SEND_REPLY]; |
| 93 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 94 | const SLOW_RPC_THRESHOLD = 500; |
Milutin Kristofic | c9a3597 | 2019-08-21 12:35:57 +0200 | [diff] [blame] | 95 | |
Chris Poucet | c6e880b | 2021-11-15 19:57:06 +0100 | [diff] [blame] | 96 | export function initErrorReporter(reportingService: ReportingService) { |
Milutin Kristofic | 380b9f6 | 2020-12-03 09:24:17 +0100 | [diff] [blame] | 97 | const normalizeError = (err: Error | unknown) => { |
| 98 | if (err instanceof Error) { |
| 99 | return err; |
| 100 | } |
| 101 | let msg = ''; |
| 102 | if (typeof err === 'string') { |
| 103 | msg += err; |
| 104 | } else { |
| 105 | msg += JSON.stringify(err); |
| 106 | } |
| 107 | const error = new Error(msg); |
| 108 | error.stack = 'unknown'; |
| 109 | return error; |
| 110 | }; |
| 111 | // TODO(dmfilippov): TS-fix-any oldOnError - define correct type |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 112 | const onError = function ( |
| 113 | oldOnError: Function, |
Tao Zhou | 38b3e0d | 2020-09-09 17:02:21 +0200 | [diff] [blame] | 114 | msg: Event | string, |
| 115 | url?: string, |
| 116 | line?: number, |
| 117 | column?: number, |
| 118 | error?: Error |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 119 | ) { |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 120 | if (oldOnError) { |
| 121 | oldOnError(msg, url, line, column, error); |
Viktar Donich | 1c97ee7 | 2017-01-17 11:13:20 -0800 | [diff] [blame] | 122 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 123 | if (error) { |
Milutin Kristofic | 380b9f6 | 2020-12-03 09:24:17 +0100 | [diff] [blame] | 124 | line = line ?? error.lineNumber; |
| 125 | column = column ?? error.columnNumber; |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 126 | } |
Kamil Musin | f0ece02 | 2022-10-14 15:22:44 +0200 | [diff] [blame] | 127 | reportingService.error('onError', normalizeError(error), { |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 128 | line, |
| 129 | column, |
Milutin Kristofic | 380b9f6 | 2020-12-03 09:24:17 +0100 | [diff] [blame] | 130 | url, |
| 131 | msg, |
| 132 | }); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 133 | return true; |
| 134 | }; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 135 | // TODO(dmfilippov): TS-fix-any unclear what is context |
Dhruv Srivastava | f97b055 | 2020-11-30 14:14:09 +0100 | [diff] [blame] | 136 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
Dhruv Srivastava | b3ed874 | 2023-03-24 14:42:06 +0100 | [diff] [blame] | 137 | const catchErrors = function (context?: any) { |
| 138 | context = context || window; |
Ben Rohlfs | cf59bdd | 2020-09-11 11:27:54 +0200 | [diff] [blame] | 139 | const oldOnError = context.onerror; |
Tao Zhou | 38b3e0d | 2020-09-09 17:02:21 +0200 | [diff] [blame] | 140 | context.onerror = ( |
| 141 | event: Event | string, |
| 142 | source?: string, |
| 143 | lineno?: number, |
| 144 | colno?: number, |
| 145 | error?: Error |
Frank Borden | 6988bdf | 2021-04-07 14:42:00 +0200 | [diff] [blame] | 146 | ) => onError(oldOnError, event, source, lineno, colno, error); |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 147 | context.addEventListener( |
| 148 | 'unhandledrejection', |
| 149 | (e: PromiseRejectionEvent) => { |
Kamil Musin | f0ece02 | 2022-10-14 15:22:44 +0200 | [diff] [blame] | 150 | reportingService.error('unhandledrejection', normalizeError(e.reason)); |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 151 | } |
| 152 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | catchErrors(); |
| 156 | |
| 157 | // for testing |
| 158 | return {catchErrors}; |
| 159 | } |
| 160 | |
Chris Poucet | c6e880b | 2021-11-15 19:57:06 +0100 | [diff] [blame] | 161 | export function initPerformanceReporter(reportingService: ReportingService) { |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 162 | // PerformanceObserver interface is a browser API. |
| 163 | if (window.PerformanceObserver) { |
| 164 | const supportedEntryTypes = PerformanceObserver.supportedEntryTypes || []; |
| 165 | // Safari doesn't support longtask yet |
| 166 | if (supportedEntryTypes.includes('longtask')) { |
| 167 | const catchLongJsTasks = new PerformanceObserver(list => { |
| 168 | for (const task of list.getEntries()) { |
Milutin Kristofic | 9462570 | 2022-09-09 10:17:18 +0200 | [diff] [blame] | 169 | // We are interested in longtask longer than 400 ms (default is 50 ms) |
| 170 | if (task.duration > 400) { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 171 | reportingService.reporter( |
| 172 | TIMING.TYPE, |
| 173 | TIMING.CATEGORY.UI_LATENCY, |
| 174 | `Task ${task.name}`, |
| 175 | Math.round(task.duration), |
| 176 | {}, |
| 177 | false |
| 178 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | }); |
| 182 | catchLongJsTasks.observe({entryTypes: ['longtask']}); |
| 183 | } |
Tao Zhou | d59cb8b | 2019-11-13 20:01:09 +0000 | [diff] [blame] | 184 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 185 | } |
Tao Zhou | d59cb8b | 2019-11-13 20:01:09 +0000 | [diff] [blame] | 186 | |
Chris Poucet | c6e880b | 2021-11-15 19:57:06 +0100 | [diff] [blame] | 187 | export function initVisibilityReporter(reportingService: ReportingService) { |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 188 | document.addEventListener('visibilitychange', () => { |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 189 | reportingService.onVisibilityChange(); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 190 | }); |
Ben Rohlfs | 8cabee1 | 2023-04-27 13:32:59 +0200 | [diff] [blame] | 191 | window.addEventListener('blur', () => { |
| 192 | reportingService.onFocusChange(); |
| 193 | }); |
| 194 | window.addEventListener('focus', () => { |
| 195 | reportingService.onFocusChange(); |
| 196 | }); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 197 | } |
Milutin Kristofic | 74de484 | 2020-01-29 14:09:01 +0100 | [diff] [blame] | 198 | |
Ben Rohlfs | f431330 | 2023-02-23 11:57:05 +0100 | [diff] [blame] | 199 | export function initClickReporter(reportingService: ReportingService) { |
| 200 | document.addEventListener('click', (e: MouseEvent) => { |
| 201 | const anchorEl = e |
| 202 | .composedPath() |
| 203 | .find(el => isElementTarget(el) && el.tagName.toUpperCase() === 'A') as |
| 204 | | HTMLAnchorElement |
| 205 | | undefined; |
| 206 | if (!anchorEl) return; |
| 207 | reportingService.reportInteraction(Interaction.LINK_CLICK, { |
| 208 | path: getEventPath(e), |
| 209 | link: anchorEl.href, |
| 210 | text: anchorEl.innerText, |
| 211 | }); |
| 212 | }); |
| 213 | } |
| 214 | |
Ben Rohlfs | 991c03b | 2023-04-25 11:20:38 +0200 | [diff] [blame] | 215 | /** |
| 216 | * Reports generic user interaction every x seconds to detect, if the user is |
| 217 | * present and is using the application somehow. If you just look at |
| 218 | * `document.visibilityState`, then the user may have left the browser open |
| 219 | * without locking the screen. So it helps to know whether some interaction is |
| 220 | * actually happening. |
| 221 | */ |
| 222 | export class InteractionReporter implements Finalizable { |
| 223 | /** Accumulates event names until the next round of interaction reporting. */ |
| 224 | private interactionEvents = new Set<string>(); |
| 225 | |
| 226 | /** Allows clearing the interval timer. Mostly useful for tests. */ |
| 227 | private intervalId?: number; |
| 228 | |
| 229 | constructor( |
| 230 | private readonly reportingService: ReportingService, |
| 231 | private readonly reportingIntervalMs = 10 * 1000 |
| 232 | ) { |
| 233 | const events = ['mousemove', 'scroll', 'wheel', 'keydown', 'pointerdown']; |
| 234 | for (const eventName of events) { |
| 235 | document.addEventListener(eventName, () => |
| 236 | this.interactionEvents.add(eventName) |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | this.intervalId = window.setInterval( |
| 241 | () => this.report(), |
| 242 | this.reportingIntervalMs |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | finalize() { |
| 247 | window.clearInterval(this.intervalId); |
| 248 | } |
| 249 | |
| 250 | private report() { |
| 251 | const active = this.interactionEvents.size > 0; |
| 252 | if (active) { |
| 253 | this.reportingService.reportInteraction(Interaction.USER_ACTIVE, { |
| 254 | events: [...this.interactionEvents], |
| 255 | }); |
| 256 | } else if (document.visibilityState === 'visible') { |
| 257 | this.reportingService.reportInteraction(Interaction.USER_PASSIVE, {}); |
| 258 | } |
| 259 | this.interactionEvents.clear(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | let interactionReporter: InteractionReporter; |
| 264 | |
| 265 | export function initInteractionReporter(reportingService: ReportingService) { |
| 266 | if (!interactionReporter) { |
| 267 | interactionReporter = new InteractionReporter(reportingService); |
| 268 | } |
| 269 | } |
| 270 | |
Milutin Kristofic | aad3ce4 | 2022-06-01 20:21:24 +0200 | [diff] [blame] | 271 | export function initWebVitals(reportingService: ReportingService) { |
| 272 | function reportWebVitalMetric(name: Timing, metric: Metric) { |
Milutin Kristofic | e19583e | 2023-02-07 15:13:21 +0100 | [diff] [blame] | 273 | let score = metric.value; |
| 274 | // CLS good score is 0.1 and poor score is 0.25. Logging system |
| 275 | // prefers integers, so we multiple by 100; |
| 276 | if (name === Timing.CLS) { |
| 277 | score *= 100; |
| 278 | } |
Milutin Kristofic | aad3ce4 | 2022-06-01 20:21:24 +0200 | [diff] [blame] | 279 | reportingService.reporter( |
| 280 | TIMING.TYPE, |
| 281 | TIMING.CATEGORY.UI_LATENCY, |
| 282 | name, |
Milutin Kristofic | e19583e | 2023-02-07 15:13:21 +0100 | [diff] [blame] | 283 | score, |
| 284 | { |
| 285 | navigationType: metric.navigationType, |
| 286 | rating: metric.rating, |
| 287 | entries: metric.entries, |
| 288 | } |
Milutin Kristofic | aad3ce4 | 2022-06-01 20:21:24 +0200 | [diff] [blame] | 289 | ); |
| 290 | } |
| 291 | |
Milutin Kristofic | d9b92d8 | 2023-02-02 21:20:47 +0100 | [diff] [blame] | 292 | onCLS(metric => reportWebVitalMetric(Timing.CLS, metric)); |
Milutin Kristofic | d9b92d8 | 2023-02-02 21:20:47 +0100 | [diff] [blame] | 293 | onLCP(metric => reportWebVitalMetric(Timing.LCP, metric)); |
| 294 | onINP(metric => reportWebVitalMetric(Timing.INP, metric)); |
Milutin Kristofic | aad3ce4 | 2022-06-01 20:21:24 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 297 | // Calculates the time of Gerrit being in a background tab. When Gerrit reports |
| 298 | // a pageLoad metric it’s attached to its details for latency analysis. |
| 299 | // It resets on locationChange. |
| 300 | class HiddenDurationTimer { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 301 | public accHiddenDurationMs = 0; |
| 302 | |
| 303 | public lastVisibleTimestampMs: number | null = null; |
| 304 | |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 305 | constructor() { |
| 306 | this.reset(); |
| 307 | } |
| 308 | |
| 309 | reset() { |
| 310 | this.accHiddenDurationMs = 0; |
| 311 | this.lastVisibleTimestampMs = 0; |
| 312 | } |
| 313 | |
| 314 | onVisibilityChange() { |
| 315 | if (document.visibilityState === 'hidden') { |
| 316 | this.lastVisibleTimestampMs = now(); |
| 317 | } else if (document.visibilityState === 'visible') { |
| 318 | if (this.lastVisibleTimestampMs !== null) { |
| 319 | this.accHiddenDurationMs += now() - this.lastVisibleTimestampMs; |
| 320 | // Set to null for guarding against two 'visible' events in a row. |
| 321 | this.lastVisibleTimestampMs = null; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | get hiddenDurationMs() { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 327 | if ( |
| 328 | document.visibilityState === 'hidden' && |
| 329 | this.lastVisibleTimestampMs !== null |
| 330 | ) { |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 331 | return this.accHiddenDurationMs + now() - this.lastVisibleTimestampMs; |
| 332 | } |
| 333 | return this.accHiddenDurationMs; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | export function now() { |
| 338 | return Math.round(window.performance.now()); |
| 339 | } |
| 340 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 341 | type PeformanceTimingEventName = keyof Omit<PerformanceTiming, 'toJSON'>; |
| 342 | |
| 343 | interface EventInfo { |
| 344 | type: string; |
| 345 | category: string; |
| 346 | name: string; |
| 347 | value?: EventValue; |
| 348 | eventStart: number; |
| 349 | eventDetails?: string; |
| 350 | repoName?: string; |
Milutin Kristofic | 3a84452 | 2021-01-21 10:01:55 +0100 | [diff] [blame] | 351 | changeId?: string; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 352 | inBackgroundTab?: boolean; |
| 353 | enabledExperiments?: string; |
| 354 | } |
| 355 | |
| 356 | interface PageLoadDetails { |
| 357 | rpcList: SlowRpcCall[]; |
| 358 | hiddenDurationMs: number; |
| 359 | screenSize?: {width: number; height: number}; |
| 360 | viewport?: {width: number; height: number}; |
| 361 | usedJSHeapSizeMb?: number; |
Ben Rohlfs | 5b5475e | 2024-07-02 18:32:31 +0200 | [diff] [blame] | 362 | parallelRequestsEnabled?: boolean; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | interface SlowRpcCall { |
| 366 | anonymizedUrl: string; |
| 367 | elapsed: number; |
| 368 | } |
| 369 | |
| 370 | type PendingReportInfo = [EventInfo, boolean | undefined]; |
| 371 | |
Chris Poucet | 55cbccb | 2021-11-16 03:17:06 +0100 | [diff] [blame] | 372 | export class GrReporting implements ReportingService, Finalizable { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 373 | private readonly _flagsService: FlagsService; |
| 374 | |
| 375 | private readonly _baselines = STARTUP_TIMERS; |
| 376 | |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 377 | private reportRepoName: string | undefined; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 378 | |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 379 | private reportChangeId: NumericChangeId | undefined; |
Milutin Kristofic | 3a84452 | 2021-01-21 10:01:55 +0100 | [diff] [blame] | 380 | |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 381 | private pending: PendingReportInfo[] = []; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 382 | |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 383 | private slowRpcList: SlowRpcCall[] = []; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 384 | |
Ben Rohlfs | bad48d4 | 2020-12-17 15:03:33 +0100 | [diff] [blame] | 385 | /** |
Ben Rohlfs | bd4c91b | 2021-12-22 11:37:41 +0100 | [diff] [blame] | 386 | * Keeps track of which ids were already reported for events that should only |
| 387 | * be reported once per session. |
Ben Rohlfs | bad48d4 | 2020-12-17 15:03:33 +0100 | [diff] [blame] | 388 | */ |
Ben Rohlfs | bd4c91b | 2021-12-22 11:37:41 +0100 | [diff] [blame] | 389 | private reportedIds = new Set<string>(); |
Ben Rohlfs | bad48d4 | 2020-12-17 15:03:33 +0100 | [diff] [blame] | 390 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 391 | public readonly hiddenDurationTimer = new HiddenDurationTimer(); |
| 392 | |
| 393 | constructor(flagsService: FlagsService) { |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 394 | this._flagsService = flagsService; |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 395 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 396 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 397 | private get performanceTiming() { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 398 | return window.performance.timing; |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 399 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 400 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 401 | private get slowRpcSnapshot() { |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 402 | return (this.slowRpcList || []).slice(); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 403 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 404 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 405 | private _arePluginsLoaded() { |
| 406 | return ( |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 407 | this._baselines && !hasOwnProperty(this._baselines, Timing.PLUGINS_LOADED) |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 408 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 409 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 410 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 411 | private _isMetricsPluginLoaded() { |
| 412 | return ( |
| 413 | this._arePluginsLoaded() || |
| 414 | (this._baselines && |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 415 | !hasOwnProperty(this._baselines, Timing.METRICS_PLUGIN_LOADED)) |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 416 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 417 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 418 | |
Chris Poucet | 55cbccb | 2021-11-16 03:17:06 +0100 | [diff] [blame] | 419 | finalize() {} |
| 420 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 421 | /** |
| 422 | * Reporter reports events. Events will be queued if metrics plugin is not |
| 423 | * yet installed. |
| 424 | * |
Dmitrii Filippov | 486c488 | 2020-08-06 18:34:08 +0200 | [diff] [blame] | 425 | * @param noLog If true, the event will not be logged to the JS console. |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 426 | */ |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 427 | reporter( |
| 428 | type: string, |
| 429 | category: string, |
| 430 | eventName: string, |
| 431 | eventValue?: EventValue, |
| 432 | eventDetails?: EventDetails, |
Dmitrii Filippov | 486c488 | 2020-08-06 18:34:08 +0200 | [diff] [blame] | 433 | noLog?: boolean |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 434 | ) { |
| 435 | const eventInfo = this._createEventInfo( |
| 436 | type, |
| 437 | category, |
| 438 | eventName, |
| 439 | eventValue, |
| 440 | eventDetails |
| 441 | ); |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 442 | if (type === ERROR.TYPE && category === ERROR.CATEGORY.EXCEPTION) { |
Dhruv Srivastava | b68c85f | 2020-11-30 19:04:34 +0100 | [diff] [blame] | 443 | console.error( |
| 444 | (typeof eventValue === 'object' && eventValue.error) || eventName |
| 445 | ); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // We report events immediately when metrics plugin is loaded |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 449 | if (this._isMetricsPluginLoaded() && !this.pending.length) { |
Dmitrii Filippov | 486c488 | 2020-08-06 18:34:08 +0200 | [diff] [blame] | 450 | this._reportEvent(eventInfo, noLog); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 451 | } else { |
| 452 | // We cache until metrics plugin is loaded |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 453 | this.pending.push([eventInfo, noLog]); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 454 | if (this._isMetricsPluginLoaded()) { |
Dhruv Srivastava | b3ed874 | 2023-03-24 14:42:06 +0100 | [diff] [blame] | 455 | this.pending.forEach(([eventInfo, noLog]) => { |
| 456 | this._reportEvent(eventInfo, noLog); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 457 | }); |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 458 | this.pending = []; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 459 | } |
| 460 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 461 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 462 | |
Dhruv Srivastava | b3ed874 | 2023-03-24 14:42:06 +0100 | [diff] [blame] | 463 | private _reportEvent(eventInfo: EventInfo, noLog?: boolean) { |
Ben Rohlfs | 5399855 | 2022-03-30 17:47:28 +0200 | [diff] [blame] | 464 | const {type, value, name, eventDetails} = eventInfo; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 465 | document.dispatchEvent(new CustomEvent(type, {detail: eventInfo})); |
Dhruv Srivastava | b3ed874 | 2023-03-24 14:42:06 +0100 | [diff] [blame] | 466 | if (noLog) { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 467 | return; |
| 468 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 469 | if (type !== ERROR.TYPE) { |
| 470 | if (value !== undefined) { |
Dhruv Srivastava | 805f4910 | 2022-07-19 13:17:20 +0200 | [diff] [blame] | 471 | console.debug( |
| 472 | `Reporting(${new Date().toISOString()}): ${name}: ${value}` |
| 473 | ); |
Ben Rohlfs | 5399855 | 2022-03-30 17:47:28 +0200 | [diff] [blame] | 474 | } else if (eventDetails !== undefined) { |
Dhruv Srivastava | 805f4910 | 2022-07-19 13:17:20 +0200 | [diff] [blame] | 475 | console.debug( |
| 476 | `Reporting(${new Date().toISOString()}): ${name}: ${eventDetails}` |
| 477 | ); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 478 | } else { |
Dhruv Srivastava | 805f4910 | 2022-07-19 13:17:20 +0200 | [diff] [blame] | 479 | console.debug(`Reporting(${new Date().toISOString()}): ${name}`); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 480 | } |
| 481 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 482 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 483 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 484 | private _createEventInfo( |
| 485 | type: string, |
| 486 | category: string, |
| 487 | name: string, |
| 488 | value?: EventValue, |
| 489 | eventDetails?: EventDetails |
| 490 | ): EventInfo { |
| 491 | const eventInfo: EventInfo = { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 492 | type, |
| 493 | category, |
| 494 | name, |
| 495 | value, |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 496 | eventStart: now(), |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 497 | }; |
| 498 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 499 | if ( |
| 500 | typeof eventDetails === 'object' && |
| 501 | Object.entries(eventDetails).length !== 0 |
| 502 | ) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 503 | eventInfo.eventDetails = JSON.stringify(eventDetails); |
| 504 | } |
Tao Zhou | cef7917 | 2020-04-08 16:05:31 +0200 | [diff] [blame] | 505 | |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 506 | if (this.reportRepoName) { |
| 507 | eventInfo.repoName = this.reportRepoName; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 508 | } |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 509 | if (this.reportChangeId) { |
| 510 | eventInfo.changeId = `${this.reportChangeId}`; |
Milutin Kristofic | 3a84452 | 2021-01-21 10:01:55 +0100 | [diff] [blame] | 511 | } |
Tao Zhou | cef7917 | 2020-04-08 16:05:31 +0200 | [diff] [blame] | 512 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 513 | const isInBackgroundTab = document.visibilityState === 'hidden'; |
| 514 | if (isInBackgroundTab !== undefined) { |
| 515 | eventInfo.inBackgroundTab = isInBackgroundTab; |
| 516 | } |
| 517 | |
Milutin Kristofic | b131af9 | 2021-07-13 15:42:34 +0200 | [diff] [blame] | 518 | if ( |
| 519 | name === Timing.APP_STARTED && |
| 520 | this._flagsService.enabledExperiments.length |
| 521 | ) { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 522 | eventInfo.enabledExperiments = JSON.stringify( |
| 523 | this._flagsService.enabledExperiments |
| 524 | ); |
Tao Zhou | cef7917 | 2020-04-08 16:05:31 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 527 | return eventInfo; |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 528 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 529 | |
| 530 | /** |
| 531 | * User-perceived app start time, should be reported when the app is ready. |
| 532 | */ |
| 533 | appStarted() { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 534 | this.timeEnd(Timing.APP_STARTED); |
Milutin Kristofic | 091ab76 | 2020-03-19 16:37:48 +0100 | [diff] [blame] | 535 | this._reportNavResTimes(); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 536 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 537 | |
Ben Rohlfs | 8cabee1 | 2023-04-27 13:32:59 +0200 | [diff] [blame] | 538 | onFocusChange() { |
| 539 | this.reporter( |
| 540 | LIFECYCLE.TYPE, |
| 541 | LIFECYCLE.CATEGORY.VISIBILITY, |
| 542 | LifeCycle.FOCUS, |
| 543 | undefined, |
| 544 | { |
| 545 | isVisible: document.visibilityState === 'visible', |
| 546 | hasFocus: document.hasFocus(), |
| 547 | }, |
| 548 | false |
| 549 | ); |
| 550 | } |
| 551 | |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 552 | onVisibilityChange() { |
| 553 | this.hiddenDurationTimer.onVisibilityChange(); |
Milutin Kristofic | aaef49a | 2021-03-10 13:30:19 +0100 | [diff] [blame] | 554 | let eventName; |
| 555 | if (document.visibilityState === 'hidden') { |
| 556 | eventName = LifeCycle.VISIBILILITY_HIDDEN; |
| 557 | } else if (document.visibilityState === 'visible') { |
| 558 | eventName = LifeCycle.VISIBILILITY_VISIBLE; |
| 559 | } |
| 560 | if (eventName) |
| 561 | this.reporter( |
| 562 | LIFECYCLE.TYPE, |
| 563 | LIFECYCLE.CATEGORY.VISIBILITY, |
| 564 | eventName, |
| 565 | undefined, |
| 566 | { |
| 567 | hiddenDurationMs: this.hiddenDurationTimer.hiddenDurationMs, |
Ben Rohlfs | 8cabee1 | 2023-04-27 13:32:59 +0200 | [diff] [blame] | 568 | isVisible: document.visibilityState === 'visible', |
| 569 | hasFocus: document.hasFocus(), |
Milutin Kristofic | aaef49a | 2021-03-10 13:30:19 +0100 | [diff] [blame] | 570 | }, |
| 571 | false |
| 572 | ); |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 573 | } |
| 574 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 575 | /** |
Milutin Kristofic | 091ab76 | 2020-03-19 16:37:48 +0100 | [diff] [blame] | 576 | * Browser's navigation and resource timings |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 577 | */ |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 578 | private _reportNavResTimes() { |
Milutin Kristofic | 091ab76 | 2020-03-19 16:37:48 +0100 | [diff] [blame] | 579 | const perfEvents = Object.keys(this.performanceTiming.toJSON()); |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 580 | perfEvents.forEach(eventName => |
| 581 | this._reportPerformanceTiming(eventName as PeformanceTimingEventName) |
Milutin Kristofic | 091ab76 | 2020-03-19 16:37:48 +0100 | [diff] [blame] | 582 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 583 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 584 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 585 | private _reportPerformanceTiming( |
| 586 | eventName: PeformanceTimingEventName, |
| 587 | eventDetails?: EventDetails |
| 588 | ) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 589 | const eventTiming = this.performanceTiming[eventName]; |
| 590 | if (eventTiming > 0) { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 591 | const elapsedTime = eventTiming - this.performanceTiming.navigationStart; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 592 | // NavResTime - Navigation and resource timings. |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 593 | this.reporter( |
| 594 | TIMING.TYPE, |
| 595 | TIMING.CATEGORY.UI_LATENCY, |
| 596 | `NavResTime - ${eventName}`, |
| 597 | elapsedTime, |
| 598 | eventDetails, |
| 599 | true |
| 600 | ); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 601 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 602 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 603 | |
| 604 | beforeLocationChanged() { |
| 605 | for (const prop of Object.keys(this._baselines)) { |
Ben Rohlfs | 055d4a4 | 2023-05-03 13:55:08 +0200 | [diff] [blame] | 606 | if (LOCATION_CHANGE_OK_TIMERS.includes(prop)) continue; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 607 | delete this._baselines[prop]; |
| 608 | } |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 609 | this.time(Timing.CHANGE_DISPLAYED); |
| 610 | this.time(Timing.CHANGE_LOAD_FULL); |
| 611 | this.time(Timing.DASHBOARD_DISPLAYED); |
| 612 | this.time(Timing.DIFF_VIEW_CONTENT_DISPLAYED); |
| 613 | this.time(Timing.DIFF_VIEW_DISPLAYED); |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 614 | this.time(Timing.FILE_LIST_DISPLAYED); |
Ben Rohlfs | 17f3575d | 2024-02-26 11:27:00 +0100 | [diff] [blame] | 615 | |
| 616 | this.setRepoName(undefined); |
| 617 | this.setChangeId(undefined); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 618 | // reset slow rpc list since here start page loads which report these rpcs |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 619 | this.slowRpcList = []; |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 620 | this.hiddenDurationTimer.reset(); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 621 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 622 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 623 | locationChanged(page: string) { |
| 624 | this.reporter( |
| 625 | NAVIGATION.TYPE, |
| 626 | NAVIGATION.CATEGORY.LOCATION_CHANGED, |
| 627 | NAVIGATION.EVENT.PAGE, |
| 628 | page |
| 629 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 630 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 631 | |
| 632 | dashboardDisplayed() { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 633 | if (hasOwnProperty(this._baselines, Timing.STARTUP_DASHBOARD_DISPLAYED)) { |
| 634 | this.timeEnd(Timing.STARTUP_DASHBOARD_DISPLAYED, this._pageLoadDetails()); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 635 | } else { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 636 | this.timeEnd(Timing.DASHBOARD_DISPLAYED, this._pageLoadDetails()); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 637 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 638 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 639 | |
Milutin Kristofic | 933e6bd | 2021-06-07 20:19:45 +0200 | [diff] [blame] | 640 | changeDisplayed(eventDetails?: EventDetails) { |
| 641 | eventDetails = {...eventDetails, ...this._pageLoadDetails()}; |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 642 | if (hasOwnProperty(this._baselines, Timing.STARTUP_CHANGE_DISPLAYED)) { |
Milutin Kristofic | 933e6bd | 2021-06-07 20:19:45 +0200 | [diff] [blame] | 643 | this.timeEnd(Timing.STARTUP_CHANGE_DISPLAYED, eventDetails); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 644 | } else { |
Milutin Kristofic | 933e6bd | 2021-06-07 20:19:45 +0200 | [diff] [blame] | 645 | this.timeEnd(Timing.CHANGE_DISPLAYED, eventDetails); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 646 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 647 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 648 | |
| 649 | changeFullyLoaded() { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 650 | if (hasOwnProperty(this._baselines, Timing.STARTUP_CHANGE_LOAD_FULL)) { |
| 651 | this.timeEnd(Timing.STARTUP_CHANGE_LOAD_FULL); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 652 | } else { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 653 | this.timeEnd(Timing.CHANGE_LOAD_FULL); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 654 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 655 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 656 | |
| 657 | diffViewDisplayed() { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 658 | if (hasOwnProperty(this._baselines, Timing.STARTUP_DIFF_VIEW_DISPLAYED)) { |
| 659 | this.timeEnd(Timing.STARTUP_DIFF_VIEW_DISPLAYED, this._pageLoadDetails()); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 660 | } else { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 661 | this.timeEnd(Timing.DIFF_VIEW_DISPLAYED, this._pageLoadDetails()); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 662 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 663 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 664 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 665 | diffViewContentDisplayed() { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 666 | if ( |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 667 | hasOwnProperty( |
| 668 | this._baselines, |
| 669 | Timing.STARTUP_DIFF_VIEW_CONTENT_DISPLAYED |
| 670 | ) |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 671 | ) { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 672 | this.timeEnd(Timing.STARTUP_DIFF_VIEW_CONTENT_DISPLAYED); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 673 | } else { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 674 | this.timeEnd(Timing.DIFF_VIEW_CONTENT_DISPLAYED); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 675 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 676 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 677 | |
| 678 | fileListDisplayed() { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 679 | if (hasOwnProperty(this._baselines, Timing.STARTUP_FILE_LIST_DISPLAYED)) { |
| 680 | this.timeEnd(Timing.STARTUP_FILE_LIST_DISPLAYED); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 681 | } else { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 682 | this.timeEnd(Timing.FILE_LIST_DISPLAYED); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 683 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 684 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 685 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 686 | private _pageLoadDetails(): PageLoadDetails { |
| 687 | const details: PageLoadDetails = { |
Milutin Kristofic | a358012 | 2020-04-14 14:31:48 +0200 | [diff] [blame] | 688 | rpcList: this.slowRpcSnapshot, |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 689 | hiddenDurationMs: this.hiddenDurationTimer.accHiddenDurationMs, |
Ben Rohlfs | 5b5475e | 2024-07-02 18:32:31 +0200 | [diff] [blame] | 690 | parallelRequestsEnabled: this._flagsService.isEnabled( |
| 691 | KnownExperimentId.PARALLEL_DASHBOARD_REQUESTS |
| 692 | ), |
Milutin Kristofic | a358012 | 2020-04-14 14:31:48 +0200 | [diff] [blame] | 693 | }; |
| 694 | |
| 695 | if (window.screen) { |
| 696 | details.screenSize = { |
| 697 | width: window.screen.width, |
| 698 | height: window.screen.height, |
| 699 | }; |
| 700 | } |
| 701 | |
Tao Zhou | 34b0f12 | 2020-08-31 13:44:50 +0200 | [diff] [blame] | 702 | if (document?.documentElement) { |
Milutin Kristofic | a358012 | 2020-04-14 14:31:48 +0200 | [diff] [blame] | 703 | details.viewport = { |
| 704 | width: document.documentElement.clientWidth, |
| 705 | height: document.documentElement.clientHeight, |
| 706 | }; |
| 707 | } |
| 708 | |
Tao Zhou | 34b0f12 | 2020-08-31 13:44:50 +0200 | [diff] [blame] | 709 | if (window.performance?.memory) { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 710 | const toMb = (bytes: number) => |
| 711 | Math.round((bytes / (1024 * 1024)) * 100) / 100; |
| 712 | details.usedJSHeapSizeMb = toMb(window.performance.memory.usedJSHeapSize); |
Milutin Kristofic | a358012 | 2020-04-14 14:31:48 +0200 | [diff] [blame] | 713 | } |
| 714 | |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 715 | details.hiddenDurationMs = this.hiddenDurationTimer.hiddenDurationMs; |
Milutin Kristofic | a358012 | 2020-04-14 14:31:48 +0200 | [diff] [blame] | 716 | return details; |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 717 | } |
Milutin Kristofic | a358012 | 2020-04-14 14:31:48 +0200 | [diff] [blame] | 718 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 719 | reportExtension(name: string) { |
Milutin Kristofic | aaef49a | 2021-03-10 13:30:19 +0100 | [diff] [blame] | 720 | this.reporter( |
| 721 | LIFECYCLE.TYPE, |
| 722 | LIFECYCLE.CATEGORY.EXTENSION_DETECTED, |
| 723 | LifeCycle.EXTENSION_DETECTED, |
| 724 | undefined, |
| 725 | {name} |
| 726 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 727 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 728 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 729 | pluginLoaded(name: string) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 730 | if (name.startsWith('metrics-')) { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 731 | this.timeEnd(Timing.METRICS_PLUGIN_LOADED); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 732 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 733 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 734 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 735 | pluginsLoaded(pluginsList?: string[]) { |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 736 | this.timeEnd(Timing.PLUGINS_LOADED); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 737 | this.reporter( |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 738 | LIFECYCLE.TYPE, |
| 739 | LIFECYCLE.CATEGORY.PLUGINS_INSTALLED, |
Milutin Kristofic | aaef49a | 2021-03-10 13:30:19 +0100 | [diff] [blame] | 740 | LifeCycle.PLUGINS_INSTALLED, |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 741 | undefined, |
| 742 | {pluginsList: pluginsList || []}, |
Ben Rohlfs | 5399855 | 2022-03-30 17:47:28 +0200 | [diff] [blame] | 743 | false |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 744 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 745 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 746 | |
Ben Rohlfs | 8941858 | 2021-09-27 12:15:57 +0200 | [diff] [blame] | 747 | pluginsFailed(pluginsList?: string[]) { |
| 748 | if (!pluginsList || pluginsList.length === 0) return; |
| 749 | this.reporter( |
| 750 | LIFECYCLE.TYPE, |
| 751 | LIFECYCLE.CATEGORY.PLUGINS_INSTALLED, |
| 752 | LifeCycle.PLUGINS_FAILED, |
| 753 | undefined, |
| 754 | {pluginsList: pluginsList || []}, |
Ben Rohlfs | 5399855 | 2022-03-30 17:47:28 +0200 | [diff] [blame] | 755 | false |
Ben Rohlfs | 8941858 | 2021-09-27 12:15:57 +0200 | [diff] [blame] | 756 | ); |
| 757 | } |
| 758 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 759 | /** |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 760 | * Reset named Timing. |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 761 | */ |
Milutin Kristofic | 48a0f43 | 2021-03-16 22:53:08 +0100 | [diff] [blame] | 762 | time(name: Timing) { |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 763 | this._baselines[name] = now(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 764 | window.performance.mark(`${name}-start`); |
Milutin Kristofic | 6e6f42e | 2024-04-09 19:29:05 +0200 | [diff] [blame] | 765 | // When time(Timing.DASHBOARD_DISPLAYED) is called gr-dashboard-view |
| 766 | // we need to clean-up slowRpcList, otherwise it can accumulate to big size |
| 767 | if (name === Timing.DASHBOARD_DISPLAYED) { |
| 768 | this.slowRpcList = []; |
| 769 | this.hiddenDurationTimer.reset(); |
| 770 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 771 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 772 | |
| 773 | /** |
| 774 | * Finish named timer and report it to server. |
| 775 | */ |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 776 | timeEnd(name: Timing, eventDetails?: EventDetails): number { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 777 | if (!hasOwnProperty(this._baselines, name)) { |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 778 | return 0; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 779 | } |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 780 | const begin = this._baselines[name]; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 781 | delete this._baselines[name]; |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 782 | const end = now(); |
| 783 | const elapsed = end - begin; |
| 784 | this._reportTiming(name, elapsed, eventDetails); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 785 | |
| 786 | // Finalize the interval. Either from a registered start mark or |
| 787 | // the navigation start time (if baseTime is 0). |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 788 | if (begin !== 0) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 789 | window.performance.measure(name, `${name}-start`); |
| 790 | } else { |
David Ostrovsky | f91f966 | 2021-02-22 19:34:37 +0100 | [diff] [blame] | 791 | // Microsoft Edge does not handle the 2nd param correctly |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 792 | // (if undefined). |
| 793 | window.performance.measure(name); |
| 794 | } |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 795 | return elapsed; |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 796 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 797 | |
| 798 | /** |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 799 | * Send a timing report with an arbitrary time value. |
| 800 | * |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 801 | * @param name Timing name. |
| 802 | * @param time The time to report as an integer of milliseconds. |
| 803 | * @param eventDetails non sensitive details |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 804 | */ |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 805 | private _reportTiming( |
| 806 | name: string, |
| 807 | time: number, |
| 808 | eventDetails?: EventDetails |
| 809 | ) { |
| 810 | this.reporter( |
| 811 | TIMING.TYPE, |
| 812 | TIMING.CATEGORY.UI_LATENCY, |
| 813 | name, |
| 814 | time, |
| 815 | eventDetails |
| 816 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 817 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 818 | |
| 819 | /** |
David Ostrovsky | f91f966 | 2021-02-22 19:34:37 +0100 | [diff] [blame] | 820 | * Get a timer object to for reporting a user timing. The start time will be |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 821 | * the time that the object has been created, and the end time will be the |
| 822 | * time that the "end" method is called on the object. |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 823 | */ |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 824 | getTimer(name: string): Timer { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 825 | let called = false; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 826 | let start: number; |
| 827 | let max: number | null = null; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 828 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 829 | const timer: Timer = { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 830 | // Clear the timer and reset the start time. |
| 831 | reset: () => { |
| 832 | called = false; |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 833 | start = now(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 834 | return timer; |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 835 | }, |
Wyatt Allen | 18f0354 | 2018-06-19 17:03:43 -0700 | [diff] [blame] | 836 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 837 | // Stop the timer and report the intervening time. |
Ben Rohlfs | 23152eb | 2021-04-30 09:01:30 +0200 | [diff] [blame] | 838 | end: (eventDetails?: EventDetails) => { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 839 | if (called) { |
| 840 | throw new Error(`Timer for "${name}" already ended.`); |
| 841 | } |
| 842 | called = true; |
Milutin Kristofic | 0b221a6 | 2020-04-30 19:57:10 +0200 | [diff] [blame] | 843 | const time = now() - start; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 844 | |
| 845 | // If a maximum is specified and the time exceeds it, do not report. |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 846 | if (max && time > max) { |
| 847 | return timer; |
| 848 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 849 | |
Ben Rohlfs | 23152eb | 2021-04-30 09:01:30 +0200 | [diff] [blame] | 850 | this._reportTiming(name, time, eventDetails); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 851 | return timer; |
Wyatt Allen | 18f0354 | 2018-06-19 17:03:43 -0700 | [diff] [blame] | 852 | }, |
Viktar Donich | 78b7cd2 | 2016-08-18 16:45:05 -0700 | [diff] [blame] | 853 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 854 | // Set a maximum reportable time. If a maximum is set and the timer is |
| 855 | // ended after the specified amount of time, the value is not reported. |
| 856 | withMaximum(maximum) { |
| 857 | max = maximum; |
| 858 | return timer; |
| 859 | }, |
| 860 | }; |
Viktar Donich | 78b7cd2 | 2016-08-18 16:45:05 -0700 | [diff] [blame] | 861 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 862 | // The timer is initialized to its creation time. |
| 863 | return timer.reset(); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 864 | } |
Milutin Kristofic | 623ecd1 | 2020-02-15 21:46:58 +0100 | [diff] [blame] | 865 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 866 | /** |
| 867 | * Log timing information for an RPC. |
| 868 | * |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 869 | * @param anonymizedUrl The URL of the RPC with tokens obfuscated. |
| 870 | * @param elapsed The time elapsed of the RPC. |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 871 | */ |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 872 | reportRpcTiming(anonymizedUrl: string, elapsed: number) { |
| 873 | this.reporter( |
| 874 | TIMING.TYPE, |
| 875 | TIMING.CATEGORY.RPC, |
| 876 | 'RPC-' + anonymizedUrl, |
| 877 | elapsed, |
| 878 | {}, |
| 879 | true |
| 880 | ); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 881 | if (elapsed >= SLOW_RPC_THRESHOLD) { |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 882 | this.slowRpcList.push({anonymizedUrl, elapsed}); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 883 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 884 | } |
Viktar Donich | 78b7cd2 | 2016-08-18 16:45:05 -0700 | [diff] [blame] | 885 | |
Milutin Kristofic | aaef49a | 2021-03-10 13:30:19 +0100 | [diff] [blame] | 886 | reportLifeCycle(eventName: LifeCycle, details: EventDetails) { |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 887 | this.reporter( |
| 888 | LIFECYCLE.TYPE, |
| 889 | LIFECYCLE.CATEGORY.DEFAULT, |
| 890 | eventName, |
| 891 | undefined, |
| 892 | details, |
Ben Rohlfs | 5399855 | 2022-03-30 17:47:28 +0200 | [diff] [blame] | 893 | false |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 894 | ); |
Milutin Kristofic | 0fe9679 | 2020-03-26 13:17:35 +0100 | [diff] [blame] | 895 | } |
| 896 | |
Milutin Kristofic | e1a562f | 2021-06-01 19:51:11 +0200 | [diff] [blame] | 897 | reportPluginLifeCycleLog(eventName: string, details: EventDetails) { |
| 898 | this.reporter( |
| 899 | PLUGIN.TYPE, |
| 900 | PLUGIN.CATEGORY.LIFECYCLE, |
| 901 | eventName, |
| 902 | undefined, |
| 903 | details, |
Ben Rohlfs | 5399855 | 2022-03-30 17:47:28 +0200 | [diff] [blame] | 904 | false |
Milutin Kristofic | e1a562f | 2021-06-01 19:51:11 +0200 | [diff] [blame] | 905 | ); |
| 906 | } |
| 907 | |
| 908 | reportPluginInteractionLog(eventName: string, details: EventDetails) { |
| 909 | this.reporter( |
| 910 | PLUGIN.TYPE, |
| 911 | PLUGIN.CATEGORY.INTERACTION, |
| 912 | eventName, |
| 913 | undefined, |
| 914 | details, |
| 915 | true |
| 916 | ); |
| 917 | } |
| 918 | |
Ben Rohlfs | bd4c91b | 2021-12-22 11:37:41 +0100 | [diff] [blame] | 919 | /** |
| 920 | * Returns true when the event was deduped and thus should not be reported. |
| 921 | */ |
| 922 | _dedup( |
| 923 | eventName: string | Interaction, |
| 924 | details: EventDetails, |
| 925 | deduping?: Deduping |
| 926 | ): boolean { |
| 927 | if (!deduping) return false; |
| 928 | let id = ''; |
| 929 | switch (deduping) { |
| 930 | case Deduping.DETAILS_ONCE_PER_CHANGE: |
| 931 | id = `${eventName}-${this.reportChangeId}-${JSON.stringify(details)}`; |
| 932 | break; |
| 933 | case Deduping.DETAILS_ONCE_PER_SESSION: |
| 934 | id = `${eventName}-${JSON.stringify(details)}`; |
| 935 | break; |
| 936 | case Deduping.EVENT_ONCE_PER_CHANGE: |
| 937 | id = `${eventName}-${this.reportChangeId}`; |
| 938 | break; |
| 939 | case Deduping.EVENT_ONCE_PER_SESSION: |
| 940 | id = `${eventName}`; |
| 941 | break; |
| 942 | default: |
| 943 | throw new Error(`Invalid 'deduping' option '${deduping}'.`); |
| 944 | } |
| 945 | if (this.reportedIds.has(id)) return true; |
| 946 | this.reportedIds.add(id); |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | reportInteraction( |
| 951 | eventName: string | Interaction, |
| 952 | details: EventDetails, |
| 953 | options?: ReportingOptions |
| 954 | ) { |
| 955 | if (this._dedup(eventName, details, options?.deduping)) return; |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 956 | this.reporter( |
| 957 | INTERACTION.TYPE, |
| 958 | INTERACTION.CATEGORY.DEFAULT, |
| 959 | eventName, |
| 960 | undefined, |
| 961 | details, |
Ben Rohlfs | 5399855 | 2022-03-30 17:47:28 +0200 | [diff] [blame] | 962 | false |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 963 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 964 | } |
Viktar Donich | 82ad0ec | 2018-05-17 14:33:22 -0700 | [diff] [blame] | 965 | |
Milutin Kristofic | 2fddf92 | 2021-03-11 10:35:19 +0100 | [diff] [blame] | 966 | reportExecution(name: Execution, details?: EventDetails) { |
Ben Rohlfs | bd4c91b | 2021-12-22 11:37:41 +0100 | [diff] [blame] | 967 | if (this._dedup(name, details, Deduping.DETAILS_ONCE_PER_SESSION)) return; |
Ben Rohlfs | bad48d4 | 2020-12-17 15:03:33 +0100 | [diff] [blame] | 968 | this.reporter( |
| 969 | LIFECYCLE.TYPE, |
| 970 | LIFECYCLE.CATEGORY.EXECUTION, |
Milutin Kristofic | 2fddf92 | 2021-03-11 10:35:19 +0100 | [diff] [blame] | 971 | name, |
Ben Rohlfs | bad48d4 | 2020-12-17 15:03:33 +0100 | [diff] [blame] | 972 | undefined, |
| 973 | details, |
Ben Rohlfs | 94f34a1 | 2021-02-26 18:37:32 +0100 | [diff] [blame] | 974 | true // skip console log |
Ben Rohlfs | bad48d4 | 2020-12-17 15:03:33 +0100 | [diff] [blame] | 975 | ); |
| 976 | } |
| 977 | |
Ben Rohlfs | 629d89d | 2021-03-13 22:04:19 +0100 | [diff] [blame] | 978 | trackApi( |
| 979 | pluginApi: Pick<PluginApi, 'getPluginName'>, |
| 980 | object: string, |
| 981 | method: string |
| 982 | ) { |
Ben Rohlfs | 13edc7c | 2021-03-08 08:50:07 +0100 | [diff] [blame] | 983 | const plugin = pluginApi?.getPluginName() ?? 'unknown'; |
Milutin Kristofic | 2fddf92 | 2021-03-11 10:35:19 +0100 | [diff] [blame] | 984 | this.reportExecution(Execution.PLUGIN_API, {plugin, object, method}); |
Ben Rohlfs | 94f34a1 | 2021-02-26 18:37:32 +0100 | [diff] [blame] | 985 | } |
| 986 | |
Kamil Musin | f0ece02 | 2022-10-14 15:22:44 +0200 | [diff] [blame] | 987 | error(errorSource: string, error: Error, details?: EventDetails) { |
| 988 | const message = `${errorSource}: ${error.message}`; |
| 989 | const eventDetails = { |
| 990 | errorMessage: message, |
| 991 | ...details, |
| 992 | stack: error.stack, |
| 993 | }; |
Milutin Kristofic | 380b9f6 | 2020-12-03 09:24:17 +0100 | [diff] [blame] | 994 | |
| 995 | this.reporter( |
| 996 | ERROR.TYPE, |
| 997 | ERROR.CATEGORY.EXCEPTION, |
Kamil Musin | f0ece02 | 2022-10-14 15:22:44 +0200 | [diff] [blame] | 998 | errorSource, |
Milutin Kristofic | 380b9f6 | 2020-12-03 09:24:17 +0100 | [diff] [blame] | 999 | {error}, |
Kamil Musin | f0ece02 | 2022-10-14 15:22:44 +0200 | [diff] [blame] | 1000 | eventDetails |
Milutin Kristofic | 380b9f6 | 2020-12-03 09:24:17 +0100 | [diff] [blame] | 1001 | ); |
| 1002 | } |
| 1003 | |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 1004 | reportErrorDialog(message: string) { |
| 1005 | this.reporter( |
| 1006 | ERROR.TYPE, |
| 1007 | ERROR.CATEGORY.ERROR_DIALOG, |
Kamil Musin | f0ece02 | 2022-10-14 15:22:44 +0200 | [diff] [blame] | 1008 | 'ErrorDialog', |
| 1009 | {error: new Error(message)}, |
| 1010 | {errorMessage: message} |
Dmitrii Filippov | a5ace2f | 2020-07-29 09:30:56 +0200 | [diff] [blame] | 1011 | ); |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 1012 | } |
Milutin Kristofic | 68a27a3 | 2020-01-27 15:53:17 +0100 | [diff] [blame] | 1013 | |
Ben Rohlfs | 17f3575d | 2024-02-26 11:27:00 +0100 | [diff] [blame] | 1014 | setRepoName(repoName?: string) { |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 1015 | this.reportRepoName = repoName; |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 1016 | } |
Milutin Kristofic | 3a84452 | 2021-01-21 10:01:55 +0100 | [diff] [blame] | 1017 | |
Ben Rohlfs | 17f3575d | 2024-02-26 11:27:00 +0100 | [diff] [blame] | 1018 | setChangeId(changeId?: NumericChangeId) { |
| 1019 | const originalChangeId = this.reportChangeId; |
Dhruv Srivastava | 7e454cd | 2021-02-22 15:06:58 +0100 | [diff] [blame] | 1020 | this.reportChangeId = changeId; |
Ben Rohlfs | 17f3575d | 2024-02-26 11:27:00 +0100 | [diff] [blame] | 1021 | |
| 1022 | if (!!changeId && changeId !== originalChangeId) { |
| 1023 | this.reportInteraction(Interaction.CHANGE_ID_CHANGED, {changeId}); |
| 1024 | } |
Milutin Kristofic | 3a84452 | 2021-01-21 10:01:55 +0100 | [diff] [blame] | 1025 | } |
Milutin Kristofic | da88b33 | 2020-03-24 10:19:12 +0100 | [diff] [blame] | 1026 | } |
Milutin Kristofic | 68a27a3 | 2020-01-27 15:53:17 +0100 | [diff] [blame] | 1027 | |
Tao Zhou | 4cd35cb | 2020-07-22 11:28:22 +0200 | [diff] [blame] | 1028 | export const DEFAULT_STARTUP_TIMERS = {...STARTUP_TIMERS}; |