java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java结合prometheus自定义数据监控

java结合prometheus如何实现自定义数据监控

作者:涅米涅米

文章介绍了如何配置Prometheus监控系统,包括配置文件prometheus.yml、被监控应用的指标暴露配置以及自定义监控指标的实现,同时,还详细说明了监控应用如何通过Prometheus API获取数据、处理数据并返回结果

一、配置prometheus

...

- job_name: 'my-service'
  metrics_path: /metrics
  static_configs:
  - targets: ['xxx.xxx.xxx.xxx:yyyy'] //被监控应用的url

...

二、被监控应用

思路

  1. 引入相关依赖
  2. 配置监控指标暴露的endpoint
  3. 自定义监控指标

关键代码

1. 引入相关依赖

<!-- prometheus -->
<dependency>
		<groupId>io.prometheus</groupId>
		<artifactId>simpleclient</artifactId>
		<version>0.3.0</version>
</dependency>
<dependency>
		<groupId>io.prometheus</groupId>
		<artifactId>simpleclient_hotspot</artifactId>
		<version>0.3.0</version>
</dependency>
<dependency>
		<groupId>io.prometheus</groupId>
		<artifactId>simpleclient_servlet</artifactId>
		<version>0.3.0</version>
</dependency>

2. 将监控指标暴露到’/metrics’

import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
...

@Configuration
public class PrometheusConfig {

		// ...

		@Bean
		public ServletRegistrationBean servletRegistrationBean(){
			DefaultExports.initialize();
			return new ServletRegistrationBean(new MetricsServlet(), "/metrics");
		}
}

3. 自定义监控指标

import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
...

// counter只增不减
static final Counter customizeCounter = Counter.build()
        .name("customize_counter") //定义名称,名称可用于搜索
        .help("customize counter") //定义描述
        .register();
customizeCounter.inc(); //当前对象加1

// gauge可增可减
static final Gauge customizeGauge = Gauge.build()
        .name("customize_gauge")
        .help("customize gauge")
        .labelNames("label1", "label2", "label3") //定义标签名称,可定义多个
        .register();
customizeGauge.inc(); //当前对象加1
customizeGauge.dec(); //当前对象减1
customizeGauge.labels("value1","value2","value3").set(1100); //设置标签值,标签可用于条件筛选

// 此外还有histogram和summary两种指标

三、监控应用

思路

  1. 引入相关依赖
  2. 调用prometheus API获取数据
  3. 整理数据并返回

关键代码

1. 引入相关依赖

<!-- prometheus -->
<dependency>
		<groupId>io.prometheus</groupId>
		<artifactId>simpleclient</artifactId>
		<version>0.3.0</version>
</dependency>
<dependency>
		<groupId>io.prometheus</groupId>
		<artifactId>simpleclient_hotspot</artifactId>
		<version>0.3.0</version>
</dependency>
<dependency>
		<groupId>io.prometheus</groupId>
		<artifactId>simpleclient_servlet</artifactId>
		<version>0.3.0</version>
</dependency>

2. 调用prometheus API

String query = "customize_counter";
String url = "http://[ip]:[port]/api/v1/query?query=" + query + "&time=2019-05-01T20:10:51.781Z";
HttpGet get = new HttpGet(url);
CloseableHttpClient httpClient = HttpClients.custom().build();
CloseableHttpResponse response = httpClient.execute(get);
String query = "rate(customize_counter{label1 = value1}[30s])";
String url = "http://[ip]:[port]/api/v1/query_range?query=" + query + "&start=2019-05-01T20:10:51.781Z&end=2019-05-02T20:10:51.781Z";
HttpGet get = new HttpGet(url);
CloseableHttpClient httpClient = HttpClients.custom().build();
CloseableHttpResponse response = httpClient.execute(get);

更多使用方法请参考Prometheus - 查询

3. 处理数据

public class ComparatorPromData implements Comparator {

    @Override
    public int compare(Object o1, Object o2) {
        List list1 = (List)o1;
        List list2 = (List)o2;
        return ((Integer) list1.get(0)).compareTo(((Integer) list2.get(0)));
    }
}
public class SortUtil {

    public static List sortPromData(List<List> list) {
        ComparatorPromData comparator = new ComparatorPromData();
        Collections.sort(list, comparator);
        return list;
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文