java結合prometheus如何實現(xiàn)自定義數(shù)據(jù)監(jiān)控
更新時間:2024年12月12日 11:07:44 作者:涅米涅米
文章介紹了如何配置Prometheus監(jiān)控系統(tǒng),包括配置文件prometheus.yml、被監(jiān)控應用的指標暴露配置以及自定義監(jiān)控指標的實現(xiàn),同時,還詳細說明了監(jiān)控應用如何通過Prometheus API獲取數(shù)據(jù)、處理數(shù)據(jù)并返回結果
一、配置prometheus
- prometheus.yml
... - job_name: 'my-service' metrics_path: /metrics static_configs: - targets: ['xxx.xxx.xxx.xxx:yyyy'] //被監(jiān)控應用的url ...
二、被監(jiān)控應用
思路
- 引入相關依賴
- 配置監(jiān)控指標暴露的endpoint
- 自定義監(jiān)控指標
關鍵代碼
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. 將監(jiān)控指標暴露到’/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)控指標
- 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兩種指標
三、監(jiān)控應用
思路
- 引入相關依賴
- 調用prometheus API獲取數(shù)據(jù)
- 整理數(shù)據(jù)并返回
關鍵代碼
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
- 獲取某個時間點的數(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);
- 獲取某個時間段的數(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);
更多使用方法請參考Prometheus - 查詢
3. 處理數(shù)據(jù)
- 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的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot整合JdbcTemplate具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解
這篇文章主要介紹了Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04@Resource和@Autowired兩個注解的區(qū)別及說明
這篇文章主要介紹了@Resource和@Autowired兩個注解的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06java 服務器接口快速開發(fā)之servlet詳細教程
Servlet(Server Applet)是Java Servlet的簡稱,稱為小服務程序或服務連接器,用Java編寫的服務器端程序,具有獨立于平臺和協(xié)議的特性,主要功能在于交互式地瀏覽和生成數(shù)據(jù),生成動態(tài)Web內容2021-06-06Spring Cloud Feign實現(xiàn)動態(tài)URL
本文主要介紹了Spring Cloud Feign實現(xiàn)動態(tài)URL,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02