22

I am trying to use implement the AWS X-ray into my current project (using Node.js and Serverless framework). I am trying to wire the X-ray to one of my lambda function, I got the problem of

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

the other solution I tried is from 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.

pyy
  • 915
  • 3
  • 9
  • 25
  • 1
    When are you getting this error? When testing locally? As the error message says, your context object (your second parameter) seems to be missing the segments it needs. X-Ray wants to use those so you can trace where the call goes, how long every action takes, etc. So for that it needs a valid context. (Ran into the same problem myself when running a local test with a fake context object.) – Hieron Mar 21 '19 at 12:53
  • 1
    @Hieron Thanks for your response. Actually, I am using the serverless framework and I used the serverless-offline plugin to help me run locally. is the context fake or not. I use the solution is to enable the configuration in Lambda function. – pyy Mar 21 '19 at 14:19
  • 1
    Ok, so you are running locally, so it is probably an issue with the context not being exactly the same as it is when actually running on AWS. Which also means it probably isn't an issue with AWS/Lambda, but with the serverless-offline plugin. Maybe ask them, on their github page? – Hieron Mar 21 '19 at 20:49
  • 1
    @pyy, what was the solution? – Victor P Oct 01 '19 at 12:51
  • 1
    @VictorP For now, I am using one Lambda feature by enabling X-ray tracing either in management console or cloudformation template. So X-ray can trace Lambda for now. I will later spend sometimes to try X-ray with other AWS services. – pyy Oct 01 '19 at 13:13

3 Answers3

16

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.

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

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>
r.pedrosa
  • 709
  • 5
  • 12
6

I encountered this error also. To fix it, I disabled XRay when running locally. XRay isn't needed when running locally because I can just set up debug log statements at that time.

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.

Link here

AWSXRay.setContextMissingStrategy("LOG_ERROR");
Tim Nguyen
  • 71
  • 1
  • 2
0

If you don't want the error clogging up your output you can add a helper that ignores only that error.

// 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);
    }
  });
}
lamarrh
  • 1
  • 1