Code Monkey home page Code Monkey logo

Comments (8)

awssandra avatar awssandra commented on June 9, 2024

Hi Scandinaro,

We have an open issue with Dynamo Document Client here:
#23

Could you verify the work-around fixes your context propagation issue?

Thanks,
Sandra

from aws-xray-sdk-node.

scandinaro avatar scandinaro commented on June 9, 2024

@awssandra above I'm using one example from that linked issue solution, and then below I just tried the last suggested solution from @monken, but I'm still receiving the same error. It seems like maybe this solution works for express, but not restify?

const AWS           = require('./aws');
const AWSXray       = require('aws-xray-sdk');
const dynamoOptions = {
    httpOptions: {
        timeout: 2000
    },
    maxRetries: 0
};

const dynamoDb = new AWS.DynamoDB.DocumentClient({
    service: new AWS.DynamoDB(dynamoOptions)
});

AWSXray.captureAWSClient(dynamoDb.service);

from aws-xray-sdk-node.

awssandra avatar awssandra commented on June 9, 2024

That should not be the case. The SDK uses CLS behind the scenes to propagate context, regardless of the framework in use. The context is either getting lost as a part of the Restify hook, or in the AWS call.

Do any other AWS calls work?
Can you verify if a "xray.getSegment()" call in the route returns the base segment?
What version of Restify are you using?
Is the "xray.enable(/server/, /name/)" call immediately before your defined routes (after any/all interceptors/hooks)?

from aws-xray-sdk-node.

scandinaro avatar scandinaro commented on June 9, 2024

@awssandra

Do any other AWS calls work?

Yes, we are individually using capture aws client on the cognito client successfully. Dynamo is the only aws service we are using which is having this issue obviously at least somewhat related to using the document client vs the core dynamo module. this is less relevant because of a below answer

Can you verify if a "xray.getSegment()" call in the route returns the base segment?

I was able to confirm that calling it in the route before doing anything else, results in an unhandled promise rejection thrown Error: Failed to get the current sub/segment from the context. this is less relevant because of a below answer

What version of Restify are you using?

We are running 4.1.1 in production, but I just tried upgrading to 7.3.0 but we are still throwing the same error for both trying to call xray.getSegment() as well as when trying to use capture client on the dynamo module. Since I just went through the trouble of replacing all our restify middleware with the 5.0+ compatible styles we will now be running 7.3 in production shortly. this is less relevant because of a below answer

Is the "xray.enable(/server/, /name/)" call immediately before your defined routes (after any/all interceptors/hooks)?

This seems to be the source of some of our issues. We use the aws cognito module in our authentication middleware to authenticate API requests for some routes. Removing the xray client wrapper from cognito and moving the xray enable command after our last middleware seems to fix the dynamo segment error that we were getting on all requests with subsequent dynamo db calls, but now it seems that while we always have access to the xray segment in the route, it only makes it to the dynamo module on some requests and not others. It seems like it might somehow be related to the HTTP Method? It seems like it might work on GETs but not POST and DELETE? In one example, on a DELETE HTTP request we actually have back to back dynamodb calls. The first call is a dynamob.get() request which doesn't throw the sub/segment error, then the second request fired syncronously after the first is a dynamodb.put request which does throw the error.

from aws-xray-sdk-node.

scandinaro avatar scandinaro commented on June 9, 2024

@awssandra just following back up on this.

from aws-xray-sdk-node.

awssandra avatar awssandra commented on June 9, 2024

Hi scandinaro,

Apologies for the delay, much of the team was out for vacation (myself included) and recently back. We're diving into this and will update you as soon as we have more information.

Thanks,
Sandra

from aws-xray-sdk-node.

chrisradek avatar chrisradek commented on June 9, 2024

@scandinaro
I'm trying to reproduce the issue you're seeing but haven't had much luck yet. Here's my full code based on the original example you provided:

dynamodb.js

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

const options = {
  httpOptions: {
    timeout: 2000
  },
  maxRetries: 0,
  region: 'us-west-2'
};

const client = xray.captureAWSClient(new AWS.DynamoDB(options));
const docClient = new AWS.DynamoDB.DocumentClient(options);
docClient.service = client;

module.exports = {
  client: docClient
};

app.js

const resitfy = require('restify');
const xray = require('aws-xray-sdk-restify');
const client = require('./dynamodb').client;

function createServer() {
  const server = resitfy.createServer({
    name: 'api'
  });

  xray.enable(server, 'restify-root');

  server.use(resitfy.plugins.dateParser());
  server.use(resitfy.plugins.queryParser());
  server.use(resitfy.plugins.bodyParser());

  server.get('/foo', function(req, res, next) {
    client.scan({
      TableName: 'TABLE'
    }, function(err, data) {
      if (err) {
        res.send(err.statusCode, err.message);
        next(err);
      } else {
        res.send(200, data);
        next();
      }
    });
  });

  server.get('/bar', function(req, res, next) {
    client.get({
      TableName: 'TABLE',
      Key: {
        'id': 'ID'
      }
    }, function(err, data) {
      if (err) {
        res.send(err.statusCode, err.message);
        next(err);
      } else {
        data.Item.timestamp = Date.now();
        client.put({
          TableName: 'TABLE',
          Item: data.Item,
          ReturnValues: 'ALL_OLD'
        }, function(err, data) {
          if (err) {
            res.send(err.statusCode, err.message);
            next(err);
          } else {
            res.send(200, data);
            next();
          }
        });
      }
    });
  });

  return server;
}

const server = createServer();

server.listen(3002);

Where do you attach your auth middleware? And are you using promises in your routes/middleware? I tried making the GET /bar route match your last use case where you get an item, then put an item as a result. Let me know if what you have differs from what I've put together.

from aws-xray-sdk-node.

chrisradek avatar chrisradek commented on June 9, 2024

Closing due to inactivity, but feel free to open a new issue if you are still encountering this error.

from aws-xray-sdk-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.