Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node v8.5 with --trace-events-enabled not producing trace log file

Tags:

node.js

I'm running node v8.5 and I'm trying to play around with the experimental Tracing feature.

Starting my application node --trace-events-enabled app.js I would expect to see a trace log file generated per the node documentation here https://nodejs.org/api/tracing.html which I can view in chrome by visiting chrome://tracing and loading that generated trace log file.

However, it doesn't seem like node is generating that log file at all. Are there settings I'm missing, or is the log file saved outside my project directory?

like image 315
SupperSam Avatar asked Oct 29 '22 01:10

SupperSam


1 Answers

I have recently tried with node v8.9.1 and the correct creation of the logs dependes on how you close your app.js.

This example's app works correctly: it creates a file called node_trace.1.log in the directory where you start node (node --trace-events-enabled ./bin/trace-me.js will create the file in ./):

console.log("Trace me");

const interv = setInterval(()=>console.log("Runnning"), 1000);

// quit on ctrl-c when running docker in terminal
process.on('SIGINT', function onSigint() {
    console.info('Got SIGINT (aka ctrl-c). Graceful shutdown ', new Date().toISOString());
    clearInterval(interv);
});

process.on('beforeExit', function (exitCode) {
   console.log("Before exit: "+ exitCode);
});

If you kill your process with ctrl-c without managing it, for example, the beforeExit event will not be call and the trace logs will never be created.

The same if you call process.exit():

it will terminate as soon as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr. as described on docs.

So the solution is managing correctly the SIGINT and SIGTERM events and check if the beforeExit is called because it is emitted only when Node.js empties its event loop and is not killed.

like image 143
Manuel Spigolon Avatar answered Nov 15 '22 05:11

Manuel Spigolon