Skip to content

fix(node): Remove ANR debug option and instead add logger.isEnabled() #9230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion packages/node-integration-tests/suites/anr/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ setTimeout(() => {
Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
debug: true,
beforeSend: event => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(event));
},
});

Sentry.enableAnrDetection({ captureStackTrace: true, anrThreshold: 200, debug: true }).then(() => {
Sentry.enableAnrDetection({ captureStackTrace: true, anrThreshold: 200 }).then(() => {
function longWork() {
for (let i = 0; i < 100; i++) {
const salt = crypto.randomBytes(128).toString('base64');
Expand Down
3 changes: 2 additions & 1 deletion packages/node-integration-tests/suites/anr/basic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ setTimeout(() => {
Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
debug: true,
beforeSend: event => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(event));
},
});

await Sentry.enableAnrDetection({ captureStackTrace: true, anrThreshold: 200, debug: true });
await Sentry.enableAnrDetection({ captureStackTrace: true, anrThreshold: 200 });

function longWork() {
for (let i = 0; i < 100; i++) {
Expand Down
3 changes: 2 additions & 1 deletion packages/node-integration-tests/suites/anr/forked.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ setTimeout(() => {
Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
debug: true,
beforeSend: event => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(event));
},
});

Sentry.enableAnrDetection({ captureStackTrace: true, anrThreshold: 200, debug: true }).then(() => {
Sentry.enableAnrDetection({ captureStackTrace: true, anrThreshold: 200 }).then(() => {
function longWork() {
for (let i = 0; i < 100; i++) {
const salt = crypto.randomBytes(128).toString('base64');
Expand Down
22 changes: 19 additions & 3 deletions packages/node-integration-tests/suites/anr/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import * as path from 'path';

const NODE_VERSION = parseSemver(process.versions.node).major || 0;

/** The output will contain logging so we need to find the line that parses as JSON */
function parseJsonLine<T>(input: string): T {
return (
input
.split('\n')
.map(line => {
try {
return JSON.parse(line) as T;
} catch {
return undefined;
}
})
.filter(a => a) as T[]
)[0];
}

describe('should report ANR when event loop blocked', () => {
test('CJS', done => {
// The stack trace is different when node < 12
Expand All @@ -15,7 +31,7 @@ describe('should report ANR when event loop blocked', () => {
const testScriptPath = path.resolve(__dirname, 'basic.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
const event = JSON.parse(stdout) as Event;
const event = parseJsonLine<Event>(stdout);

expect(event.exception?.values?.[0].mechanism).toEqual({ type: 'ANR' });
expect(event.exception?.values?.[0].type).toEqual('ApplicationNotResponding');
Expand All @@ -42,7 +58,7 @@ describe('should report ANR when event loop blocked', () => {
const testScriptPath = path.resolve(__dirname, 'basic.mjs');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
const event = JSON.parse(stdout) as Event;
const event = parseJsonLine<Event>(stdout);

expect(event.exception?.values?.[0].mechanism).toEqual({ type: 'ANR' });
expect(event.exception?.values?.[0].type).toEqual('ApplicationNotResponding');
Expand All @@ -64,7 +80,7 @@ describe('should report ANR when event loop blocked', () => {
const testScriptPath = path.resolve(__dirname, 'forker.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
const event = JSON.parse(stdout) as Event;
const event = parseJsonLine<Event>(stdout);

expect(event.exception?.values?.[0].mechanism).toEqual({ type: 'ANR' });
expect(event.exception?.values?.[0].type).toEqual('ApplicationNotResponding');
Expand Down
13 changes: 5 additions & 8 deletions packages/node/src/anr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface Options {
*/
captureStackTrace: boolean;
/**
* Log debug information.
* @deprecated Use 'init' debug option instead
*/
debug: boolean;
}
Expand Down Expand Up @@ -94,9 +94,7 @@ function startInspector(startPort: number = 9229): string | undefined {

function startChildProcess(options: Options): void {
function log(message: string, ...args: unknown[]): void {
if (options.debug) {
logger.log(`[ANR] ${message}`, ...args);
}
logger.log(`[ANR] ${message}`, ...args);
}

try {
Expand All @@ -111,7 +109,7 @@ function startChildProcess(options: Options): void {

const child = spawn(process.execPath, [options.entryScript], {
env,
stdio: options.debug ? ['inherit', 'inherit', 'inherit', 'ipc'] : ['ignore', 'ignore', 'ignore', 'ipc'],
stdio: logger.isEnabled() ? ['inherit', 'inherit', 'inherit', 'ipc'] : ['ignore', 'ignore', 'ignore', 'ipc'],
});
// The child process should not keep the main process alive
child.unref();
Expand Down Expand Up @@ -142,9 +140,7 @@ function startChildProcess(options: Options): void {

function handleChildProcess(options: Options): void {
function log(message: string): void {
if (options.debug) {
logger.log(`[ANR child process] ${message}`);
}
logger.log(`[ANR child process] ${message}`);
}

process.title = 'sentry-anr';
Expand Down Expand Up @@ -233,6 +229,7 @@ export function enableAnrDetection(options: Partial<Options>): Promise<void> {
pollInterval: options.pollInterval || DEFAULT_INTERVAL,
anrThreshold: options.anrThreshold || DEFAULT_HANG_THRESHOLD,
captureStackTrace: !!options.captureStackTrace,
// eslint-disable-next-line deprecation/deprecation
debug: !!options.debug,
};

Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const originalConsoleMethods: {
interface Logger extends LoggerConsoleMethods {
disable(): void;
enable(): void;
isEnabled(): boolean;
}

/**
Expand Down Expand Up @@ -63,6 +64,7 @@ function makeLogger(): Logger {
disable: () => {
enabled = false;
},
isEnabled: () => enabled,
};

if (__DEBUG_BUILD__) {
Expand Down