|
1 | 1 | # `@sentry-internal/node-native-stacktrace`
|
2 | 2 |
|
3 |
| -Native Node module to capture stack traces from all registered threads. |
| 3 | +A native Node.js module that can capture JavaScript stack traces for registered |
| 4 | +main or worker threads from any other thread, even if event loops are blocked. |
4 | 5 |
|
5 |
| -This allows capturing main and worker thread stack traces from another watchdog |
6 |
| -thread, even if the event loops are blocked. |
| 6 | +The module also provides a means to create a watchdog system to track event loop |
| 7 | +blocking via periodic heartbeats. When the time from the last heartbeat crosses |
| 8 | +a threshold, JavaScript stack traces can be captured. The heartbeats can |
| 9 | +optionally include state information which is included with the corresponding |
| 10 | +stack trace. |
7 | 11 |
|
8 |
| -In the main or worker threads: |
| 12 | +## Basic Usage |
| 13 | + |
| 14 | +### 1. Register threads you want to monitor |
| 15 | + |
| 16 | +In your main thread or worker threads: |
9 | 17 |
|
10 | 18 | ```ts
|
11 |
| -const { registerThread } = require("@sentry-internal/node-native-stacktrace"); |
| 19 | +import { registerThread } from "@sentry-internal/node-native-stacktrace"; |
12 | 20 |
|
| 21 | +// Register this thread for monitoring |
13 | 22 | registerThread();
|
14 | 23 | ```
|
15 | 24 |
|
16 |
| -Watchdog thread: |
| 25 | +### 2. Capture stack traces from any thread |
17 | 26 |
|
18 | 27 | ```ts
|
19 |
| -const { |
20 |
| - captureStackTrace, |
21 |
| -} = require("@sentry-internal/node-native-stacktrace"); |
| 28 | +import { captureStackTrace } from "@sentry-internal/node-native-stacktrace"; |
22 | 29 |
|
| 30 | +// Capture stack traces from all registered threads |
23 | 31 | const stacks = captureStackTrace();
|
24 | 32 | console.log(stacks);
|
25 | 33 | ```
|
26 | 34 |
|
27 |
| -Results in: |
| 35 | +### Example Output |
| 36 | + |
| 37 | +Stack traces show where each thread is currently executing: |
28 | 38 |
|
29 | 39 | ```js
|
30 | 40 | {
|
@@ -83,50 +93,99 @@ Results in:
|
83 | 93 | }
|
84 | 94 | ```
|
85 | 95 |
|
86 |
| -## Detecting blocked event loops |
| 96 | +## Advanced Usage: Automatic blocked event loop Detection |
| 97 | + |
| 98 | +Set up automatic detection of blocked event loops: |
| 99 | + |
| 100 | +### 1. Set up thread heartbeats |
87 | 101 |
|
88 |
| -In the main or worker threads if you call `registerThread()` regularly, times |
89 |
| -are recorded. |
| 102 | +Send regular heartbeats with optional state information: |
90 | 103 |
|
91 | 104 | ```ts
|
92 |
| -const { |
| 105 | +import { |
93 | 106 | registerThread,
|
94 | 107 | threadPoll,
|
95 |
| -} = require("@sentry-internal/node-native-stacktrace"); |
| 108 | +} from "@sentry-internal/node-native-stacktrace"; |
96 | 109 |
|
| 110 | +// Register this thread |
97 | 111 | registerThread();
|
98 | 112 |
|
| 113 | +// Send heartbeats every 200ms with optional state |
99 | 114 | setInterval(() => {
|
100 |
| - threadPoll({ optional_state: "some_value" }); |
| 115 | + threadPoll({ |
| 116 | + endpoint: "/api/current-request", |
| 117 | + userId: getCurrentUserId(), |
| 118 | + }); |
101 | 119 | }, 200);
|
102 | 120 | ```
|
103 | 121 |
|
104 |
| -In the watchdog thread you can call `getThreadsLastSeen()` to get how long it's |
105 |
| -been in milliseconds since each thread polled. |
| 122 | +### 2. Monitor from a watchdog thread |
106 | 123 |
|
107 |
| -If any thread has exceeded a threshold, you can call `captureStackTrace()` to |
108 |
| -get the stack traces for all threads. |
| 124 | +Monitor all registered threads from a dedicated thread: |
109 | 125 |
|
110 | 126 | ```ts
|
111 |
| -const { |
| 127 | +import { |
112 | 128 | captureStackTrace,
|
113 | 129 | getThreadsLastSeen,
|
114 |
| -} = require("@sentry-internal/node-native-stacktrace"); |
| 130 | +} from "@sentry-internal/node-native-stacktrace"; |
115 | 131 |
|
116 | 132 | const THRESHOLD = 1000; // 1 second
|
117 | 133 |
|
118 | 134 | setInterval(() => {
|
119 |
| - for (const [thread, time] in Object.entries(getThreadsLastSeen())) { |
120 |
| - if (time > THRESHOLD) { |
121 |
| - const threads = captureStackTrace(); |
122 |
| - const blockedThread = threads[thread]; |
123 |
| - const { frames, state } = blockedThread; |
124 |
| - console.log( |
125 |
| - `Thread '${thread}' blocked more than ${THRESHOLD}ms`, |
126 |
| - frames, |
127 |
| - state, |
128 |
| - ); |
| 135 | + const threadsLastSeen = getThreadsLastSeen(); |
| 136 | + |
| 137 | + for (const [threadId, timeSinceLastSeen] of Object.entries(threadsLastSeen)) { |
| 138 | + if (timeSinceLastSeen > THRESHOLD) { |
| 139 | + // Thread appears to be blocked - capture diagnostics |
| 140 | + const stackTraces = captureStackTrace(); |
| 141 | + const blockedThread = stackTraces[threadId]; |
| 142 | + |
| 143 | + console.error(`🚨 Thread ${threadId} blocked for ${timeSinceLastSeen}ms`); |
| 144 | + console.error("Stack trace:", blockedThread.frames); |
| 145 | + console.error("Last known state:", blockedThread.state); |
129 | 146 | }
|
130 | 147 | }
|
131 |
| -}, 1000); |
| 148 | +}, 500); // Check every 500ms |
| 149 | +``` |
| 150 | + |
| 151 | +## API Reference |
| 152 | + |
| 153 | +### Functions |
| 154 | + |
| 155 | +#### `registerThread(threadName?: string): void` |
| 156 | + |
| 157 | +Registers the current thread for monitoring. Must be called from each thread you |
| 158 | +want to capture stack traces from. |
| 159 | + |
| 160 | +- `threadName` (optional): Name for the thread. Defaults to the current thread |
| 161 | + ID. |
| 162 | + |
| 163 | +#### `captureStackTrace<State>(): Record<string, Thread<State>>` |
| 164 | + |
| 165 | +Captures stack traces from all registered threads. Can be called from any thread |
| 166 | +but will not capture the stack trace of the calling thread itself. |
| 167 | + |
| 168 | +```ts |
| 169 | +type Thread<S> = { |
| 170 | + frames: StackFrame[]; |
| 171 | + state?: S; |
| 172 | +}; |
| 173 | + |
| 174 | +type StackFrame = { |
| 175 | + function: string; |
| 176 | + filename: string; |
| 177 | + lineno: number; |
| 178 | + colno: number; |
| 179 | +}; |
132 | 180 | ```
|
| 181 | + |
| 182 | +#### `threadPoll<State>(state?: State): void` |
| 183 | + |
| 184 | +Sends a heartbeat from the current thread with optional state information. The |
| 185 | +state object will be serialized and included as a JavaScript object with the |
| 186 | +corresponding stack trace. |
| 187 | + |
| 188 | +#### `getThreadsLastSeen(): Record<string, number>` |
| 189 | + |
| 190 | +Returns the time in milliseconds since each registered thread called |
| 191 | +`threadPoll()`. |
0 commit comments