我刚刚开始在我的 SpringBoot 3 应用程序中使用 Micrometer / Prometheus / Grafana 来实现可观察性。我的 pom 中有所需的依赖项,并且想要查找 API 的总点击次数。这是我尝试过的,但在 http://localhost:9090/ 的 Prometheus UI 中,我没有看到 2 个变量 getMoviesOfDirector.count 或 getMoviesOfDirector 中的任何一个。请求指导如何获取 API 总点击量以及采用哪种方法。
方法1
@Counted(value = "getMoviesOfDirector.count", description = "getMoviesOfDirector count")
public ResponseEntity<List<Movie>> getMoviesOfDirector(...)
方法2
//Counter metric
Counter counter = Counter.builder("getMoviesOfDirector")
.description("a number of requests to /getMoviesOfDirector endpoint")
.register(meterRegistry);
counter.increment();
Spring Boot 开箱即用地支持此功能,您应该看到控制器端点的计数器:
http_server_requests_seconds_count{..., uri="/getMoviesOfDirector"} 1.0
您不需要在此之上使用
@Counted
。
此外,根据您使用的 Boot 3.x 版本,您可能需要创建一个
CountedAspect
@Bean
,如果您升级到 Boot 3.3.x,将为您创建该 bean。
另一个提示:Prometheus 在其命名约定中使用下划线 (
_
),并且 Micrometer 支持这一点,因此 Micrometer 中的 test.counter
在 Prometheus 端将是 test_counter_total
。