@Timed annotation in spring metrics

user_x picture user_x · Aug 8, 2018 · Viewed 11k times · Source

I use @Timed annotation on String Boot rest controller and it works fine. Method from controller calls method from service which is also annotated with @Timed.

However, this annotation on method in subsequent service bean doesn't work (I don't see results in /metrics). Why is it happening? Could it be fixed?

Answer

Karol Dowbecki picture Karol Dowbecki · Aug 8, 2018

As per Support for @Timed in any Spring-managed bean #361 you can get this behaviour by registering TimedAspect manually.

@Configuration
@EnableAspectJAutoProxy
public class AutoTimingConfiguration {
  @Bean
  public TimedAspect timedAspect(MeterRegistry registry) {
    return new TimedAspect(registry);
  }
}

Do note that as per jkschneider comment in #361:

We can revisit application of @Timed via AOP or a BPP in Boot 2.1, depending on how the community reacts to the feature.