9

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?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
littleAlien
  • 721
  • 2
  • 8
  • 20

1 Answers1

11

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.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • But how to make it works? I'm getting ClassNotFoundException: org.aspectj.lang.annotation.Around error and have no luck to fix it... – littleAlien Aug 08 '18 at 15:24
  • 3
    @user_x you are missing AspectJ dependencies needed by `@EnableAspectJAutoProxy`. You can add `spring-boot-starter-aop` as a dependency. – Karol Dowbecki Aug 08 '18 at 15:25
  • @user_x verify that aspect is applied during runtime e.g. by looking at the stack trace for `TimedAspect`. – Karol Dowbecki Aug 08 '18 at 15:42
  • @user_x also take a look at TimedAspect code to see the metric name (e.g. `method.timed` vs `@Timed("myname").` – Karol Dowbecki Aug 08 '18 at 15:57
  • if aspect hasn't applied during runtime - then what to do? :) I don't think it's 'naming' issue. I set name in service method with the same style as for controller method. – littleAlien Aug 08 '18 at 19:46
  • 2
    @user_x I confirmed the above configuration works with a sample: https://github.com/izeye/sample-micrometer-spring-boot/tree/timed-annotation although I found some other issue with controllers: https://github.com/micrometer-metrics/micrometer/issues/780 – Johnny Lim Aug 09 '18 at 11:30
  • @JohnnyLim thanks, will check later, I see you use slightly upper spring version, may be this is the cause. – littleAlien Aug 10 '18 at 10:48
  • Anybody knows if this works out of the box with CompletableFutures? I'm getting some weird results, too low. – Robert Gabriel Apr 15 '19 at 14:05