简体   繁体   English

如何使用 AWS X-Ray 跟踪 lambda 调用 lambda?

[英]How to trace a lambda invokes a lambda with AWS X-Ray?

I have an AWS Lambda function which is triggered by an API Gateway event.我有一个由 API 网关事件触发的 AWS Lambda function。 The API Gateway is configured to use X-Ray. API 网关配置为使用 X-Ray。

As the Lambda tracing configuration defaults to PassTrough it is also shown in X-Ray (service map, etc.).由于 Lambda 跟踪配置默认为 PassTrough,因此它也显示在 X-Ray 中(服务 map 等)。

The invoked Lambda uses the node.js aws-sdk to invoke another lambda.调用的 Lambda 使用 node.js aws-sdk 调用另一个 lambda。 If I understand correctly the Tracing ID has to be passed on to the next Invocation in order to show this Lambda also in X-Ray.如果我理解正确,则必须将跟踪 ID 传递给下一次调用,以便在 X 射线中也显示此 Lambda。 In the API of the SDK I found no option for this.SDK 的 API 中,我没有找到任何选项。

const result = await lambda
        .invoke(lambdaParamsCreateUser)
        .promise()

How can I achieve this?我怎样才能做到这一点? How can I trace also the invocation of the original request?如何跟踪原始请求的调用?

With the tips of @Balu Vyamajala I changed the AWS-SDK import to the following:在@Balu Vyamajala 的提示下,我将 AWS-SDK 导入更改为以下内容:

import AWS from "aws-sdk";

import AwsXRay from "aws-xray-sdk-core";
const aws = AwsXRay.captureAWS(AWS);
export default aws;

I use it when I invoice my second function like this:我在为我的第二个 function 开具发票时使用它,如下所示:

import AWS from "aws";
const Lambda = AWS.Lambda;
// ...
const lambda = new Lambda({ region: "eu-central-1" });
const lambdaPromise = lambda
        .invoke({
            FunctionName: AUTH_CREATE_USER_FUNC,
            InvocationType: "RequestResponse",
            Qualifier: AUTH_CREATE_USER_FUNC_VERSION,
            Payload: JSON.stringify({
                eMail: eMail,
                device: device,
                customerId: customerId,
            }),
            LogType: "Tail",
        })
        .promise()

But in X-Ray there is no invocation chain:-(但是在 X-Ray 中没有调用链:-(

https://imgur.com/wDMlNzb https://imgur.com/wDMlNzb

Do I make a mistake?我犯错了吗?

if we enable X-Ray for both Lambda functions, trace-id is automatically passed and will be same for both Lambdas.如果我们为两个 Lambda 函数启用 X-Ray,trace-id 会自动传递,并且对于两个 Lambda 都是相同的。

In the code, we can enable X-Ray simply by wrapping it around aws-sdk在代码中,我们只需将 X-Ray 包裹在 aws-sdk 周围即可启用

JavaScript: JavaScript:

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

Typescript: Typescript:

import AWSXRay from 'aws-xray-sdk';
import aws from 'aws-sdk';
const AWS = AWSXRay.captureAWS(aws)

Here is a sample test to confirm.这是一个样本测试来确认。

balu-test >> sample-test balu-test >> sample-test

Lambda 1 (balu-test): Lambda 1(巴鲁测试):

const AWSXRay = require("aws-xray-sdk-core");
const AWS = AWSXRay.captureAWS(require("aws-sdk"));    
const lambda = new AWS.Lambda();

exports.handler = async function (event, context) {
  var params = {
    FunctionName: "sample-test",
    InvocationType: "RequestResponse",
    Payload: '{ "name" : "foo" }',
  };
  
  const response = await lambda.invoke(params).promise();
  console.log('response',response);

  return "sucess";
};

Lambda 2(sample-test): Lambda 2(样品测试):

const AWSXRay = require("aws-xray-sdk-core");
const AWS = AWSXRay.captureAWS(require("aws-sdk"));    
let region = "us-east-1"
let secretName = "SomeSecret"
let secret
let decodedBinarySecret    
var client = new AWS.SecretsManager({
  region: region,
});

exports.handler = (event, context, callback) => {
  client.getSecretValue({ SecretId: secretName }, function (err, data) {
    if (err) {
      callback(err);
    } else {
      if ("SecretString" in data) {
        secret = data.SecretString;
      } else {
        let buff = new Buffer(data.SecretBinary, "base64");
        decodedBinarySecret = buff.toString("ascii");
      }
      callback(null, secret);
    }
  });
};

TraceId is same and X-Ray points to same graph for both Lambda invocations. TraceId 是相同的,并且 X-Ray 对于 Lambda 调用都指向相同的图形。 Same thing happens when first api is called from Api-Gateway.当从 Api-Gateway 调用第一个 api 时,也会发生同样的事情。 First time trace-id is generated and is passed along as http header to downstream processes.第一次生成跟踪 ID 并作为 http header 传递到下游进程。

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM