[英]Which functions should I use to read aws lambda log
Once my lambda run is finished, I am getting this payload as a result:一旦我的 lambda 运行完成,我就会得到这个有效负载:
{
"version": "1.0",
"timestamp": "2020-09-30T19:20:03.360Z",
"requestContext": {
"requestId": "2de65baf-f630-48a7-881a-ce3145f1127d",
"functionArn": "arn:aws:lambda:us-east-2:044739556748:function:puppeteer:$LATEST",
"condition": "Success",
"approximateInvokeCount": 1
},
"responseContext": {
"statusCode": 200,
"executedVersion": "$LATEST"
}
}
I would like to read logs of my run from cloudwatch and also memory usage which I can see in lambda monitoring tab:我想从 cloudwatch 读取我的运行日志以及我可以在 lambda 监控选项卡中看到的内存使用情况:
How can do it via sdk?如何通过 sdk 做到这一点? Which functions should I use?我应该使用哪些功能?
I am using nodejs.我正在使用 nodejs。
You need to discover the log stream name that has been assigned to the Lambda function invocation.您需要发现已分配给 Lambda 函数调用的日志流名称。 This is available inside the Lambda function's context
.这在 Lambda 函数的context
可用。
exports.handler = async (event, context) => {
console.log('context', context);
};
Results in the following log:结果在以下日志中:
context { callbackWaitsForEmptyEventLoop: [Getter/Setter],
succeed: [Function],
fail: [Function],
done: [Function],
functionVersion: '$LATEST',
functionName: 'test-log',
memoryLimitInMB: '128',
logGroupName: '/aws/lambda/test-log',
logStreamName: '2020/10/03/[$LATEST]f123a3c1bca123df8c12e7c12c8fe13e',
clientContext: undefined,
identity: undefined,
invokedFunctionArn: 'arn:aws:lambda:us-east-1:123456781234:function:test-log',
awsRequestId: 'e1234567-6b7c-4477-ac3d-74bc62b97bb2',
getRemainingTimeInMillis: [Function: getRemainingTimeInMillis] }
So, the CloudWatch Logs stream name is available in context.logStreamName
.因此,CloudWatch Logs 流名称在context.logStreamName
可用。 I'm not aware of an API to map a Lambda request ID to a log stream name after the fact, so you may need to return this or somehow persist the mapping.我不知道有什么 API 可以在事后将 Lambda 请求 ID 映射到日志流名称,因此您可能需要返回它或以某种方式保留映射。
Finding logs of a specific request-id can be done via AWS cloudwatch API.可以通过 AWS cloudwatch API 查找特定请求 ID 的日志。
You can use [filterLogEvents][1] API to extract (using regex) the relevant START and REPORT logs to gather the relevant information of the memory usage (You will also get the log stream name in the response for future use).您可以使用 [filterLogEvents][1] API 提取(使用正则表达式)相关的 START 和 REPORT 日志以收集内存使用的相关信息(您还将在响应中获得日志流名称以备将来使用)。
If you want to gather all the relevant logs of a specific invocation you will need to query create pairs of START and REPORT logs and query for all the logs in the specific timeframe between them on a specific log stream.如果要收集特定调用的所有相关日志,则需要查询创建 START 和 REPORT 日志对,并查询特定日志流上特定时间范围内的所有日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.