I am using Spring AOP/AspectJ in my Spring Boot API to make an annotation in Java like @TrackExecutionTime, that I can slap on any method and it logs the total time it took the method to run. This is working currently in my Spring boot up. My issue is, my API is getting hit thousands of times a day, and I want log some other unique info with this execution time, so I can track/trace each request through my logs. Some of the data that I can use is sent with the POST request body from the JSON, but I can't see how to pass these arguments into the definition of this annotation - can anyone help?
I want to pass some arguments (customer firstname, lastname, etc..) from this Customer object that is the requestBody the client will POST as JSON to my API to my annotation logger:
@RestController
public class CustomersController implements CustomersApi {
@Autowired
private CustomerService customerService;
return ResponseEntity.ok(offersService.fetchCustomer(Customer, clientId));
}
I defined the concrete class and the interface for annotation like this. This is where I want to pass the Customer object so I can log the firstname or lastname:
@Aspect
@Component
@Slf4j
@ConditionalOnExpression("${aspect.enabled:true}")
public class ExecutionTimeAdvice {
@Around("@annotation(com.mailshine.springboot.aop.aspectj.advise.TrackExecutionTime)")
public Object executionTime(ProceedingJoinPoint point) throws Throwable {
long startTime = System.currentTimeMillis();
Object object = point.proceed();
long endtime = System.currentTimeMillis();
log.info("Class Name: "+ point.getSignature().getDeclaringTypeName() +". Method Name: "+ point.getSignature().getName() + ". Time taken for Execution is : " + (endtime-startTime) +"ms");
return object;
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TrackExecutionTime {
}