我尝试使用micronium @Timed
注解来应用Prometheus度量,发现它们只适用于控制器端点,而不是“简单的”公共和私有方法。
举个例子:
@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")
的指标,原因是什么?
Note:我在这个github线程上读到,Sping Boot 不能识别任意方法上的@Timed
注解,你需要手动配置一个TimedAspect
Bean才能让它工作。
@Configuration
@EnableAspectJAutoProxy
public class MetricsConfig {
@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}
}
- 要在本地尝试,请参阅必要的要点here*
2条答案
按热度按时间6ovsh4lw1#
@Timed
仅适用于其他类调用的public
方法。Sping Boot 注解(如
@Timed
/@Transactional
)需要所谓的代理,这种代理只发生在public
方法的调用之间。很好的解释是https://stackoverflow.com/a/3429757/2468241
8yoxcaq72#
我认为this教程中有一个更好的例子来说明它是如何工作的。这是一个很好的例子,说明了如何在控制器调用和结果中使用
@Timed
。请注意,如果您的执行器端点在身份验证之后,您将需要在您的prometheus配置中添加一个承载令牌(参见here)。