1 Answer
- Newest
- Most votes
- Most comments
0
Hello,
Using XRay with Spring uses AOP [1][2] in order to avoid the need of any code changes and this involves using proxies with your beans [3]. Unfortunately, AOP doesn't work with internal method calls (method within the same object), hence these are not intercepted. This will result XRay to only generate segments on calls between objects, but not for calls within the same object.
As a possible workaround, you can add a custom subsegment to your code as detailed on reference [4].
import com.amazonaws.xray.spring.aop.XRayEnabled; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.amazonaws.xray.AWSXRay; @RestController @RequestMapping("/xray") @XRayEnabled public class XrayController { @GetMapping(value = "/method1") public String method1() { return method2(); } public String method2() { AWSXRay.beginSubsegment("method2"); String s="Hello"; AWSXRay.endSubsegment(); return s; } }
Alternatively, you can force inter-object communication by calling method2() as another instance of the class:
import com.amazonaws.xray.spring.aop.XRayEnabled; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; @RestController @RequestMapping("/xray") @XRayEnabled public class XrayController { @Autowired XrayController tc; @GetMapping(value = "/method1") public String method1() { return tc.method2(); } public String method2() { return "Hello"; } }
Unfortunately, both workarounds require changes to your code
References
- https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-aop-spring.html - Aspect-Oriented Programming for AWS X-Ray Using Spring
- https://aws.amazon.com/blogs/devops/aspect-oriented-programming-for-aws-x-ray-using-spring/ - Aspect-Oriented Programming for AWS X-Ray Using Spring
- https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-understanding-aop-proxies - Details on AOP proxies
- https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-subsegments.html - Generating custom subsegments with the X-Ray SDK for Java
Relevant content
- asked 3 years ago
- asked a year ago
- asked 6 months ago
- asked a year ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago