AWS Lambda:识别冷启动 [英] AWS Lambda: Identifying cold starts
问题描述
是否有明确的方法来识别冷启动"?是在Lambda本身中还是在运行时中,还是通过日志?我知道冷启动的特点是运行时间更长,实际上我可以看到,但是我正在寻找一种清晰的方法. 如果那很重要,我正在使用Node.js.
Is there a clear way to identify "cold starts"? Either in runtime in the Lambda itself, or via the logs? I know that cold starts are characterized by longer runtimes, which I can actually see, but I'm looking for a clear cut way. I'm using Node.js if that matters.
更新:对于两个用例,下面有两个很好的答案: -在lambda运行时识别冷启动. -从CloudWatch日志中识别冷启动.
Update: There are two good answers below, for two use cases: - Identifying the cold start as the lambda runs. - Identifying the cold start from the CloudWatch log.
推荐答案
如果在NodeJS脚本的顶部添加一些初始化代码,则可以在代码中告诉您这是一个冷门,并且您将然后就可以记录下来,如果您想在日志中看到它.例如:
If you add some initialization code to the top of your NodeJS script, you will be able to tell in the code that it is a cold start, and you will then be able to log that if you want to see it in the logs. For example:
var coldStart = true;
console.log("This line of code exists outside the handler, and only executes on a cold start");
exports.myHandler = function(event, context, callback) {
if (coldStart) {
console.log("First time the handler was called since this function was deployed in this container");
}
coldStart = false;
...
callback(...);
}
这篇关于AWS Lambda:识别冷启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!