@Counted 如何在 Spring Boot 中工作?

问题描述

@Counted 如何工作? 我在 Controller 中的方法添加了 @Counted 注释,并希望看到有多少点击进入控制器。但我看不到添加到 url http://localhost:8080/actuator/prometheus 的指标。

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

解决方法

您需要添加一个 CountedAspect 作为 bean,然后在调用该方法时创建指标:

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Config {

    @Bean
    CountedAspect countedAspect(MeterRegistry registry) {
        return new CountedAspect(registry);
    }

(不记得我们为什么添加了 @EnableAspectJAutoProxy(proxyTargetClass = true)

尽管这种工具并不完美,但一旦您重构代码,标签 classmethod 就会发生变化,并且您的 Grafana 仪表板可能不再起作用。