Is it possible to print the stack trace of a nodejs app when it is becoming very slow or froze to get information about performance spikes?
This would be incredibly helpful in instances where the reproduction for the issue is unknown.
In Java this saved hundreds of hours and was straight forward:
Is something like this possible with nodejs?
FI: the nodejs diagnostics report doesn't contain any javascript stack trace when initiated from a sig kill event.
@isaacs answer is correct, but if you need more specific or cleaner error stack, you can use this function: function getCleanerStack () { var err = new Error (); Error.captureStackTrace (err, getStack); return err.stack; } This function is inspired directly from the console.trace function in NodeJS. Source code: Recent version or Old version.
There are the following methods by which we can get a stack trace for JavaScript when throwing an exception. Using console.trace: The console object also has a method called console.trace () method, which gives you the trace on the console. Every time when it is called stack trace generate for the function.
you can use node-stack-trace module which is a power full module to track call stacks.
This is a method of Java’s throwable class that prints the throwable Exception object as well as with other Information like the line number where Exception occurs and class name where the exception occurred. Throwable is super-class for all exception classes. Both the stack traces are discussed below:
You are looking for checking if event loop is blocked or slow. There is a npm package https://www.npmjs.com/package/blocked-at that detects slow synchronous execution and report where it started.
Usage:
const blocked = require('blocked-at');
blocked((time, stack) => {
console.log(`Blocked for ${time}ms, operation started here:`, stack)
});
from scratch you can implement yourself a check in this way:
var interval = 500;
var interval = setInterval(function() {
var last = process.hrtime();
setImmediate(function() {
var delta = process.hrtime(last);
if (delta > blockDelta) {
console.log("node.eventloop_blocked", delta);
}
});
}, interval);
The idea is: if the timer doesn't fire after the expected time, this mean that event loop was blocked in some operation.
This snippet check if event loop is blocked for more than 500 ms. Isn't perfect, I'm suggest to use blocked-at for more robust control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With