report
Diagnostic Report
Purpose: Collects and saves information to help diagnose problems in running scripts or programs.
Sections:
Header
Basic information: report version, event (e.g., exception), trigger (e.g., unhandled error), filename, dump time, process ID (PID), command line, Node.js version, OS information (name, version, etc.).
Component versions: Node.js, V8, UV, Zlib, OpenSSL, and other modules.
CPU, network, and host data: CPU speed, network interface details, host machine name, etc.
Example:
JavaScript Stack
Error message: The actual error message encountered.
Stack trace: A list of functions that were called, leading to the error.
Example:
Native Stack
Native call stack: Shows the C++ functions that were called during the error.
Example:
JavaScript Heap
Memory statistics: Total, used, and available memory, garbage collection information, and details about different heap spaces (e.g., read-only, old, code).
Example:
Resource Usage
System resource information: Memory (RSS), CPU usage, page faults, file system activity.
Example:
UV Thread Resource Usage
Resource usage for libuv threads: Similar to the previous section, but specifically for libuv threads.
Example:
Node.js UV Handles
List of UV handles: Different types of handles, such as async, timers, checks, and signals, along with their state and details.
Example:
Workers
Web Workers: If any worker threads were created during the script execution.
Example:
Environment Variables
List of environment variables: Variables passed to the script from the operating system (e.g., PATH, USER, HOME).
Example:
User Limits
System resource limits: Soft and hard limits for various resources (e.g., maximum memory size, CPU time, file size).
Example:
Shared Objects
List of shared libraries: Libraries used by the script (e.g., libc, libdl, etc.).
Example:
Real-World Applications:
Debugging: Diagnosing errors or performance issues in scripts or programs.
Incident response: Analyzing causes and gathering evidence for incidents involving Node.js applications.
Performance analysis: Understanding memory usage, CPU consumption, and other performance metrics.
Security investigations: Identifying potential security vulnerabilities or suspicious activity.
Reporting Runtime Errors and Health Data
Node.js has a reporting module that allows you to generate reports that provide information about the runtime environment, errors, and resource usage of your Node.js applications.
Generating Reports
There are three main ways to generate a report:
Uncaught Exceptions: Node.js will automatically generate a report if an unhandled exception occurs in your code.
Signal: You can send a signal to Node.js (by default,
SIGUSR2
) to trigger a report.Fatal Errors: Node.js will generate a report when a fatal error occurs that causes the application to terminate.
Additional Flags
In addition to the default report generation methods, you can also use the following flags to customize the report:
--report-compact
: Generates a single-line JSON report for easy processing.--report-directory
: Specifies the directory where the report should be saved.--report-filename
: Specifies the name of the report file.--report-signal
: Sets the signal that triggers the report (not supported on Windows).
Generating Reports from JavaScript
You can also generate reports programmatically using the process.report
API:
You can pass a filename to writeReport()
to save the report to a specific file:
You can also pass an Error
object to writeReport()
to include the error stack in the report:
Report Content
Reports contain detailed information about the Node.js runtime environment, including:
Header: Event type, date, time, PID, Node.js version
JavaScript and native stack traces
V8 heap information
libuv
handle informationOS platform information (CPU, memory usage, system limits)
Real-World Applications
Diagnostic reports are useful for:
Debugging unhandled exceptions and fatal errors
Inspecting resource usage and performance bottlenecks
Monitoring application health and stability
Automating error reporting and performance analysis
Customizing Report Generation in Node.js
Runtime Configuration (via process.report
object):
1. Error Triggers:
reportOnFatalError
: Generates a report when a fatal error (internal error) occurs.reportOnSignal
: Generates a report when a specific signal is sent to the process (not supported on Windows).reportOnUncaughtException
: Generates a report when an uncaught exception occurs.
2. Report File:
filename
: Specifies the output file name.directory
: Specifies the directory where the report file will be saved.
Code Snippet:
Environment Variable Configuration:
Environment variables can also be used to configure report generation at startup:
Real-World Applications:
Debugging: Reports can provide valuable information for debugging errors and identifying their causes.
Error Monitoring: Reports can be collected and analyzed to monitor error trends and identify potential issues.
Crash Reporting: Reports can be automatically sent to a central server to track and resolve crashes.
Complete Code Implementation:
Interaction with Workers
Imagine your computer as a house with multiple rooms. The main room is where the main program runs.
Workers are like separate rooms in the house that can run code independently from the main room.
When the main room creates a report, it can also include reports from the workers.
The main room will wait for the workers to finish their reports before finishing its own report. But since computers are very fast, this waiting time is usually very short.
Code Snippet:
Real-World Application:
Workers can be used to perform tasks that can be done independently from the main program, such as:
Data processing
Image manipulation
Video encoding
Background calculations