aws/aws-xray-sdk-java

Subsegment missing in trace

Ftwpker opened this issue · 2 comments

Hi all,

I'm currently trying to setup individual subsegments for a dynamoDB and SQS call to see their individual performance.

My project is in Java using Micronaut and GraalVM (I followed these steps to add the needed classes for reflection to work)

After doing so I was able to see subsegment traces for SQS but when I add in a subsegment for dynamoDB call, I no longer see the SQS subsegment info for that trace.

After debugging a bit I see the parent traceIDs are the same for both but at the end of the call, there is only one subsegment added to the current segment which is the dynamo one.

Here are the way my current subsegments are setup:

SQS
`

SendMessageRequest sendMessageRequest =
    SendMessageRequest.builder().messageBody(message).queueUrl(queueUrl).build();

Subsegment subsegment = AWSXRay.beginSubsegment("sqs");

return sqsAsyncClient
    .sendMessage(sendMessageRequest)
    .thenAccept(
        x -> {
          AWSXRay.endSubsegment(subsegment);
          log.info("Successfully queued request: {}", message);
        })
    .exceptionally(throwable -> handleExceptions(throwable, message));`

I know I need to also close the subsegment in case of exception but for testing purposes I haven't done so yet and all my request are successful so far.

DynamoDb

`

Subsegment dbSegment = AWSXRay.beginSubsegment("db call");

boolean result =
    dynamoDao
        .messageExists(message)
        .thenApply(
            x -> {
              AWSXRay.endSubsegment(dbSegment);
              log.info("Successfully checked message status");
              return x;
            })
        .exceptionally(
            exception -> {
              return false;
            })
        .join();

return result;`

Hi @Ftwpker,

You can instrument individual clients made with the AWS SDK so that subsegments are created automatically for those downstream calls. This is documented here: https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-awssdkclients.html

You shouldn't need to manually create subsegments. Hope this helps!

Thanks @willarmiros, I may try that out again for this project but in the case where we want to also instrument a non AWS service we may leverage subsegments so definitely would be helpful if we are able to get it working.

I did eventually get the subsegments to show up by encapsulating both subsegments under another subsegment.

`

Subsegment subsegment = AWSXRay.beginSubsegment("processing start");

//would contain trace for dynamo from the inital post
checkMessageExists(...)

//would contain trace for sqs from the inital post
sendSQSMessage(...)

AWSXray.endSubsegment(subsegment)

`

However if I had the individual subsegments in each method without wrapping in another subsegment it seems whichever subsegment that began and ended first was added whereas the other subsegment did not get logged with xray.

So in this case I would usually see the DB subsegment but not the sqs subsegment.

Not sure why the wrapping would make it work and the individual subsegment seems to stop after the first one is finished