簡體   English   中英

如何不使用Express中間件為node.js應用配置AWS X-Ray?

[英]How to Configure AWS X-Ray for node.js app not using Express middleware?

我正在嘗試將AWS X-Ray與托管在AWS Lambda(無服務器)上的nodejs api集成在一起。 X-Ray使用快速中間件按預期用於api的方式工作,並且能夠在AWS Console上查看跟蹤。 對於沒有快速框架的異步功能,我在集成時會遇到問題。

嘗試啟用手動模式,但面臨-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);
         .... };

因此,在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM