使用 prometheus 的自定义计数器在 /actuator/prometheus 上不可见

Swa*_*dey 5 java metrics spring-boot spring-boot-actuator prometheus

我正在尝试在我的 spring-boot 应用程序中添加自定义指标。我已经查看了许多示例,但仍然无法添加自定义计数器。

应用程序属性

management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
Run Code Online (Sandbox Code Playgroud)

代码

static final Counter requests = 
Counter.build().namespace("java").name("requests_total").help("Total requests.")
.register();

@CrossOrigin
@GetMapping("/test")
public int processRequest() {
    requests.inc();
    return (int) requests.get();
}
Run Code Online (Sandbox Code Playgroud)

当我访问 API 时,我可以看到计数器值增加。http://localhost:8080/actuator/prometheus问题是我在普罗米修斯页面上找不到我新创建的指标:9090。所以我认为计数器没有被注册(??)。我在这里缺少什么?

mwe*_*uch 1

看起来您正在直接使用 Prometheus Java API。您创建的计数器已注册到CollectorRegistryPrometheus Java API 的默认值,但它未注册到 Micrometer,因为它正在实例化自己的计数器CollectorRegistry,因此您的计数器不会显示在那里。

您应该使用 Micrometer CounterAPI,而不是直接使用 Prometheus Java API。这样做的另一个好处是,您可以更换监控后端,而无需对仪器代码进行任何更改。

此外,您似乎想测量 HTTP 请求。这些通常是自动计时的。查看端点http_server_requests_seconds_[count,sum,max]中调用的度量标准系列/actuator/prometheus