欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-04 18:51:13
...

一、配置prometheus

prometheus.yml

...

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

...

二、被监控应用

思路

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

关键代码

1. 引入相关依赖

pom.xml

<!-- 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’

PrometheusConfig.java

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. 自定义监控指标

MyMetrics.java

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. 引入相关依赖

pom.xml

<!-- 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. 处理数据

prometheus有时会返回乱序的结果,以下代码可以按时间戳排序

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;
    }
}
相关标签: prometheus