I'm trying to log Application Insights metrics from AWS Lambda written in node.js. Here is my code:
const appInsights = require("applicationinsights");
appInsights.setup("<guid>");
module.exports.monitor = (event, context, callback) => {
let client = appInsights.defaultClient;
// context.callbackWaitsForEmptyEventLoop = false;
client.trackMetric({name: "AI Test", value: 25});
const response = { statusCode: 200, body: 'done' };
callback(null, response);
};
The problem is that Lambda calls time out.
If I uncomment context.callbackWaitsForEmptyEventLoop = false;
statement, the timeouts are gone and AWS works. But I only receive one metric value in Application Insights: from the very first call. Subsequent calls don't seem to come through anymore.
How can I adjust Application Insights client code to work reliably and without causing timeouts? What's going on here?