我正在try 使用测微计@Timed注释应用普罗米修斯度量标准.

举个例子:

@RestController
public class TestController {

    @GetMapping("/test")
    @Timed("test-endpoint") //does create prometheus metrics
    public String test() {
        privateMethod();
        publicMethod();
        return "test";
    }

    @Timed("test-private") //does NOT create prometheus metrics
    private void privateMethod() {System.out.println("private stuff");}

    @Timed("test-public") //does NOT create prometheus metrics
    public void publicMethod() {System.out.println("public stuff");}
}

创建以下指标:

...
# HELP test_endpoint_seconds  
# TYPE test_endpoint_seconds summary
test_endpoint_seconds_count{class="com.example.micrometerannotationexample.TestController",exception="none",method="test",} 1.0
test_endpoint_seconds_sum{class="com.example.micrometerannotationexample.TestController",exception="none",method="test",} 0.0076286
# HELP test_endpoint_seconds_max  
# TYPE test_endpoint_seconds_max gauge
test_endpoint_seconds_max{class="com.example.micrometerannotationexample.TestController",exception="none",method="test",} 0.0076286
...

找不到@Timed("test-private")@Timed("test-public")的指标,为什么?


注意:我在this github thread上读到,Spring Boot无法识别任意方法上的@Timed个注释,需要手动配置TimedAspect Bean才能工作.我试过了,但还是没有效果.

@Configuration
@EnableAspectJAutoProxy
public class MetricsConfig {
    @Bean
    public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
    }
}

To try this locally see necessary gist 100

推荐答案

@Timed仅适用于另一个类调用的公共方法.

像@timed/@transactional这样的Spring bootstrap 注释需要所谓的代理,这种代理只在调用公共方法之间发生.

这是一个很好的解释

Java相关问答推荐

我可以从Java模块中排除maven资源文件夹吗?

是否需要关闭Executors返回的执行器.newVirtualThreadPerTaskExecutor()?

具有额外列的Hibert多对多关系在添加关系时返回NonUniqueHealthExcellent

int Array Stream System. out. print方法在打印Java8时在末尾添加% sign

如何在Android上获取来电信息

在Java中将Charsequence数组更改为String数组或List String<>

CAMEL 4中的SAXParseException

对Java中的通配符参数的混淆

JPanel透支重叠的JComcoBox

如何修复PDF重建过程中的文本定位

Jenv-相同的Java版本,但带有前缀

在Eclipse中数组的可空性

将ByteBuffer异步写入InputStream或Channel或类似对象

在JDK Flight Recorder中只记录单个线程

如何使用WebEnvironment.RANDOM_PORT获得第二个随机端口?

根据应用程序 Select 的语言检索数据

基于距离的APACHE POI公式判断

如何在JSP中从select中获取值并将其放入另一个select

Spring Mapstruct如何获取Lazy初始化实体字段的信息?

@此处不能应用可为null的批注