View a markdown version of this page

监控 Lambda SnapStart - Amazon Lambda
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)。

监控 Lambda SnapStart

您可以使用 Amazon CloudWatch、Amazon X-Ray 和 使用遥测 API 访问扩展的实时遥测数据 监控 Lambda SnapStart 函数。

注意

AWS_LAMBDA_LOG_GROUP_NAME 和 AWS_LAMBDA_LOG_STREAM_NAME 环境变量在 Lambda SnapStart 函数中不可用。

了解使用 SnapStart 的日志记录和计费行为

SnapStart 函数的 CloudWatch 日志流格式存在一些差异:

  • 初始化日志 – 创建新的执行环境时,REPORT 不会包含 Init Duration 字段。这是因为 Lambda 在您创建版本时而不是在函数调用期间初始化 SnapStart 函数。对于 SnapStart 函数,Init Duration 字段在 INIT_REPORT 记录中。此记录显示 Init 阶段 的持续时间详细信息,包括任何 beforeCheckpoint 运行时挂钩的持续时间。

  • 调用日志 – 创建新的执行环境时,REPORT 会包括 Restore Duration 和 Billed Restore Duration 字段:

    • Restore Duration:Lambda 还原快照、加载运行时和运行任何还原后运行时挂钩所花费的时间。恢复快照的过程可能包含在 MicroVM 之外的活动上花费的时间。Restore Duration 中报告了此时间。

    • Billed Restore Duration:Lambda 加载运行时和运行任何还原后运行时挂钩所花费的时间。

注意

与所有 Lambda 函数一样,持续时间收费适用于在函数处理程序中运行的代码。对于 SnapStart 函数,持续时间费用也适用于在处理程序之外声明的初始化代码、运行时加载所需的时间,以及在运行时钩子中运行的任何代码。

冷启动持续时间为 Restore Duration + Duration 的总和。

以下示例为 Lambda Insights 查询,该查询返回 SnapStart 函数的延迟百分位数。有关 Lambda Insights 查询的更多信息,请参阅 使用查询排除函数故障的示例工作流程。

filter @type = "REPORT" | parse @log /\d+:\/aws\/lambda\/(?<function>.*)/ | parse @message /Restore Duration: (?<restoreDuration>.*?) ms/ | stats count(*) as invocations, pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 50) as p50, pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 90) as p90, pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 99) as p99, pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 99.9) as p99.9 group by function, (ispresent(@initDuration) or ispresent(restoreDuration)) as coldstart | sort by coldstart desc

适用于 SnapStart 的 X-Ray 活动跟踪

您可以使用 X-Ray 来跟踪向 Lambda SnapStart 函数发送的请求。SnapStart 函数的 X-Ray 子分段存在一些差异:

  • SnapStart 函数没有 Initialization 子分段。

  • Restore 子分段会显示 Lambda 还原快照、加载运行时和运行任何还原后运行时挂钩所花费的时间。恢复快照的过程可能包含在 MicroVM 之外的活动上花费的时间。该时间在 Restore 子分段中报告。您无需为在 microVM 之外还原快照所花费的时间付费。

SnapStart 的遥测 API 事件

Lambda 将以下 SnapStart 事件发送到 遥测 API:

Amazon API Gateway 和函数 URL 指标

如果您使用 API Gateway 创建 Web API,则可以使用 IntegrationLatency 指标来测量端到端延迟(API Gateway 将请求转发到后端和从后端收到响应之间的时间)。

如果使用 Lambda 函数 URL,则可以使用 UrlRequestLatency 指标来测量端到端延迟(函数 URL 收到请求和函数 URL 返回响应之间的时间)。