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

How to expose metrics to Prometheus from a Java (Spring boot) application

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

Prometheus 和 Graphite 一样,是一个时间序列存储引擎。

Grafana 然后可以查询 Prometheus 以生成图形和警报。

https://prometheus.io/docs/introduction/faq/

正如文档引用的那样,Prometheus 与其他指标存储系统不同,它使用(有争议的)"pull" 模型。

这意味着有一个(独立的)Prometheus 服务器必须是 downloaded/installed。然后,此服务器定期向应用程序服务器列表发出 HTTP GET 请求(拉取)- 例如 Java Spring 引导服务器以获取(内存中)存储的指标。

参考:https://prometheus.io/docs/introduction/faq/#why-do-you-pull-rather-than-push?

因此(Spring Boot)应用程序必须公开一个指标端点,Prometheus 服务器可以从中提取指标(默认为 /metrics)。

参考:https://github.com/prometheus/client_java

因此,在 Google 上有很多可用的文档,但那是(可以说是令人费解的)拓扑结构 - 以及 SoundCloud 和 Prometheus 人员关于为什么 "pull" 模型优于 "push" 与其他所有指标框架一样。

要集成 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>

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

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

private static final Counter myCounter = Counter.build()
        .name("CounterName")
        .labelNames("status")
        .help("Counter desc").register();

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

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

Spring 启动 2.0.0

检查您的 spring 引导属性文件以确保启用指标和 Prometheus 导出。

例如:

management.endpoints.web.exposure.include=*
management.endpoint.metrics.enabled=true
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true

从 spring-引导角度看我的 Few Cents -

build.gradle

compile 'io.prometheus:simpleclient_servlet:0.3.0'
compile('io.prometheus:simpleclient_hotspot:0.3.0')
compile('io.prometheus:simpleclient_spring_boot:0.3.0')

你的 class 包含 main 函数应该有以下注释

@EnableSpringBootMetricsCollector
@EnablePrometheusEndpoint
...
...
@Bean
public boolean initializePrometheusEssentials() {
    return prometheusUtility.initializePrometheusEssentials();
}

PometheusUtility.java

import io.prometheus.client.Counter;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

@Component("PrometheusUtility")
public class PrometheusUtility {

private PrometheusUtility(){

}

private final static Map<String,Counter> counters = new HashMap<>();

public boolean initializePrometheusEssentials() {
counters.put("SAMPLE-COUNTER",Counter.build()
            .name("SAMPLE COUNTER")
            .help("Records the Sample Count").register());
            return true;
}


public static void incrementCounter(String counterName){
    counters.get(counterName).inc();
}
}

使用 incrementCounter 方法对其进行递增。

并确保将以下属性添加到您的 env 文件

# Prometheus settings
#change prometheus endpoint to metrics
endpoints.prometheus.id=
#change actuator endpoint to springmetrics
endpoints.metrics.id=
endpoints.metrics.sensitive=
endpoints.metrics.enabled=
endpoints.prometheus.sensitive=