Skip to content

Commit 782c55a

Browse files
AbhiPrasadtimfishLuca Forstner
authored
feat(node): Add docs for ANR tracking (#7943)
Co-authored-by: Tim Fish <[email protected]> Co-authored-by: Luca Forstner <[email protected]>
1 parent 8a27203 commit 782c55a

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Loading
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
title: Application Not Responding (ANR)
3+
sidebar_order: 70
4+
description: "Learn how to turn off or specify ANRs for Node.js"
5+
keywords:
6+
[
7+
"anr",
8+
"Application Not Responding",
9+
"Event Loop Blocked",
10+
"Event Loop Stalls",
11+
]
12+
---
13+
14+
Application Not Responding (ANR) errors, or Event Loop Stall errors, are triggered when the Node.js main thread event loop of an application is blocked for more than five seconds. The Node SDK reports ANR errors as Sentry events and can optionally attach a stacktrace of the blocking code to the ANR event.
15+
16+
<Include name="feature-stage-beta.mdx" />
17+
18+
_(Available in version 7.72.0 and above)_
19+
20+
To enable ANR detection, import and use the `enableANRDetection` function from the `@sentry/node` package before you run the rest of your application code. Any event loop blocking before calling `enableANRDetection` will not be detected by the SDK.
21+
22+
<Alert level="info">
23+
24+
ANR detection requires Node 10 or higher.
25+
26+
</Alert>
27+
28+
```javascript {tableTitle: ESM}
29+
import * as Sentry from "@sentry/node";
30+
31+
Sentry.init({
32+
dsn: "___PUBLIC_DSN___",
33+
tracesSampleRate: 1.0,
34+
});
35+
36+
await Sentry.enableANRDetection({ captureStackTrace: true });
37+
// Function that runs your app
38+
runApp();
39+
```
40+
41+
```javascript {tableTitle: CJS}
42+
const Sentry = require("@sentry/node");
43+
44+
Sentry.init({
45+
dsn: "___PUBLIC_DSN___",
46+
tracesSampleRate: 1.0,
47+
});
48+
49+
Sentry.enableANRDetection({ captureStackTrace: true }).then(() => {
50+
// Function that runs your app
51+
runApp();
52+
});
53+
```
54+
55+
![Example of an ANR error event](anr-node-example.png)
56+
57+
<Alert level="warning">
58+
59+
ANR detection is not supported for [Node.js clusters](https://nodejs.org/api/cluster.html).
60+
61+
</Alert>
62+
63+
## Configuration options
64+
65+
You can pass in a configuration object to the `enableANRDetection` function to customize the ANR detection behavior.
66+
67+
```ts
68+
declare function enableAnrDetection(options: Partial<Options>): Promise<void>;
69+
```
70+
71+
```ts
72+
interface Options {
73+
/**
74+
* The app entry script. This is used to run the same script as the child process.
75+
*
76+
* Defaults to `process.argv[1]`.
77+
*/
78+
entryScript: string;
79+
/**
80+
* Interval to send heartbeat messages to the child process.
81+
*
82+
* Defaults to 50ms.
83+
*/
84+
pollInterval: number;
85+
/**
86+
* Threshold in milliseconds to trigger an ANR event.
87+
*
88+
* Defaults to 5000ms.
89+
*/
90+
anrThreshold: number;
91+
/**
92+
* Whether to capture a stack trace when the ANR event is triggered.
93+
*
94+
* Defaults to `false`.
95+
*
96+
* This uses the node debugger which enables the inspector API and opens the required ports.
97+
*/
98+
captureStackTrace: boolean;
99+
/**
100+
* Log debug information.
101+
*/
102+
debug: boolean;
103+
}
104+
```
105+
106+
## ANR Implementation and Overhead
107+
108+
ANR detection with the Node SDK works with a forked child process. The child process runs the same entry point as the main app. To ensure that the main app code does not run in the child process, we use a promise that only resolves in the main process.
109+
110+
The main app process sends a heartbeat message to the child process every 50ms. If the child process does not receive a heartbeat message for 5 seconds, it triggers an ANR event. If the `captureStackTrace` option is enabled, the child process uses WebSockets to capture stack traces via the [v8 inspector API](https://nodejs.org/api/inspector.html).
111+
112+
Once an ANR event has been reported, the child process exits to prevent further duplicate events. The main process will still continue to run like usual.
113+
114+
In general overhead from using Node.js ANR tracking is expected to be minimal. With no ANR detected, the only overhead in the main app process is polling the child process over IPC by default every 50ms. The ANR child process consumes around 50-60 MB or RAM and simply keeps track of the polling. Once ANR has been detected, the main process will get paused briefly in the debugger to capture a stack trace frames. At this point, the event loop has been blocked for seconds so the debugging overhead can be considered negligible.

0 commit comments

Comments
 (0)