java結(jié)合prometheus如何實現(xiàn)自定義數(shù)據(jù)監(jiān)控
更新時間:2024年12月12日 11:07:44 作者:涅米涅米
文章介紹了如何配置Prometheus監(jiān)控系統(tǒng),包括配置文件prometheus.yml、被監(jiān)控應(yīng)用的指標(biāo)暴露配置以及自定義監(jiān)控指標(biāo)的實現(xiàn),同時,還詳細(xì)說明了監(jiān)控應(yīng)用如何通過Prometheus API獲取數(shù)據(jù)、處理數(shù)據(jù)并返回結(jié)果
一、配置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)前對象加1
// gauge可增可減
static final Gauge customizeGauge = Gauge.build()
.name("customize_gauge")
.help("customize gauge")
.labelNames("label1", "label2", "label3") //定義標(biāo)簽名稱,可定義多個
.register();
customizeGauge.inc(); //當(dāng)前對象加1
customizeGauge.dec(); //當(dāng)前對象減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
- 獲取某個時間點的數(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有時會返回亂序的結(jié)果,以下代碼可以按時間戳排序
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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring-boot @Component和@Bean的區(qū)別詳解
這篇文章主要介紹了spring-boot @Component和@Bean的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07

