我正在尝试将AWS X-ray集成到我的项目中(使用Node.js和Serverless框架)。我正在尝试将X-ray连接到我的一个Lambda函数上,但是我遇到了问题。
Error: Failed to get the current sub/segment from the context.
at Object.contextMissingRuntimeError [as contextMissing] (/.../node_modules/aws-xray-sdk-core/lib/context_utils.js:21:15)
at Object.getSegment (/.../node_modules/aws-xray-sdk-core/lib/context_utils.js:92:45)
at Object.resolveSegment (/.../node_modules/aws-xray-sdk-core/lib/context_utils.js:73:19).....
以下是代码:
import { DynamoDB } from "aws-sdk";
import AWSXRay from 'aws-xray-sdk';
export const handler = async (event, context, callback) => {
const dynamo = new DynamoDB.DocumentClient({
service: new DynamoDB({ region })
});
AWSXRay.captureAWSClient(dynamo.service);
try {
// call dynamoDB function
} catch(err) {
//...
}
}
对于这个问题,我使用了来自https://forums.aws.amazon.com/thread.jspa?messageID=821510󈤆的解决方案。
我尝试过的另一个解决方案来自https://forums.aws.amazon.com/thread.jspa?messageID=829923󊧣
代码如下:
import AWSXRay from 'aws-xray-sdk';
const AWS = AWSXRay.captureAWS(require('aws-sdk'));
export const handler = async (event, context, callback) => {
const dynamo = new AWS.DynamoDB.DocumentClient({region});
//....
}
依然无法正常工作...
感激任何形式的帮助。