Search code examples
amazon-web-servicesaws-lambdaamazon-dynamodbaws-xray

How to make querying dynamodb xray as a subsegment of a customised segment?


I have a lambda function which queries dynamodb multiple times. I'd like to use xray to get the performance about the query.

I created the lambda in nodejs:

this.documentClient = new DocumentClient({
      service: new DynamoDB(),
    });
AWSXray.captureAWSClient((this.documentClient as any).service);

when querying dynamodb, I am using the code:

const segment = AWSXRay.getSegment();
const subsegment = segment.addNewSubsegment('query dynamodb');
await this.documentClient.query(...).promise();
await this.documentClient.query(...).promise();
await this.documentClient.query(...).promise();
...
subsegment.close();

I am able to see the trace data:

enter image description here

But the problem is the query dynamodb segment is not the parent of other dynamodb query subsegments. How can I wrap all dynamodb query inside my customised segment?


Solution

  • I think the problem here is that the query dynamodb subsegment is not being set in the context for the DynamoDB subsegments to be parented. As a result, the parent for all the subsegments is the Lambda FacadeSegment. You can try using the Manual Mode to manually pass around the subsegment to the DDB calls. Refer to the example for details.