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

启用事务搜索

可以使用事务搜索来增强 Lambda 的可观测性,这样无需采样即可捕获 Lambda 函数调用的所有跟踪跨度。此功能允许收集函数的 100% 跨度,而不受跟踪上下文传播中 sampled 标志的影响。这可确保不会对下游依赖服务产生额外影响。通过在 Lambda 上启用事务搜索,您可以全面了解自己的函数性能,并可以解决很少发生的问题。要开始使用,请参阅 Transaction Search

在控制台中启用 Transaction Search

以下过程介绍如何在控制台中启用 Transaction Search。

在 CloudWatch 控制台中启用 Transaction Search
  1. 通过 https://console.aws.amazon.com/cloudwatch/ 打开 CloudWatch 控制台。

  2. 在导航窗格的 Application Signals 下,选择交易搜索

  3. 选择启用 Transaction Search

  4. 选中将跨度作为结构化日志摄取的框,然后输入要编制索引的跨度百分比。您可以免费将跨度编制 1% 的索引,稍后再根据要求更改百分比。

使用 API 启用 Transaction Search

以下过程介绍如何使用 API 启用 Transaction Search。

第 1 步:创建授予访问在 CloudWatch Logs 中摄取跨度的策略

使用 Amazon CLI 或 SDK 启用 Transaction Search 时,必须使用基于资源的策略(具有 PutResourcePolicy)配置权限。

策略示例

以下示例策略允许 X-Ray 向 CloudWatch Logs 发送跟踪

{ "Version": "2012-10-17", "Statement": [ { "Sid": "TransactionSearchXRayAccess", "Effect": "Allow", "Principal": { "Service": "xray.amazonaws.com" }, "Action": "logs:PutLogEvents", "Resource": [ "arn:partition:logs:region:account-id:log-group:aws/spans:*", "arn:partition:logs:region:account-id:log-group:/aws/application-signals/data:*" ], "Condition": { "ArnLike": { "aws:SourceArn": "arn:partition:xray:region:account-id:*" }, "StringEquals": { "aws:SourceAccount": "account-id" } } } ] }
示例命令

以下示例说明如何使用 PutResourcePolicy 格式化处理 Amazon CLI 命令。

aws logs put-resource-policy --policy-name MyResourcePolicy --policy-document '{ "Version": "2012-10-17", "Statement": [ { "Sid": "TransactionSearchXRayAccess", "Effect": "Allow", "Principal": { "Service": "xray.amazonaws.com" }, "Action": "logs:PutLogEvents", "Resource": [ "arn:partition:logs:region:account-id:log-group:aws/spans:*", "arn:partition:logs:region:account-id:log-group:/aws/application-signals/data:*" ], "Condition": { "ArnLike": { "aws:SourceArn": "arn:partition:logs:region:account-id:*" }, "StringEquals": { "aws:SourceAccount": "account-id" } } } ]}'

第 2 步:配置跟踪分段的目的地

配置具有 UpdateTraceSegmentDestination 的跨度的摄取。

示例命令

以下示例说明如何使用 UpdateTraceSegmentDestination 格式化处理 Amazon CLI 命令。

aws xray update-trace-segment-destination --destination CloudWatchLogs

第 3 步:配置要索引的跨度数量

使用 UpdateIndexingRule 配置所需的采样百分比

示例命令

以下示例说明如何使用 UpdateIndexingRule 格式化处理 Amazon CLI 命令。

aws xray update-indexing-rule --name "Default" --rule '{"Probabilistic": {"DesiredSamplingPercentage": number}}'
注意

启用 Transaction Search 后,可能需要十分钟才能使用跨度进行搜索和分析。

第 4 步:验证跨度是否可供搜索和分析

要验证跨度是否可供搜索和分析,请使用 GetTraceSegmentDestination

示例命令

以下示例说明如何使用 GetTraceSegmentDestination 格式化处理 Amazon CLI 命令。

aws xray get-trace-segment-destination
响应示例

以下示例显示了当 Transaction Search 处于活动状态时的预期响应。

{ "Destination": "CloudWatchLogs", "Status": "ACTIVE" }