简体   繁体   中英

How to set up Spring Boot to make component metered without adding @Timed to each class, but rather specifying already used annotations like @Service

I have a Spring Boot application, full of endpoints (ie @Controller 's) and @Service 's with @Repository 's behind them. I'd like to make all of them to export the timed metrics to Micrometer. At some point I'd like to disable them all too. At some point I would need to meter, for instance, all methods of @Service labelled classes or meter the timing of @Repository 's. So I don't like all the classes that already keep @Service annotation to have @Timed inserted and the removed, and I have to idea how to do it with the Spring Data interfaces for repositories.

What kind of beans I should set up to make MeterRegistry consider that all the @RestController labeled code already have @Timed annotation, to make this configuration an easy to switch on and switch off property? I know, that for the incoming http request management.metrics.web.server.request.autotime.enabled=true will make this trick for getting the time of incoming http request, that will work for me.

However, the same probably I'd like to apply to service and repository layer, if possible. But there is no way to identify what is actually service, rather than @Service annotation. So as I don't like to add @Timed there explicitly, what kind of set up can be done to make MeterRegistry consider any method in @Service labelled class and in any Spring Data repository at the application?

There is no out-of-the-box solution for it, but you can implement it by yourself, eg., with Spring AOP.

You can look at the TimedAspect source code, especially at @Around aspect for @Timed annotation, and recreate it for @Service or @Repository .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM