Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execution order of @Timed and @Transactional #33595

Open
dkroehan opened this issue Sep 25, 2024 · 2 comments
Open

Execution order of @Timed and @Transactional #33595

dkroehan opened this issue Sep 25, 2024 · 2 comments
Labels
status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@dkroehan
Copy link

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:

  1. Transaction start
  2. Time measurement starts
  3. Method body execution
  4. Time measurement stops
  5. Transaction commits

What I would like to achieve is the following order:

  1. Time measurement starts
  2. Transaction start
  3. Method body execution
  4. Transaction commits
  5. 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 {

}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 25, 2024
@quaff
Copy link
Contributor

quaff commented Sep 26, 2024

@wilkinsona
Copy link
Member

While reviewing spring-projects/spring-boot#42450, I formed the opinion that this is quite subjective and I suspect that users will be able to make compelling cases for either ordering. To fix this, I think we may have to make the ordering configurable so that the advice from the TimedAspect that Boot configures can be made to run before or after Framework's advice for @Transactional.

There's other advice that users may or may not want to be including in the timing such as Spring Security's advice for method-based security. Anything that we do here looks like it will require changes across multiple projects. As part of that, I wonder if it's worth considering support for some relative ordering so that we don't have to rely on magic numbers across those projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

4 participants