Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Allow capture of more than 1 ANR event
  • Loading branch information
timfish committed Dec 12, 2024
commit 7697d7b08e56f869b852c1a311b3290849441946
6 changes: 6 additions & 0 deletions packages/node/src/integrations/anr/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export interface AnrIntegrationOptions {
* This uses the node debugger which enables the inspector API and opens the required ports.
*/
captureStackTrace: boolean;
/**
* Maximum number of ANR events to send.
*
* Defaults to 1.
*/
maxAnrEvents: number;
/**
* Tags to include with ANR events.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/integrations/anr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ async function _startWorker(
pollInterval: integrationOptions.pollInterval || DEFAULT_INTERVAL,
anrThreshold: integrationOptions.anrThreshold || DEFAULT_HANG_THRESHOLD,
captureStackTrace: !!integrationOptions.captureStackTrace,
maxAnrEvents: integrationOptions.maxAnrEvents || 1,
staticTags: integrationOptions.staticTags || {},
contexts,
};
Expand Down
18 changes: 10 additions & 8 deletions packages/node/src/integrations/anr/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type VoidFunction = () => void;

const options: WorkerStartData = workerData;
let session: Session | undefined;
let hasSentAnrEvent = false;
let sentAnrEvents = 0;
let mainDebugImages: Record<string, string> = {};

function log(msg: string): void {
Expand Down Expand Up @@ -134,11 +134,11 @@ function applyScopeToEvent(event: Event, scope: ScopeData): void {
}

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

hasSentAnrEvent = true;
sentAnrEvents += 1;

await sendAbnormalSession();

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

// Delay for 5 seconds so that stdio can flush if the main event loop ever restarts.
// This is mainly for the benefit of logging or debugging.
setTimeout(() => {
process.exit(0);
}, 5_000);
if (sentAnrEvents >= options.maxAnrEvents) {
// Delay for 5 seconds so that stdio can flush if the main event loop ever restarts.
// This is mainly for the benefit of logging or debugging.
setTimeout(() => {
process.exit(0);
}, 5_000);
}
}

let debuggerPause: VoidFunction | undefined;
Expand Down
Loading