aws / aws-xray-sdk-node

The official AWS X-Ray SDK for Node.js.
Apache License 2.0
272 stars 155 forks source link

Lambda Node 14 - Error: Missing AWS Lambda trace data for X-Ray. Expected _X_AMZN_TRACE_ID to be set #383

Open paul-uz opened 3 years ago

paul-uz commented 3 years ago

I am trying to set up some custom subsegments to gather timings of operations.

Code below:

const AWS = require('aws-sdk');
const AWSXRay = require('aws-xray-sdk');
const xmljs = require('xml-js');

const segment = AWSXRay.getSegment();

const self = {
    handler: async (event, context) => {
    try {
      const xmlsegment = segment.addNewSubsegment('XML');
      const data = xmljs.xml2js(event.body, {compact: true});
      xmlsegment.close();
    }
}

module.exports = self;

When hitting the API endpoint, I'm getting "Error: Missing AWS Lambda trace data for X-Ray. Expected _X_AMZN_TRACE_ID to be set" in the logs.

I am following the information here https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-subsegments.html#xray-sdk-nodejs-subsegments-lambda

willarmiros commented 3 years ago

Hi @paul-uz,

You cannot retrieve any X-Ray subsegments/segments outside the Lambda function handler, which is why you are receiving this error. Moving the getSegment() to within your handler function should fix your issue. See more here: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-tracing.html

paul-uz commented 3 years ago

@willarmiros I suspected as much, and whilst I did not get any errors afterwards, I simply don't think it worked :/

EDIT - I took a look at the page you linked, and it only really shows how to iclude the xray sdk. It lack any real information on how to implement tracing. the code example they give is incomplete :/

awssandra commented 3 years ago

hi paul-uz,

You can enable debug logs on Lambda via the AWS_XRAY_DEBUG_MODE environment variable to better log your trace functionality to verify if the behavior is correct. https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-configuration.html#xray-sdk-nodejs-configuration-logging

Can you post any findings here?

Thanks, Sandra

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in next 7 days. Thank you for your contributions.

diegoquirozramirez commented 3 years ago

This is my solution,

if (process.env.IS_OFFLINE) { AWSXRay.setContextMissingStrategy("LOG_ERROR"); }

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in next 7 days. Thank you for your contributions.