Creating X-Ray Inspector
Creating X-Ray Inspector
-
First, to measure methods, add the @XRayEnabled annotation to the ProductRepository and ProductsController files.


-
In the config directory, create a new file named XRayInspector.

-
Add two annotations to the XRayInspector class:
- @Aspect: This is a Spring AOP annotation that indicates the class contains cross-cutting concerns, meaning behaviors that can affect or be applied across different classes or methods. In this case, it involves adding monitoring information from AWS X-Ray.
- @Component: This Spring Framework annotation marks the class as a “component,” allowing Spring to automatically detect and manage it as a bean in the ApplicationContext.

-
Have the XRayInspector class inherit from BaseAbstractXRayInterceptor, an abstract class used to create interceptors for AWS X-Ray, where methods can be overridden to customize the behavior of subsegments within AWS X-Ray.
@Aspect
@Component
public class XRayInspector extends BaseAbstractXRayInterceptor {
@Override
protected void xrayEnabledClasses() {
// Implement custom behavior here
}
}

- Create the generateMetadata() method. This method overrides the method from the parent class (BaseAbstractXRayInterceptor). Its purpose is to generate metadata for a subsegment of AWS X-Ray based on the ongoing join point. ProceedingJoinPoint is a concept in AOP representing a point of execution where an interception can occur, and a Subsegment is part of a trace in AWS X-Ray, allowing additional information to be collected. In this case, the method simply calls the parent class method using super.generateMetadata().
@Override
protected Map<String, Map<String, Object>> generateMetadata(
ProceedingJoinPoint proceedingJoinPoint, Subsegment subsegment
) {
return super.generateMetadata(proceedingJoinPoint, subsegment);
}

- The xrayEnabledClasses() method is annotated with @Pointcut. This annotation specifies a pointcut expression in Spring AOP, describing where an advice should be applied. In this case, the pointcut expression @within(com.amazonaws.xray.spring.aop.XRayEnabled) indicates that the advice will be applied to any class annotated with @XRayEnabled provided by com.amazonaws.xray.spring.aop. The method itself has no content inside, as it is used solely to define the location where the advice should be applied.
@Override
@Pointcut("@within(com.amazonaws.xray.spring.aop.XRayEnabled)")
protected void xrayEnabledClasses() {}

Add X-Ray interceptor to the DynamoDB client
- To add an X-Ray interceptor to the DynamoDB client, open the
DynamoDBConfig file and modify the dynamoDbAsyncClient configuration as follows:
.overrideConfiguration(ClientOverrideConfiguration.builder()
.addExecutionInterceptor(new TracingInterceptor())
.build())

-
Finally, add @EnableAspectJAutoProxy to ProductsserviceApplication.
