java結(jié)合prometheus如何實(shí)現(xiàn)自定義數(shù)據(jù)監(jiān)控
一、配置prometheus
- prometheus.yml
... - job_name: 'my-service' metrics_path: /metrics static_configs: - targets: ['xxx.xxx.xxx.xxx:yyyy'] //被監(jiān)控應(yīng)用的url ...
二、被監(jiān)控應(yīng)用
思路
- 引入相關(guān)依賴
- 配置監(jiān)控指標(biāo)暴露的endpoint
- 自定義監(jiān)控指標(biāo)
關(guān)鍵代碼
1. 引入相關(guān)依賴
- 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. 將監(jiān)控指標(biāo)暴露到’/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. 自定義監(jiān)控指標(biāo)
- 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(); //當(dāng)前對(duì)象加1 // gauge可增可減 static final Gauge customizeGauge = Gauge.build() .name("customize_gauge") .help("customize gauge") .labelNames("label1", "label2", "label3") //定義標(biāo)簽名稱,可定義多個(gè) .register(); customizeGauge.inc(); //當(dāng)前對(duì)象加1 customizeGauge.dec(); //當(dāng)前對(duì)象減1 customizeGauge.labels("value1","value2","value3").set(1100); //設(shè)置標(biāo)簽值,標(biāo)簽可用于條件篩選 // 此外還有histogram和summary兩種指標(biāo)
三、監(jiān)控應(yīng)用
思路
- 引入相關(guān)依賴
- 調(diào)用prometheus API獲取數(shù)據(jù)
- 整理數(shù)據(jù)并返回
關(guān)鍵代碼
1. 引入相關(guān)依賴
- 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. 調(diào)用prometheus API
- 獲取某個(gè)時(shí)間點(diǎn)的數(shù)據(jù)
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);
- 獲取某個(gè)時(shí)間段的數(shù)據(jù)
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);
更多使用方法請(qǐng)參考Prometheus - 查詢
3. 處理數(shù)據(jù)
- prometheus有時(shí)會(huì)返回亂序的結(jié)果,以下代碼可以按時(shí)間戳排序
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; } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
5分鐘快速學(xué)會(huì)spring boot整合JdbcTemplate的方法
這篇文章主要給大家介紹了如何通過5分鐘快速學(xué)會(huì)spring boot整合JdbcTemplate的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot整合JdbcTemplate具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Java實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼生成
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Java基于jeeplus vue實(shí)現(xiàn)簡單工作流過程圖解
這篇文章主要介紹了Java基于jeeplus vue實(shí)現(xiàn)簡單工作流過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04@Resource和@Autowired兩個(gè)注解的區(qū)別及說明
這篇文章主要介紹了@Resource和@Autowired兩個(gè)注解的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06帶你用Java方法輕松實(shí)現(xiàn)樹的同構(gòu)
給定兩棵樹T1和T2。如果T1可以通過若干次左右孩子互換就變成T2,則我們稱兩棵樹是“同構(gòu)”的。例如圖1給出的兩棵樹就是同構(gòu)的,因?yàn)槲覀儼哑渲幸豢脴涞慕Y(jié)點(diǎn)A、B、G的左右孩子互換后,就得到另外一棵樹2021-06-06java 服務(wù)器接口快速開發(fā)之servlet詳細(xì)教程
Servlet(Server Applet)是Java Servlet的簡稱,稱為小服務(wù)程序或服務(wù)連接器,用Java編寫的服務(wù)器端程序,具有獨(dú)立于平臺(tái)和協(xié)議的特性,主要功能在于交互式地瀏覽和生成數(shù)據(jù),生成動(dòng)態(tài)Web內(nèi)容2021-06-06Spring Cloud Feign實(shí)現(xiàn)動(dòng)態(tài)URL
本文主要介紹了Spring Cloud Feign實(shí)現(xiàn)動(dòng)態(tài)URL,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02