使用方法


【Usage】

node --report-uncaught-exception --report-on-signal \
--report-on-fatalerror app.js 
  • --report-uncaught-exception 允许在未捕获的异常发生时生成报告。在结合原生堆栈和其他运行时环境数据检查 JavaScript 堆栈时非常有用。
  • --report-on-signal 允许在接收到指定的(或预定义的)信号时生成报告,以作用于正在运行的 Node.js 进程。(见下文关于如何修改触发报告的信号。)默认信号是 SIGUSR2。当需要从另一个程序触发报告时,这个功能非常有用。应用监控器可以利用该功能定期收集报告,并将丰富的内部运行时数据绘制到它们的视图中。

Windows 不支持基于信号的报告生成。

【Signal based report generation is not supported in Windows.】

在正常情况下,无需修改报告触发信号。然而,如果 SIGUSR2 已经用于其他用途,则此标志可以帮助更改报告生成的信号,同时保留 SIGUSR2 对上述用途的原始含义。

【Under normal circumstances, there is no need to modify the report triggering signal. However, if SIGUSR2 is already used for other purposes, then this flag helps to change the signal for report generation and preserve the original meaning of SIGUSR2 for the said purposes.】

  • --report-on-fatalerror 启用在致命错误(Node.js 运行时的内部错误,例如内存不足)导致应用终止时触发报告。可用于检查各种诊断数据元素,例如堆、栈、事件循环状态、资源消耗等,以分析致命错误的原因。
  • --report-compact 以紧凑格式生成报告,单行 JSON,比默认为人类阅读设计的多行格式更容易被日志处理系统使用。
  • --report-directory 生成报告的位置。
  • --report-filename 报告将被写入的文件名。
  • --report-signal 设置或重置报告生成的信号(不支持 Windows)。默认信号是 SIGUSR2

报告也可以通过 JavaScript 应用的 API 调用触发:

【A report can also be triggered via an API call from a JavaScript application:】

process.report.writeReport(); 

此函数接受一个可选的附加参数 filename,它是写入报告的文件名。

【This function takes an optional additional argument filename, which is the name of a file into which the report is written.】

process.report.writeReport('./foo.json'); 

此函数接受一个可选的附加参数 err,它是一个 Error 对象,用作报告中打印的 JavaScript 堆栈的上下文。当在回调或异常处理程序中使用 report 来处理错误时,这允许报告包含原始错误的位置以及错误被处理的位置。

【This function takes an optional additional argument err which is an Error object that will be used as the context for the JavaScript stack printed in the report. When using report to handle errors in a callback or an exception handler, this allows the report to include the location of the original error as well as where it was handled.】

try {
  process.chdir('/non-existent-path');
} catch (err) {
  process.report.writeReport(err);
}
// Any other code 

如果同时向 writeReport() 传递了文件名和错误对象,错误对象必须作为第二个参数。

【If both filename and error object are passed to writeReport() the error object must be the second parameter.】

try {
  process.chdir('/non-existent-path');
} catch (err) {
  process.report.writeReport(filename, err);
}
// Any other code 

诊断报告的内容可以通过 JavaScript 应用的 API 调用以 JavaScript 对象的形式返回:

【The content of the diagnostic report can be returned as a JavaScript Object via an API call from a JavaScript application:】

const report = process.report.getReport();
console.log(typeof report === 'object'); // true

// Similar to process.report.writeReport() output
console.log(JSON.stringify(report, null, 2)); 

此函数接受一个可选的附加参数 err,它是一个 Error 对象,将用作报告中打印的 JavaScript 堆栈的上下文。

【This function takes an optional additional argument err, which is an Error object that will be used as the context for the JavaScript stack printed in the report.】

const report = process.report.getReport(new Error('custom error'));
console.log(typeof report === 'object'); // true 

API 版本在从应用内部检查运行时状态时非常有用,以期望自我调整资源消耗、负载均衡、监控等。

【The API versions are useful when inspecting the runtime state from within the application, in expectation of self-adjusting the resource consumption, load balancing, monitoring etc.】

报告的内容包括一个标题部分,包含事件类型、日期、时间、PID 和 Node.js 版本,包含 JavaScript 和本地堆栈跟踪的部分,包含 V8 堆信息的部分,包含 libuv 句柄信息的部分,以及显示 CPU 和内存使用情况及系统限制的操作系统平台信息部分。可以使用 Node.js REPL 触发示例报告:

【The content of the report consists of a header section containing the event type, date, time, PID, and Node.js version, sections containing JavaScript and native stack traces, a section containing V8 heap information, a section containing libuv handle information, and an OS platform information section showing CPU and memory usage and system limits. An example report can be triggered using the Node.js REPL:】

$ node
> process.report.writeReport();
Writing Node.js report to file: report.20181126.091102.8480.0.001.json
Node.js report completed
> 

当报告被写入时,会向标准错误输出(stderr)发送开始和结束的消息,并将报告的文件名返回给调用者。默认的文件名包括日期、时间、进程ID(PID)和一个序列号。序列号有助于在同一 Node.js 进程多次生成报告时,将报告转储与运行时状态关联起来。

【When a report is written, start and end messages are issued to stderr and the filename of the report is returned to the caller. The default filename includes the date, time, PID, and a sequence number. The sequence number helps in associating the report dump with the runtime state if generated multiple times for the same Node.js process.】