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

 更新时间:2024年12月12日 11:07:44   作者:涅米涅米  
文章介绍了如何配置Prometheus监控系统,包括配置文件prometheus.yml、被监控应用的指标暴露配置以及自定义监控指标的实现,同时,还详细说明了监控应用如何通过Prometheus API获取数据、处理数据并返回结果

一、配置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;
    }
}

总结

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

相关文章

  • 5分钟快速学会spring boot整合JdbcTemplate的方法

    5分钟快速学会spring boot整合JdbcTemplate的方法

    这篇文章主要给大家介绍了如何通过5分钟快速学会spring boot整合JdbcTemplate的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot整合JdbcTemplate具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-12-12
  • Java获取工程路径方法详解

    Java获取工程路径方法详解

    这篇文章主要介绍了Java获取工程路径方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java实现动态验证码生成

    Java实现动态验证码生成

    这篇文章主要为大家详细介绍了Java实现动态验证码生成,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • 如何更好的使用Java8中方法引用详解

    如何更好的使用Java8中方法引用详解

    在Java8中,我们可以直接通过方法引用来简写lambda表达式中已经存在的方法,这种特性就叫做方法引用(Method Reference)。下面这篇文章主要给大家介绍了关于如何更好的使用Java8中方法引用的相关资料,需要的朋友可以参考下。
    2017-09-09
  • Java基于jeeplus vue实现简单工作流过程图解

    Java基于jeeplus vue实现简单工作流过程图解

    这篇文章主要介绍了Java基于jeeplus vue实现简单工作流过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • @Resource和@Autowired两个注解的区别及说明

    @Resource和@Autowired两个注解的区别及说明

    这篇文章主要介绍了@Resource和@Autowired两个注解的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • 带你用Java方法轻松实现树的同构

    带你用Java方法轻松实现树的同构

    给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树
    2021-06-06
  • java 服务器接口快速开发之servlet详细教程

    java 服务器接口快速开发之servlet详细教程

    Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容
    2021-06-06
  • Spring Cloud Feign实现动态URL

    Spring Cloud Feign实现动态URL

    本文主要介绍了Spring Cloud Feign实现动态URL,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • jsoup如何爬取图片到本地

    jsoup如何爬取图片到本地

    这篇文章主要为大家详细介绍了jsoup如何爬取图片到本地,jsoup爬取网站信息,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01

最新评论