将 XRAY 追踪添加到非静止功能,例如 SQS、Cognito 触发器等 [英] Adding XRAY Tracing to non-rest functions e.g., SQS, Cognito Triggers etc

查看:45
本文介绍了将 XRAY 追踪添加到非静止功能,例如 SQS、Cognito 触发器等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 无服务器框架,我有一些未附加到 API 网关端点的功能,例如如:

Using the Serverless framework, I have functions that aren’t attached to an API Gateway Endpoint, such as:

  • 认知触发器
  • S3 活动
  • DynamoDB 流
  • SQS 活动

我也在使用 XRAY 跟踪,我在 serverless.yml 文件中将其设置为 tracing: true.好像这些功能没有被追踪,我收到的调试信息是:

I am also using XRAY tracing, which I have set as tracing: true in my serverless.yml file. It seems that these functions are not being traced, the debug message I receive is:

忽略子段 20dcd559aa2ab487 上的刷新.相关片段被标记为未采样.

Ignoring flush on subsegment 20dcd559aa2ab487. Associated segment is marked as not sampled.

有没有办法通过无服务器或云形成来添加这些功能?

Is there any way to have these functions added, either via serverless or cloudformation?

提前致谢.

推荐答案

要为所有 Service 的 Lambda 函数启用 X-Ray 跟踪,您只需在提供程序级别设置相应的跟踪配置:

To enable X-Ray tracing for all your Service’s Lambda functions you just need to set the corresponding tracing configuration on the provider level:

provider:
  tracing:
    lambda: true

如果您想在每个函数级别上设置跟踪,您可以在函数定义中使用跟踪配置:

If you want to setup tracing on a per-function level you can use the tracing config in your function definition:

functions:
  myFunction:
    handler: index.handler
    tracing: true

将跟踪设置为 true 会转换为活动跟踪配置.您可以通过以字符串形式提供所需的配置来覆盖此行为:

Setting tracing to true translates to the Active tracing configuration. You can overwrite this behavior by providing the desired configuration as a string:

functions:
  myFunction:
    handler: index.handler
    tracing: PassThrough

另请注意,您可以混合使用提供者级和功能级配置.所有函数都将继承提供者级别的配置,然后可以在单个函数的基础上覆盖:

Also note that you can mix the provider- and function-level configurations. All functions will inherit the provider-level configuration which can then be overwritten on an individual function basis:

service:
  name: my-tracing-service

provider:
  name: aws
 stage: dev
  runtime: nodejs8.10
  tracing:
    lambda: true

functions:
  myFunc1: # this function will inherit the provider-level tracing configuration
    handler: index.func1
  myFunc2:
    handler: handler.func2
    tracing: PassThrough # here we're overwriting the provider-level configuration

建议使用上述跟踪配置为 Lambda 设置 X-Ray 跟踪,因为这样做将确保 X-Ray 设置由无服务器框架核心通过 CloudFormation 管理.

It's recommended to setup X-Ray tracing for Lambda with the aforementioned tracing configuration since doing so will ensure that the X-Ray setup is managed by the Serverless Framework core via CloudFormation.

您可以更细化并指定要跟踪的资源:

You can get more granular and specify which resources you want traced as well:

打开 serverless.yml 并在 provider 部分添加跟踪配置:

Open your serverless.yml and add a tracing config inside the provider section:

provider:
  ...
  tracing:
    apiGateway: true
    lambda: true

重要提示:由于 CloudFormation 的限制,无法在目前不使用跟踪的现有部署上启用 AWS X-Ray 跟踪.

IMPORTANT: Due to CloudFormation limitations it's not possible to enable AWS X-Ray Tracing on existing deployments which don’t use tracing right now.

如果您想使用 AWS X-Ray Tracing for Lambda,请删除旧的无服务器部署并重新部署您的 lambdas 并启用跟踪.

Please remove the old Serverless Deployments and re-deploy your lambdas with tracing enabled if you want to use AWS X-Ray Tracing for Lambda.

最后,不要忘记配置正确的 IAM 权限策略:

Lastly, don't forget to have the right IAM permission policies configured:

provider:
  ...
  iamRoleStatements:
    - Effect: Allow
      Action:
        ...
        - xray:PutTraceSegments
        - xray:PutTelemetryRecords
      Resource: "*"

要为 AWS Lambda 调用的其他 AWS 服务启用 X-Ray 跟踪,您必须安装 AWS X-Ray 开发工具包.在您的项目目录中,运行:

To enable X-Ray tracing for other AWS services invoked by AWS Lambda, you MUST Install the AWS X-Ray SDK. In your project directory, run:

$ npm install -s aws-xray-sdk更新您的 Lambda 代码并使用 X-Ray 开发工具包包装 AWS 开发工具包.更改:

$ npm install -s aws-xray-sdk Update your Lambda code and wrap AWS SDK with the X-Ray SDK. Change:

const AWS = require('aws-sdk');

致:

const AWSXRay = require('aws-xray-sdk-core');
const AWS = AWSXRay.captureAWS(require('aws-sdk'));

截至发布 无服务器 v140

这篇关于将 XRAY 追踪添加到非静止功能,例如 SQS、Cognito 触发器等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆