[英]Which functions should I use to read aws lambda log
一旦我的 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"
}
}
我想從 cloudwatch 讀取我的運行日志以及我可以在 lambda 監控選項卡中看到的內存使用情況:
如何通過 sdk 做到這一點? 我應該使用哪些功能?
我正在使用 nodejs。
您需要發現已分配給 Lambda 函數調用的日志流名稱。 這在 Lambda 函數的context
可用。
exports.handler = async (event, context) => {
console.log('context', context);
};
結果在以下日志中:
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] }
因此,CloudWatch Logs 流名稱在context.logStreamName
可用。 我不知道有什么 API 可以在事后將 Lambda 請求 ID 映射到日志流名稱,因此您可能需要返回它或以某種方式保留映射。
可以通過 AWS cloudwatch API 查找特定請求 ID 的日志。
您可以使用 [filterLogEvents][1] API 提取(使用正則表達式)相關的 START 和 REPORT 日志以收集內存使用的相關信息(您還將在響應中獲得日志流名稱以備將來使用)。
如果要收集特定調用的所有相關日志,則需要查詢創建 START 和 REPORT 日志對,並查詢特定日志流上特定時間范圍內的所有日志。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.