Spring Boot中的@Counted是如何工作的?

8

@Counted 是如何工作的? 我在控制器(Controller)的方法上添加了 @Counted 注释,并期望看到有多少次请求来到控制器(Controller)。但我无法看到指标(metrics)添加到URL http://localhost:8080/actuator/prometheus 上。

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

请问您能否提供一下您为Prometheus所做的所有配置,以便将所有统计数据转移到执行器?您是否已经在执行器中获取到了密钥? - Yogesh Prajapati
@Yogesh Prajapati在Prometheus中没有获取到密钥。 - Reema Joshi
1个回答

9
您需要将 CountedAspect 作为一个 bean 添加进来,这样在调用方法时指标就会被创建:
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Config {

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

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

尽管这种工具不是完美的,但标签classmethod会随着代码重构而改变,你的Grafana仪表板可能不再起作用。


添加了CountedAspect的bean,它可以正常工作。 @Bean public CountedAspect countedAspect(MeterRegistry meterRegistry) { return new CountedAspect(meterRegistry); } - Reema Joshi
我还必须添加这个依赖项 <artifactId>spring-boot-starter-aop</artifactId>。 - Gabriel Aramburu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接