1

我正在尝试将 AWS X-Ray 与托管在 AWS Lambda(无服务器)上的 nodejs api 集成。X-Ray 使用 express 中间件按 api 的预期工作,并且能够在 AWS 控制台上查看跟踪。对于没有 express 框架的异步函数,我在集成时遇到了问题。

尝试启用手动模式,但面临Lambda 不支持手动模式错误。

提到这个-为自动模式部分开发自定义解决方案但没有运气。

有人可以帮我解决这个问题吗?

'use strict';
const AWSXRay = require('aws-xray-sdk-core');
const Aws = AWSXRay.captureAWS(require('aws-sdk'))
const capturePostgres = require('aws-xray-sdk-postgres');
const { Client } = capturePostgres(require('pg'));

module.exports.test = async (event, context) => {
         var ns = AWSXRay.getNamespace();
         const segment = newAWSXRay.Segment('Notifications_push');
         ns.enter(ns.createContext());
         AWSXRay.setSegment(segment_push);
         .... };
4

1 回答 1

0

因此,在 Lambda 中,SDK 会自动创建一个占位符(外观)段。更深入的解释在这里:https ://github.com/aws/aws-xray-sdk-node/issues/148

所有你需要的是:

const AWSXRay = require('aws-xray-sdk-core');
//lets patch the AWS SDK
const Aws = AWSXRay.captureAWS(require('aws-sdk'));

module.exports.test = async (event, context) => {
  //All capturing will work out of box

  var sqs = new AWS.SQS({apiVersion: '2012-11-05'});
  var params = {...}

  //no need to add code, just regular SQS call
  sqs.sendMessage(params, function(err, data) {
    if (err) {
      console.log("Error", err);
    } else {
      console.log("Success", data.MessageId);
    }
  });

  //if you want to create subsegments manually simply do
  const seg = AWSXRay.getSegment();
  const subseg = seg.addSubsegment('mynewsubsegment');
  subseg.close();
  //no need to close the Lambda segment
};

此处的其他文档:https ://docs.aws.amazon.com/lambda/latest/dg/nodejs-tracing.html

于 2019-07-11T21:00:19.960 回答