aws / aws/aws-xray-sdk-java

XRay multi threading - Failed to end subsegment: subsegment cannot be found

Open
#227 6 comments 0 reactions 1 assignee Claimed by @willarmiros View on GitHub
bug
Dominant language
Java
Stars
100
Forks
100
PR merge metrics
No merged PRs in 30d

Description

I have an issue using Xray in a multithreaded environment with my REST API. I'm using the Xray Auto instrumentation agent for Java, and Spring boot.

I've tried to follow the example found here https://docs.aws.amazon.com/xray/latest/devguide/scorekeep-workerthreads.html

However when ending my subsegment, I get a log output of "Suppressing AWS X-Ray context missing exception (SubsegmentNotFoundException): Failed to end subsegment: subsegment cannot be found."

I suspect what happens is that the request has been fully handled and response returned to the client, before the CompletableFuture is done. I assume this means that the Xray segment has been closed and that might explain why I'm seeing this issue. What I'm wondering is if there´s anything I can do to fix this?

I could probably live with this if it was only the trace within the CompletableFuture that was lost, but it looks like the entire trace is lost (there are calls to other HTTP services, dynamo DB etc, which won't show up unless I remove the piece of code below)

```
Entity segment = AWSXRay.getGlobalRecorder().getTraceEntity();

CompletableFuture.runAsync(() -> {
AWSXRay.getGlobalRecorder().setTraceEntity(segment);

AWSXRay.beginSubsegment("PostTest");
try {
defaultService.createStatus(accessToken, statusRequest);
} finally {
AWSXRay.endSubsegment();
}
})
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.