如何从 Java (Spring boot) 应用程序向 Prometheus 公开指标

kra*_*234 5 instrumentation maven spring-boot grafana prometheus

我的 Spring-Boot 应用程序只有一个计数器指标。我只是不知道如何将这些信息发送给 Prometheus。我正在使用 Maven(构建工具)和 Spring Boot(Java)。

Chi*_*ids 3

对于集成 Prometheus,请在 POM.XML 中添加以下依赖项

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_spring_boot</artifactId>
    <version>0.1.0</version>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_servlet</artifactId>
    <version>0.1.0</version>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_hotspot</artifactId>
    <version>0.1.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在您的 SpringBoot 应用程序类中,添加 Annonation@EnablePrometheusEndpoint

在您的控制器中,您可以定义自定义计数器

private static final Counter myCounter = Counter.build()
        .name("CounterName")
        .labelNames("status")
        .help("Counter desc").register();
Run Code Online (Sandbox Code Playgroud)

在您的服务中,您可以为您的计数器设置适当的逻辑,该逻辑将由 Prometheus 自动拉取

@RequestMapping("/myService")
    public void endpoint() {
           String processingResult = yourLogic(myCounter);
            myCounter.labels("label1",processingResult).inc();
            }
Run Code Online (Sandbox Code Playgroud)