Skip to content

Instantly share code, notes, and snippets.

@neo7BF
Last active August 11, 2020 09:33
Show Gist options
  • Save neo7BF/adb51a4f75973a1fd6b3a1914d3b4dc9 to your computer and use it in GitHub Desktop.
Save neo7BF/adb51a4f75973a1fd6b3a1914d3b4dc9 to your computer and use it in GitHub Desktop.
Misuring execution time of queries with spring aop and spring jdbc in spring boot
@Component
@Aspect
@Slf4j
public class LoggingExecutionTimeQueryAspect {
//replace <> with your own repository class info for tracing execution time of queries of that Repository
private Temporal startTime;
@Before("execution(* <PACKAGE>.<RESPOSITORY_CLASS_NAME>.*(..))")
public void logBeforeQueryInvocation(JoinPoint joinPoint){
startTime = Instant.now();
}
@After("execution(* <PACKAGE>.<RESPOSITORY_CLASS_NAME>.*(..))")
public void logAfterQueryInvocation(JoinPoint joinPoint){
Duration d = Duration.between(startTime, Instant.now());
log.info("Execution time: " + DurationFormatUtils.formatDurationHMS(d.toMillis()) + " Method: " + joinPoint.getSignature());
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment