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

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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠