Skip to content

Commit 450b420

Browse files
committed
feat: Allow capture of more than 1 ANR event
1 parent db33945 commit 450b420

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

packages/node/src/integrations/anr/common.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export interface AnrIntegrationOptions {
2121
* This uses the node debugger which enables the inspector API and opens the required ports.
2222
*/
2323
captureStackTrace: boolean;
24+
/**
25+
* Maximum number of ANR events to send.
26+
*
27+
* Defaults to 1.
28+
*/
29+
maxAnrEvents: number;
2430
/**
2531
* Tags to include with ANR events.
2632
*/

packages/node/src/integrations/anr/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ async function _startWorker(
160160
pollInterval: integrationOptions.pollInterval || DEFAULT_INTERVAL,
161161
anrThreshold: integrationOptions.anrThreshold || DEFAULT_HANG_THRESHOLD,
162162
captureStackTrace: !!integrationOptions.captureStackTrace,
163+
maxAnrEvents: integrationOptions.maxAnrEvents || 1,
163164
staticTags: integrationOptions.staticTags || {},
164165
contexts,
165166
};

packages/node/src/integrations/anr/worker.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type VoidFunction = () => void;
2323

2424
const options: WorkerStartData = workerData;
2525
let session: Session | undefined;
26-
let hasSentAnrEvent = false;
26+
let sentAnrEvents = 0;
2727
let mainDebugImages: Record<string, string> = {};
2828

2929
function log(msg: string): void {
@@ -134,11 +134,11 @@ function applyScopeToEvent(event: Event, scope: ScopeData): void {
134134
}
135135

136136
async function sendAnrEvent(frames?: StackFrame[], scope?: ScopeData): Promise<void> {
137-
if (hasSentAnrEvent) {
137+
if (sentAnrEvents >= options.maxAnrEvents) {
138138
return;
139139
}
140140

141-
hasSentAnrEvent = true;
141+
sentAnrEvents += 1;
142142

143143
await sendAbnormalSession();
144144

@@ -179,11 +179,13 @@ async function sendAnrEvent(frames?: StackFrame[], scope?: ScopeData): Promise<v
179179
await transport.send(envelope);
180180
await transport.flush(2000);
181181

182-
// Delay for 5 seconds so that stdio can flush if the main event loop ever restarts.
183-
// This is mainly for the benefit of logging or debugging.
184-
setTimeout(() => {
185-
process.exit(0);
186-
}, 5_000);
182+
if (sentAnrEvents >= options.maxAnrEvents) {
183+
// Delay for 5 seconds so that stdio can flush if the main event loop ever restarts.
184+
// This is mainly for the benefit of logging or debugging.
185+
setTimeout(() => {
186+
process.exit(0);
187+
}, 5_000);
188+
}
187189
}
188190

189191
let debuggerPause: VoidFunction | undefined;

0 commit comments

Comments
 (0)