Adding XRAY Tracing to non-rest functions e.g., SQS, Cognito Triggers etc

假装没事ソ 提交于 2021-01-27 12:39:18

问题


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

  • Cognito Triggers
  • S3 Event
  • DynamoDB Stream
  • SQS Events

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:

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?

Thanks in advance.


回答1:


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

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

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:

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

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

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.

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.

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

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

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 Update your Lambda code and wrap AWS SDK with the X-Ray SDK. Change:

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

To:

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

As of Release Serverless v140



来源:https://stackoverflow.com/questions/65207520/adding-xray-tracing-to-non-rest-functions-e-g-sqs-cognito-triggers-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!