X-Ray trace doesn't shows inner method call in springboot app

0

I'm new to aws x-ray and trying to use x-ray with AOP based approach in a springboot application. I was able to get the traces in the aws console, but traces doesn't show inner method call method2() details. Am I missing anything here ?

**Controller 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;

@RestController
@RequestMapping("/xray")
@XRayEnabled
public class XrayController {

  @GetMapping(value = "/method1")
  public String method1() {
    return method2();
  }

  public String method2() {
    return "Hello";
  }
}

Aspect Class

import com.amazonaws.xray.entities.Subsegment;
import com.amazonaws.xray.spring.aop.BaseAbstractXRayInterceptor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Map;


@Aspect
@Component
public class XRayInspector extends BaseAbstractXRayInterceptor {
  @Override
  protected Map<String, Map<String, Object>> generateMetadata(ProceedingJoinPoint proceedingJoinPoint, Subsegment subsegment)  {
    return super.generateMetadata(proceedingJoinPoint, subsegment);
  }

  @Override
  @Pointcut("@within(com.amazonaws.xray.spring.aop.XRayEnabled) && (bean(*Controller) || bean(*Service) || bean(*Client) || bean(*Mapper))")
  public void xrayEnabledClasses() {}
}

When I hit http://localhost:8080/xray/method1 endpoint, AWS Xray Console doesn't show method2() details

Enter image description here

已提問 2 年前檢視次數 887 次
1 個回答
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

  1. https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-aop-spring.html - Aspect-Oriented Programming for AWS X-Ray Using Spring
  2. https://aws.amazon.com/blogs/devops/aspect-oriented-programming-for-aws-x-ray-using-spring/ - Aspect-Oriented Programming for AWS X-Ray Using Spring
  3. https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-understanding-aop-proxies - Details on AOP proxies
  4. https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-subsegments.html - Generating custom subsegments with the X-Ray SDK for Java
AWS
支援工程師
Janko
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南