home / documentation / v18 / report

Diagnostic report

目录

自 v11.8.0 版本开始新增

稳定级别:2 - Stable

Delivers a JSON-formatted diagnostic summary, written to a file.

The report is intended for development, test, and production use, to capture and preserve information for problem determination. It includes JavaScript and native stack traces, heap statistics, platform information, resource usage etc. With the report option enabled, diagnostic reports can be triggered on unhandled exceptions, fatal errors and user signals, in addition to triggering programmatically through API calls.

A complete example report that was generated on an uncaught exception is provided below for reference.

JSON

Usage

BASH
  • --report-uncaught-exception Enables report to be generated on un-caught exceptions. Useful when inspecting JavaScript stack in conjunction with native stack and other runtime environment data.

  • --report-on-signal Enables report to be generated upon receiving the specified (or predefined) signal to the running Node.js process. (See below on how to modify the signal that triggers the report.) Default signal is SIGUSR2. Useful when a report needs to be triggered from another program. Application monitors may leverage this feature to collect report at regular intervals and plot rich set of internal runtime data to their views.

Signal based report generation is not supported in Windows.

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 Enables the report to be triggered on fatal errors (internal errors within the Node.js runtime, such as out of memory) that leads to termination of the application. Useful to inspect various diagnostic data elements such as heap, stack, event loop state, resource consumption etc. to reason about the fatal error.

  • --report-compact Write reports in a compact format, single-line JSON, more easily consumable by log processing systems than the default multi-line format designed for human consumption.

  • --report-directory Location at which the report will be generated.

  • --report-filename Name of the file to which the report will be written.

  • --report-signal Sets or resets the signal for report generation (not supported on Windows). Default signal is SIGUSR2.

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

JS

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

JS

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.

JS

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

JS

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

JS

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.

JS

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.

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:

BASH

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.

Configuration

Additional runtime configuration of report generation is available via the following properties of process.report:

reportOnFatalError triggers diagnostic reporting on fatal errors when true. Defaults to false.

reportOnSignal triggers diagnostic reporting on signal when true. This is not supported on Windows. Defaults to false.

reportOnUncaughtException triggers diagnostic reporting on uncaught exception when true. Defaults to false.

signal specifies the POSIX signal identifier that will be used to intercept external triggers for report generation. Defaults to 'SIGUSR2'.

filename specifies the name of the output file in the file system. Special meaning is attached to stdout and stderr. Usage of these will result in report being written to the associated standard streams. In cases where standard streams are used, the value in directory is ignored. URLs are not supported. Defaults to a composite filename that contains timestamp, PID, and sequence number.

directory specifies the filesystem directory where the report will be written. URLs are not supported. Defaults to the current working directory of the Node.js process.

JS

Configuration on module initialization is also available via environment variables:

BASH

Specific API documentation can be found under process API documentation section.

Interaction with workers

Worker threads can create reports in the same way that the main thread does.

Reports will include information on any Workers that are children of the current thread as part of the workers section, with each Worker generating a report in the standard report format.

The thread which is generating the report will wait for the reports from Worker threads to finish. However, the latency for this will usually be low, as both running JavaScript and the event loop are interrupted to generate the report.