ApplicationInsights-node.js icon indicating copy to clipboard operation
ApplicationInsights-node.js copied to clipboard

AppInsights doesn't flush logs from CLI on process.exit()

Open jordanlevy96 opened this issue 5 years ago • 3 comments

I am running a commandline utility in NodeJS that sends its logs to Azure App Insights. We need to use a process.exit() at the end of the program with different codes for our own analytics, but it seems that quitting the program this way prevents the App Insights module from sending its buffer to Azure, since the logs are cut off (i.e., if we had 10 logs in our local console, only 7 of them appear in App Insights). We have even tried manually calling flush() to force the propagation, but it has not changed the behavior.

Here is some sample code:

const appInsights = require('applicationinsights');
...
console.log('some log message');
...
console.log('last log message');
appInsights.defaultClient.flush();
process.exit();

On App Insights, we would only see the first log ("some log message") appear. However, removing the process.exit() call makes both appear.

This appears similar to a previous issue, unable to end node process #331. We tried the dispose() function to force the propagation after calling flush(), but again, no results.

jordanlevy96 avatar Jan 13 '20 21:01 jordanlevy96

The only workaround I found was to use

process.exitCode = 1; // or any other value

instead of the exit(1) function, since it allows a graceful shutdown.

I think the expected behavior from the appinsights nodejs module it to handle the exit event properly and also ensure that flush() is forcing the buffer data to be sent to the AppInsights server.

bigman73 avatar Jan 13 '20 21:01 bigman73

Flush is an asynchronous operation, did you try exiting inside the callback to flush? eg:

client.flush({callback: () => process.exit()});

In general I would expect there's no opportunity for this SDK to send out telemetry if you call process.exit without flush, since process.exit is an immediate termination operation

Edit: RE:

I think the expected behavior from the appinsights nodejs module it to handle the exit event properly and also ensure that flush() is forcing the buffer data to be sent to the AppInsights server.

We should be listening to this to perform our special flush handling in case of app crashes (perhaps a listener is missing here). But because operations on crash/exit need to be synchronous, this only persists telemetry to disk. When the app starts up again it will try to send what was persisted to disk over the network to AI.

OsvaldoRosado avatar Jan 13 '20 22:01 OsvaldoRosado

dang it, I got bit by this too :( @markwolff @OsvaldoRosado could we please add a note in the readme or somewhere in the doc, that says process.exit() will prevent events from being flushed? And in parallel investigate if the sdk can be resilient to process.exit

asklar avatar Sep 25 '20 17:09 asklar

The callback can be used with flush - solution applies to the latest version.

JacksonWeber avatar Apr 24 '24 22:04 JacksonWeber