简体   繁体   English

错误:在 Lambda 和 node.js 中使用 AWS X-ray 时,“无法从上下文中获取当前子/段”

[英]Error:"Failed to get the current sub/segment from the context" when use AWS X-ray in Lambda with node.js

I am trying to use implement the AWS X-ray into my current project (using Node.js and Serverless framework).我正在尝试将 AWS X-ray 用于我当前的项目(使用 Node.js 和无服务器框架)。 I am trying to wire the X-ray to one of my lambda function, I got the problem of我正在尝试将 X 射线连接到我的 lambda function 之一,我遇到了问题

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).....

code below:下面的代码:

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) {
        //...
    }
}

for this problem, I use the solution from https://forums.aws.amazon.com/thread.jspa?messageID=821510&#821510对于这个问题,我使用来自https://forums.aws.amazon.com/thread.jspa?messageID=821510&#821510的解决方案

the other solution I tried is from https://forums.aws.amazon.com/thread.jspa?messageID=829923&#829923我尝试的另一个解决方案来自https://forums.aws.amazon.com/thread.jspa?messageID=829923&#829923

code is like代码就像

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});

    //....
}

Still not working...还是行不通...

Appreciated to the help of any kind.感谢任何形式的帮助。

As you mention, that happened because you're running locally (using serverless-offline plugin) and the serverless-offline plugin doesn't provide a valid XRAY context.正如您所提到的,发生这种情况是因为您在本地运行(使用无服务器离线插件)并且无服务器离线插件不提供有效的 XRAY 上下文。

One possible way to pass this error and still be able to call your function locally is setting AWS_XRAY_CONTEXT_MISSING environment variable to LOG_ERROR instead of RUNTIME_ERROR (default).一种可能的方式来传递这个错误,仍然能够调用你的函数是局部设置AWS_XRAY_CONTEXT_MISSING环境变量LOG_ERROR而不是RUNTIME_ERROR (默认)。

Something like : 类似的东西

serverless invoke local -f functionName -e AWS_XRAY_CONTEXT_MISSING=LOG_ERROR

I didn't test this using serverless framework but it worked when the same error occurred calling an amplify function locally :我没有使用无服务器框架对此进行测试,但是当在本地调用放大函数时发生相同的错误时它起作用了:

amplify function invoke <function-name>

I encountered this error also.我也遇到了这个错误。 To fix it, I disabled XRay when running locally.为了解决这个问题,我在本地运行时禁用了 XRay。 XRay isn't needed when running locally because I can just set up debug log statements at that time.在本地运行时不需要 XRay,因为我当时可以设置调试日志语句。

This is what the code would look like这就是代码的样子

let AWS = require('aws-sdk');
if (!process.env.IS_OFFLINE) {
    const AWSXRay = require('aws-xray-sdk');
    AWS = AWSXRay.captureAWS(require('aws-sdk'));
}

If you don't like this approach, you can set up a contextStrategy to not error out when the context is missing.如果您不喜欢这种方法,您可以设置一个 contextStrategy,以便在缺少上下文时不会出错。

Link here 链接在这里

AWSXRay.setContextMissingStrategy("LOG_ERROR");

If you don't want the error clogging up your output you can add a helper that ignores only that error.如果您不希望错误阻塞您的 output,您可以添加一个仅忽略该错误的助手。

// Removes noisy Error: Failed to get the current sub/segment from the context due to Xray
export async function disableXrayError() {
  console.error = jest.fn((err) => {
    if (err.message.includes("Failed to get the current sub/segment from the context")) {
      return;
    } else {
      console.error(err);
    }
  });
}

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

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