繁体   English   中英

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

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

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

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

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

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

正如文档所引用的,与其他指标存储系统不同,Prometheus 使用(有争议的)“拉”模型。

这意味着必须下载/安装一个(独立的)Prometheus 服务器。 然后,该服务器定期向应用服务器列表(例如 Java SpringBoot 服务器)发出 HTTP GET 请求(拉取)以获取(内存中)存储的指标。

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

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

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

因此,谷歌上有很多可用的文档,但这就是(可以说是令人费解的)拓扑——以及来自 SoundCloud 和 Prometheus 的人关于为什么“拉”模型比“推”更受欢迎的论点,因为其他所有指标框架都采用。

对于集成 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 应用程序类中,添加 Annonation @EnablePrometheusEndpoint

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

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

在您的服务中,您可以为您的 Counter 设置适当的逻辑,这些逻辑将被 Prometheus 自动拉取

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

春季启动 2.0.0

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

例如:

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

从 spring-boot 的角度来看我的几分钱 -

构建.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')

包含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=

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM