Search code examples
spring-bootprometheusspring-boot-actuatorhealth-monitoringspring-micrometer

How @Counted works in spring boot?


How @Counted works? I have added @Counted annotation on my method in Controller and expecting to see how many hits are coming to the controller. But i cannot see metrics added onto the url http://localhost:8080/actuator/prometheus.

 @Counted(value = "counted.success.test",description = "testCounter")

Solution

  • You need to add a CountedAspect as a bean, then the metrics are created when you call the method:

    @Configuration
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    public class Config {
    
        @Bean
        CountedAspect countedAspect(MeterRegistry registry) {
            return new CountedAspect(registry);
        }
    

    (Can't remember why we added the @EnableAspectJAutoProxy(proxyTargetClass = true))

    Though that that kind of instumentation is not prefect, the labels class and method will change as soon as you refactor your code and your Grafana dashboard might not work any longer.