Search code examples
javaspringamazon-web-servicesspring-bootaws-xray

X-Ray trace doesn't shows inner method call


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

AWS Xray Console doesn't show method2 details


Solution

  • As I later understood with the use of M. Deinum's comment AOP prevents you to wrap a function if it is in the caller is in the same class of the callee.

    Read more on AOP https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-understanding-aop-proxies

    You can use a work around using below self injection

    public class Test {
    
    
      @Autowire 
      private Test test;
    
    
      public void method1(){
         .........
         test.method2();
        ...........
      }
    
    
      public void method2(){
        ...............
        ...............  
      }
    }
    

    notice here we call the method 2 by test.method2(); instead of this.method2()

    I referred below answer also for this solution Spring AOP not working for method call inside another method