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?
Entity segment = AWSXRay.getGlobalRecorder().getTraceEntity();
CompletableFuture.runAsync(() -> {
AWSXRay.getGlobalRecorder().setTraceEntity(segment);
AWSXRay.beginSubsegment("PostTest");
try {
defaultService.createStatus(accessToken, statusRequest);
} finally {
AWSXRay.endSubsegment();
}
})
Thanks for your deep diving, this error log confuses users, the Xray SDK is better to remind user the segment is missing when beginning subsegment but not throw an missing subsegment exception at the end.
Your guess is right, it is logical that subsegment can end later than parent segment, but subsegment have to begin earlier than its parent segment end. Else subsegment would not be created successfully and certainly cannot be ended at the end. Please try to adjust your code to start this async thread before segment close.