-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ANR detection for main process (#753)
- Loading branch information
Showing
8 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { enableAnrDetection as enableNodeAnrDetection } from '@sentry/node'; | ||
import { app } from 'electron'; | ||
|
||
import { ELECTRON_MAJOR_VERSION } from './electron-normalize'; | ||
|
||
type MainProcessOptions = Parameters<typeof enableNodeAnrDetection>[0]; | ||
|
||
interface Options { | ||
/** | ||
* Main process ANR options. | ||
* | ||
* Set to false to disable ANR detection in the main process. | ||
*/ | ||
mainProcess?: MainProcessOptions | false; | ||
} | ||
|
||
function enableAnrMainProcess(options: MainProcessOptions): Promise<void> { | ||
if (ELECTRON_MAJOR_VERSION < 4) { | ||
throw new Error('Main process ANR detection is only supported on Electron v4+'); | ||
} | ||
|
||
const mainOptions = { | ||
entryScript: app.getAppPath(), | ||
...options, | ||
}; | ||
|
||
return enableNodeAnrDetection(mainOptions); | ||
} | ||
|
||
/** | ||
* **Note** This feature is still in beta so there may be breaking changes in future releases. | ||
* | ||
* Starts a child process that detects Application Not Responding (ANR) errors. | ||
* | ||
* It's important to await on the returned promise before your app code to ensure this code does not run in the ANR | ||
* child process. | ||
* | ||
* ```js | ||
* import { init, enableAnrDetection } from '@sentry/electron'; | ||
* | ||
* init({ dsn: "__DSN__" }); | ||
* | ||
* // with ESM + Electron v28+ | ||
* await enableAnrDetection({ mainProcess: { captureStackTrace: true }}); | ||
* runApp(); | ||
* | ||
* // with CJS | ||
* enableAnrDetection({ mainProcess: { captureStackTrace: true }}).then(() => { | ||
* runApp(); | ||
* }); | ||
* ``` | ||
*/ | ||
export async function enableAnrDetection(options: Options = {}): Promise<void> { | ||
if (options.mainProcess !== false) { | ||
return enableAnrMainProcess(options.mainProcess || {}); | ||
} | ||
|
||
return Promise.resolve(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{ | ||
"method": "envelope", | ||
"sentryKey": "37f8a2ee37c0409d8970bc7559c7c7e4", | ||
"appId": "277345", | ||
"data": { | ||
"sdk": { | ||
"name": "sentry.javascript.electron", | ||
"packages": [ | ||
{ | ||
"name": "npm:@sentry/electron", | ||
"version": "{{version}}" | ||
} | ||
], | ||
"version": "{{version}}" | ||
}, | ||
"contexts": { | ||
"app": { | ||
"app_name": "anr-main", | ||
"app_version": "1.0.0", | ||
"app_start_time": "{{time}}" | ||
}, | ||
"browser": { | ||
"name": "Chrome" | ||
}, | ||
"chrome": { | ||
"name": "Chrome", | ||
"type": "runtime", | ||
"version": "{{version}}" | ||
}, | ||
"device": { | ||
"arch": "{{arch}}", | ||
"family": "Desktop", | ||
"memory_size": 0, | ||
"free_memory": 0, | ||
"processor_count": 0, | ||
"processor_frequency": 0, | ||
"cpu_description": "{{cpu}}", | ||
"screen_resolution": "{{screen}}", | ||
"screen_density": 1, | ||
"language": "{{language}}" | ||
}, | ||
"node": { | ||
"name": "Node", | ||
"type": "runtime", | ||
"version": "{{version}}" | ||
}, | ||
"os": { | ||
"name": "{{platform}}", | ||
"version": "{{version}}" | ||
}, | ||
"runtime": { | ||
"name": "Electron", | ||
"version": "{{version}}" | ||
} | ||
}, | ||
"release": "[email protected]", | ||
"environment": "development", | ||
"user": { | ||
"ip_address": "{{auto}}" | ||
}, | ||
"exception": { | ||
"values": [ | ||
{ | ||
"type": "ApplicationNotResponding", | ||
"value": "Application Not Responding for at least 1000 ms", | ||
"mechanism": { "type": "ANR" }, | ||
"stacktrace": { | ||
"frames": [ | ||
{ | ||
"colno": 0, | ||
"function": "{{function}}", | ||
"in_app": false, | ||
"lineno": 0, | ||
"module": "pbkdf2" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
}, | ||
"level": "error", | ||
"event_id": "{{id}}", | ||
"platform": "node", | ||
"timestamp": 0, | ||
"breadcrumbs": [], | ||
"tags": { | ||
"event.environment": "javascript", | ||
"event.origin": "electron", | ||
"event.process": "browser", | ||
"event_type": "javascript", | ||
"process.name": "ANR" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "anr-main", | ||
"version": "1.0.0", | ||
"main": "src/main.js", | ||
"dependencies": { | ||
"@sentry/electron": "3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
description: ANR Main Event | ||
category: ANR | ||
command: yarn | ||
condition: version.major >= 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const crypto = require('crypto'); | ||
|
||
const { app } = require('electron'); | ||
const { init, enableAnrDetection } = require('@sentry/electron/main'); | ||
|
||
init({ | ||
dsn: '__DSN__', | ||
debug: true, | ||
autoSessionTracking: false, | ||
onFatalError: () => {}, | ||
}); | ||
|
||
function longWork() { | ||
for (let i = 0; i < 100; i++) { | ||
const salt = crypto.randomBytes(128).toString('base64'); | ||
// eslint-disable-next-line no-unused-vars | ||
const hash = crypto.pbkdf2Sync('myPassword', salt, 10000, 512, 'sha512'); | ||
} | ||
} | ||
|
||
enableAnrDetection({ mainProcess: { debug: true, anrThreshold: 1000, captureStackTrace: true } }).then(() => { | ||
console.log('main app code'); | ||
app.on('ready', () => { | ||
setTimeout(() => { | ||
longWork(); | ||
}, 1000); | ||
}); | ||
}); |