Description
I use micrometer in a Spring Boot application to measure the execution time of a method.
Given the following (Kotlin) method:
class MyClass(...) {
@Timed(value = "my_timer")
@Transactional
fun doSomething() {
// Some code that performs changes on the database
}
}
I would expect that the @Timed
annotation measures the total time that the method execution takes including the @Transactional
handling. I compared it to measuring the time outside, as in the following example:
val start = System.nanoTime()
myClass.doSomething()
timer.record(System.nanoTime() - start, TimeUnit.NANOSECONDS)
Since the time measuring is different I did some debugging and found out that the underlying io.micrometer.core.aop.TimedAspect
runs after the @Transactional
processing, which means the order is as follows:
- Transaction start
- Time measurement starts
- Method body execution
- Time measurement stops
- Transaction commits
What I would like to achieve is the following order:
- Time measurement starts
- Transaction start
- Method body execution
- Transaction commits
- Time measurement stops
I already opened an issue at micrometer: micrometer-metrics/micrometer#5235, but they pointed me to the spring-framework issue tracker.
I already tried to use @DeclarePrecedence
like this + @EnableAspectJAutoProxy
on the @SpringBootApplication
class, but it had no effect.
@Aspect
@DeclarePrecedence("TimedAspect, *")
public class TimedAspectPrecedence {
}