aws / aws-xray-sdk-node

The official AWS X-Ray SDK for Node.js.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: parent.addNewSubsegmentWithoutSampling is not a function

yvele opened this issue · comments

When updating dependencies:

  • aws-sdk: 2.1231.0 → 2.1254.0
  • aws-xray-sdk-core: 3.3.8 → 3.4.0

I got a Node.js error

TypeError: parent.addNewSubsegmentWithoutSampling is not a function

With the following callstack:

 TypeError: parent.addNewSubsegmentWithoutSampling is not a function
     at features.constructor.captureAWSRequest [as customRequestHandler] (webpack://my-package/./node_modules/aws-xray-sdk-core/dist/lib/patchers/aws_p.js?:67:29)
     at features.constructor.addAllRequestListeners (webpack://my-package/./node_modules/aws-sdk/lib/service.js?:302:12)
     at features.constructor.makeRequest (webpack://my-package/./node_modules/aws-sdk/lib/service.js?:222:10)
     at features.constructor.svc.<computed> [as executeStatement] (webpack://my-package/./node_modules/aws-sdk/lib/service.js?:706:23)
     at DataService.query (webpack://my-package/./node_modules/@hapticmedia/aws-rds/lib/DataService/DataService.js?:99:37)
     at getShowcaseReleases (webpack://my-package/./src/resolvers/getShowcaseReleases.js?:45:16)
     at ResolverRouter.resolveEventBatch (webpack://my-package/./node_modules/my-helper/lib/ResolverRouter/ResolverRouter.js?:270:33)
     at ResolverRouter.resolveEvent (webpack://my-package/./node_modules/my-helper/lib/ResolverRouter/ResolverRouter.js?:323:12)
     at handleCore (webpack://my-package/./src/main.js?:100:126)
     at Runtime.handle [as handler] (webpack://my-package/./src/main.js?:106:22)

@yvele Can you please provide a code snippet of the segments / subsegments you are creating in this instance that lead you to this error? Does this only happen when you make calls to the aws sdk (v2) or do you also see it with aws sdk v3 and HTTP calls?

commented

I'm having the same issue as well. It was working fine with version 3.3.8 but not 3.4.0

2022-11-16T15:20:07.261Z	180aa6ef-7cd7-4c89-a23c-c7c14a59af76	INFO	retrieveFromS3 error: TypeError: parent.addNewSubsegmentWithoutSampling is not a function
    at features.constructor.captureAWSRequest [as customRequestHandler] (/opt/nodejs/node_modules/aws-xray-sdk-core/dist/lib/patchers/aws_p.js:67:29)
    at features.constructor.addAllRequestListeners (/opt/nodejs/node_modules/aws-sdk/lib/service.js:298:34)
    at features.constructor.makeRequest (/opt/nodejs/node_modules/aws-sdk/lib/service.js:222:10)
    at features.constructor.svc.<computed> [as getObject] (/opt/nodejs/node_modules/aws-sdk/lib/service.js:706:23)
    at retrieveFromS3 (/var/task/lambda/v1/helper.js:464:25)
    at Object.helper.retrieveFromS3 (/var/task/lambda/v1/helper.js:437:10)
    ...

It failed at helper.retrieveFromS3 function.

helper.retrieveFromS3 = function (params) {
  return retrieveFromS3(params);
};
async function retrieveFromS3(params) {
  try {
    let data = await s3.getObject(params).promise();
    return data.Body.toString("utf8");
  } catch (error) {
    console.log("retrieveFromS3 error:", error);
    throw error;
  }
}

Hi folks - are you all running in Lambda?

Hi all. I have been trying to reproduce the error mentioned above through testing using our Node SDK sample app, which uses the latest version of the node SDK (3.4.0) and AWS SDK v2, as well as through lambda but still cannot see the error you are reporting. Can you please provide more context on your applications and, if possible, a more complete code example that we can use to reproduce this issue?

Here is what I have tried so far using the latest version. I do not run into any errors and can see a complete service map on the AWS X-Ray console (including the instrumented AWS SDK calls).

  • Running the X-Ray Node SDK express sample app using the instructions in the corresponding repo's ReadMe. Both endpoints for the AWS SDK call and HTTP call are working and sending traces to the X-Ray backend on my end.
  • Using the X-Ray SDK locally to write a function that makes + instruments AWS SDK calls to S3. This is similar to the following point but tests the SDK outside of a lambda environment.
  • Creating a lambda function locally, installing the X-Ray SDK and AWS SDK v2 using npm install commands, running the zip -r lambdaFunc.zip . command to create a zip containing the lambda function and node_modules folder, and uploading that .zip file to lambda. This is the lambda function:
const { Segment } = require('aws-xray-sdk');
const xray = require('aws-xray-sdk');

const AWS = xray.captureAWS(require('aws-sdk'));

exports.handler = async (event, context) => {        
    let toSample = false;  // this can be modified - set to false to skip to the case of an unsampled facade segment and execute the line that seems to be throwing an error

    if(toSample){
        let facade = xray.getSegment();
        let test = facade.addNewSubsegment('sqs-subsegment-sampled');
        console.log("doing batch work - sampled");
          try {
            const s3 = new AWS.S3();
            let data = await s3.listBuckets().promise();
            console.log(data);
          } catch (error) {
            console.log("retrieveFromS3 error:", error);
            throw error;
          }
        test.close()
    }
    else {
        let facade = xray.getSegment();
        facade.notTraced = true
        let unsampled = facade.addNewSubsegmentWithoutSampling('sqs-subsegment-unsampled')
          try {
            const s3 = new AWS.S3();
            let data = await s3.listBuckets().promise();
            console.log(data);
          } catch (error) {
            console.log("retrieveFromS3 error:", error);
            throw error;
          }
        unsampled.close();
    }
    return 'Success';
}

If possible, please try to test one of the options above and let us know if you still see the same error on your end. Otherwise, please provide a more complete code snippet (like a complete lambda function) that we can use to reproduce the issue.

commented

Hi folks - are you all running in Lambda?

Yes, we are running in Lambda with Nodejs16.

Hi all. I have been trying to reproduce the error mentioned above through testing using our Node SDK sample app, which uses the latest version of the node SDK (3.4.0) and AWS SDK v2, as well as through lambda but still cannot see the error you are reporting. Can you please provide more context on your applications and, if possible, a more complete code example that we can use to reproduce this issue?

Here is what I have tried so far using the latest version. I do not run into any errors and can see a complete service map on the AWS X-Ray console (including the instrumented AWS SDK calls).

  • Running the X-Ray Node SDK express sample app using the instructions in the corresponding repo's ReadMe. Both endpoints for the AWS SDK call and HTTP call are working and sending traces to the X-Ray backend on my end.
  • Using the X-Ray SDK locally to write a function that makes + instruments AWS SDK calls to S3. This is similar to the following point but tests the SDK outside of a lambda environment.
  • Creating a lambda function locally, installing the X-Ray SDK and AWS SDK v2 using npm install commands, running the zip -r lambdaFunc.zip . command to create a zip containing the lambda function and node_modules folder, and uploading that .zip file to lambda. This is the lambda function:
const { Segment } = require('aws-xray-sdk');
const xray = require('aws-xray-sdk');

const AWS = xray.captureAWS(require('aws-sdk'));

exports.handler = async (event, context) => {        
    let toSample = false;  // this can be modified - set to false to skip to the case of an unsampled facade segment and execute the line that seems to be throwing an error

    if(toSample){
        let facade = xray.getSegment();
        let test = facade.addNewSubsegment('sqs-subsegment-sampled');
        console.log("doing batch work - sampled");
          try {
            const s3 = new AWS.S3();
            let data = await s3.listBuckets().promise();
            console.log(data);
          } catch (error) {
            console.log("retrieveFromS3 error:", error);
            throw error;
          }
        test.close()
    }
    else {
        let facade = xray.getSegment();
        facade.notTraced = true
        let unsampled = facade.addNewSubsegmentWithoutSampling('sqs-subsegment-unsampled')
          try {
            const s3 = new AWS.S3();
            let data = await s3.listBuckets().promise();
            console.log(data);
          } catch (error) {
            console.log("retrieveFromS3 error:", error);
            throw error;
          }
        unsampled.close();
    }
    return 'Success';
}

If possible, please try to test one of the options above and let us know if you still see the same error on your end. Otherwise, please provide a more complete code snippet (like a complete lambda function) that we can use to reproduce the issue.

We managed to simulate the issue on our side.

Our lambda function uses one shared layer, which still uses aws-xray-sdk-core v3.3.8, as the shared layer was deployed a few weeks ago. (Before the release of v3.4.0)

For our application lambda, the codebuild installs the latest v3.4.0 for our function, as we are using "aws-xray-sdk-core": "^3.3.4". (Compatible with version)

Example code with issue:

const helperLayer = require("helperLayer"); // Shared layer - using v3.3.8
const AWSXRay = require('aws-xray-sdk-core'); // This lambda - using v3.4.0
const AWS = AWSXRay.captureAWS(require('aws-sdk'));
...

We solved the issue by redeploying the shared layer to allow it to pull the latest version of aws-xray-sdk-core, so our shared layer and application lambda is using the same & latest version of aws-xray-sdk-core.

Another trick to overcome this is to do this, not sure why. 😶

const AWSXRay = require('aws-xray-sdk-core'); // This lambda - using v3.4.0
const AWS = AWSXRay.captureAWS(require('aws-sdk'));
const helperLayer = require("helperLayer"); // Shared layer - using v3.3.8
...

Hi folks - are you all running in Lambda?

Yes Lambda with Node.js v16

@carolabadeer the issue has been fixed? 🤔 There is no new release after the 3.4.0 https://github.com/aws/aws-xray-sdk-node/releases could you provide us with more information please? Thanks

@carolabadeer @yvele I'm still getting this error. Was there a fix for this?