|
| 1 | +import { |
| 2 | + defineIntegration, |
| 3 | + Event, |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 5 | + setMeasurement, |
| 6 | + Span, |
| 7 | + SpanStatus, |
| 8 | + startSpanManual, |
| 9 | + StartSpanOptions, |
| 10 | + timestampInSeconds, |
| 11 | +} from '@sentry/core'; |
| 12 | +import { app } from 'electron'; |
| 13 | +import { ipcMainHooks } from '../ipc.js'; |
| 14 | + |
| 15 | +export interface StartupTracingOptions { |
| 16 | + /* |
| 17 | + * Timeout in seconds to wait before ending the startup transaction |
| 18 | + * Defaults to 10 seconds |
| 19 | + */ |
| 20 | + timeoutSeconds?: number; |
| 21 | +} |
| 22 | + |
| 23 | +let cachedRootTransaction: Span | undefined; |
| 24 | +/** |
| 25 | + * Creates the root startup span lazily because otel hasn't been configured when the integration is setup |
| 26 | + */ |
| 27 | +function rootTransaction(): Span { |
| 28 | + if (!cachedRootTransaction) { |
| 29 | + // Calculate the actual start time of the process |
| 30 | + const uptimeMs = process.uptime() * 1000; |
| 31 | + const startTime = (Date.now() - uptimeMs) / 1000; |
| 32 | + |
| 33 | + startSpanManual( |
| 34 | + { |
| 35 | + name: 'Startup', |
| 36 | + op: 'app.start', |
| 37 | + startTime, |
| 38 | + attributes: { |
| 39 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.electron.startup', |
| 40 | + }, |
| 41 | + forceTransaction: true, |
| 42 | + }, |
| 43 | + (root) => { |
| 44 | + cachedRootTransaction = root; |
| 45 | + }, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + return cachedRootTransaction as Span; |
| 50 | +} |
| 51 | + |
| 52 | +function zeroLengthSpan(options: StartSpanOptions): void { |
| 53 | + const startTime = timestampInSeconds(); |
| 54 | + |
| 55 | + startSpanManual( |
| 56 | + { |
| 57 | + ...options, |
| 58 | + attributes: { |
| 59 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.electron.startup', |
| 60 | + ...options.attributes, |
| 61 | + }, |
| 62 | + parentSpan: options.parentSpan || rootTransaction(), |
| 63 | + startTime, |
| 64 | + }, |
| 65 | + (span) => { |
| 66 | + span.end(startTime * 1000); |
| 67 | + }, |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +function waitForRendererPageload(timeout: number): Promise<Event | undefined> { |
| 72 | + return new Promise((resolve) => { |
| 73 | + const timer = setTimeout(() => { |
| 74 | + resolve(undefined); |
| 75 | + }, timeout); |
| 76 | + ipcMainHooks.once('pageload-transaction', (event, _contents) => { |
| 77 | + clearTimeout(timer); |
| 78 | + resolve(event); |
| 79 | + }); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +function parseStatus(status: string): SpanStatus { |
| 84 | + if (status === 'ok') { |
| 85 | + return { code: 1 }; |
| 86 | + } |
| 87 | + |
| 88 | + return { code: 2, message: status }; |
| 89 | +} |
| 90 | + |
| 91 | +function applyRendererSpansAndMeasurements(parentSpan: Span, event: Event | undefined, endTimestamp: number): number { |
| 92 | + let lastEndTimestamp = endTimestamp; |
| 93 | + |
| 94 | + if (!event) { |
| 95 | + return lastEndTimestamp; |
| 96 | + } |
| 97 | + |
| 98 | + const rendererStartTime = event.start_timestamp || event.timestamp; |
| 99 | + parentSpan.setAttribute('performance.timeOrigin', rendererStartTime); |
| 100 | + |
| 101 | + startSpanManual( |
| 102 | + { |
| 103 | + name: event.transaction || 'electron.renderer', |
| 104 | + op: 'electron.renderer', |
| 105 | + startTime: rendererStartTime, |
| 106 | + parentSpan, |
| 107 | + attributes: { |
| 108 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.electron.startup', |
| 109 | + }, |
| 110 | + }, |
| 111 | + (rendererSpan) => { |
| 112 | + if (event?.spans?.length) { |
| 113 | + for (const spanJson of event.spans) { |
| 114 | + const startTime = spanJson.start_timestamp; |
| 115 | + const endTime = spanJson.timestamp; |
| 116 | + |
| 117 | + if (endTime) { |
| 118 | + lastEndTimestamp = Math.max(lastEndTimestamp, endTime); |
| 119 | + } |
| 120 | + |
| 121 | + startSpanManual( |
| 122 | + { |
| 123 | + name: spanJson.description || 'electron.renderer', |
| 124 | + op: spanJson.op, |
| 125 | + startTime, |
| 126 | + attributes: spanJson.data, |
| 127 | + parentSpan: rendererSpan, |
| 128 | + }, |
| 129 | + (span) => { |
| 130 | + if (spanJson.status) { |
| 131 | + span.setStatus(parseStatus(spanJson.status)); |
| 132 | + } |
| 133 | + |
| 134 | + span.end((endTime || startTime) * 1000); |
| 135 | + }, |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + rendererSpan.end(lastEndTimestamp * 1000); |
| 141 | + }, |
| 142 | + ); |
| 143 | + |
| 144 | + if (event.measurements) { |
| 145 | + for (const [name, measurement] of Object.entries(event.measurements)) { |
| 146 | + setMeasurement(name, measurement.value, measurement.unit, parentSpan); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if (event.contexts?.trace?.data) { |
| 151 | + for (const [key, value] of Object.entries(event.contexts.trace.data)) { |
| 152 | + if (!['sentry.op', 'sentry.origin', 'performance.timeOrigin'].includes(key)) { |
| 153 | + parentSpan.setAttribute(key, value); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return lastEndTimestamp; |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * An integration that instruments Electron's startup sequence. |
| 163 | + * |
| 164 | + * If you also use the `browserTracingIntegration` in the renderer process, the spans created in |
| 165 | + * the renderer will be included in the main process's startup transaction. This allows capturing |
| 166 | + * from main process start until the browser front-end is ready to use. |
| 167 | + * |
| 168 | + * Example: |
| 169 | + * |
| 170 | + * `main.mjs` |
| 171 | + * ```js |
| 172 | + * import { init, startupTracingIntegration } from '@sentry/electron/main'; |
| 173 | + * |
| 174 | + * init({ |
| 175 | + * dsn: '__YOUR_DSN__', |
| 176 | + * tracesSampleRate: 1.0, |
| 177 | + * integrations: [startupTracingIntegration()], |
| 178 | + * }); |
| 179 | + * ``` |
| 180 | + * `renderer.mjs` |
| 181 | + * ```js |
| 182 | + * import { init, browserTracingIntegration } from '@sentry/electron/renderer'; |
| 183 | + * |
| 184 | + * init({ |
| 185 | + * tracesSampleRate: 1.0, |
| 186 | + * integrations: [browserTracingIntegration()], |
| 187 | + * }); |
| 188 | + * ``` |
| 189 | + */ |
| 190 | +export const startupTracingIntegration = defineIntegration((options: StartupTracingOptions = {}) => { |
| 191 | + return { |
| 192 | + name: 'StartupTracing', |
| 193 | + setup() { |
| 194 | + const fallbackTimeout = setTimeout(() => { |
| 195 | + const transaction = rootTransaction(); |
| 196 | + transaction.setStatus({ code: 2, message: 'Timeout exceeded' }); |
| 197 | + transaction.end(); |
| 198 | + }, (options.timeoutSeconds || 10) * 1000); |
| 199 | + |
| 200 | + app.once('will-finish-launching', () => { |
| 201 | + zeroLengthSpan({ |
| 202 | + name: 'will-finish-launching', |
| 203 | + op: 'electron.will-finish-launching', |
| 204 | + }); |
| 205 | + }); |
| 206 | + |
| 207 | + app.once('ready', () => { |
| 208 | + zeroLengthSpan({ |
| 209 | + name: 'ready', |
| 210 | + op: 'electron.ready', |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
| 214 | + app.once('web-contents-created', (_, webContents) => { |
| 215 | + zeroLengthSpan({ |
| 216 | + name: 'web-contents-created', |
| 217 | + op: 'electron.web-contents.created', |
| 218 | + }); |
| 219 | + |
| 220 | + webContents.once('dom-ready', async () => { |
| 221 | + clearTimeout(fallbackTimeout); |
| 222 | + |
| 223 | + const parentSpan = rootTransaction(); |
| 224 | + |
| 225 | + zeroLengthSpan({ |
| 226 | + name: 'dom-ready', |
| 227 | + op: 'electron.web-contents.dom-ready', |
| 228 | + }); |
| 229 | + |
| 230 | + let lastEndTimestamp = timestampInSeconds(); |
| 231 | + |
| 232 | + const event = await waitForRendererPageload((options.timeoutSeconds || 10) * 1000); |
| 233 | + |
| 234 | + lastEndTimestamp = applyRendererSpansAndMeasurements(parentSpan, event, lastEndTimestamp); |
| 235 | + |
| 236 | + parentSpan.end(lastEndTimestamp * 1000); |
| 237 | + }); |
| 238 | + }); |
| 239 | + }, |
| 240 | + }; |
| 241 | +}); |
0 commit comments