最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)用

思路

  1. 引入相關(guān)依賴
  2. 配置監(jiān)控指標(biāo)暴露的endpoint
  3. 自定義監(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)用

思路

  1. 引入相關(guān)依賴
  2. 調(diào)用prometheus API獲取數(shù)據(jù)
  3. 整理數(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)文章

  • java實現(xiàn)線程阻塞式方法

    java實現(xiàn)線程阻塞式方法

    Java阻塞式方法會使線程暫停執(zhí)行,不占用CPU資源直至條件滿足,常見阻塞方法如Thread.sleep()、Object.wait()和I/O操作,具有一定的參考價值,感興趣的可以了解一下
    2024-10-10
  • Spring Boot循環(huán)依賴的癥狀和解決方案

    Spring Boot循環(huán)依賴的癥狀和解決方案

    循環(huán)依賴是指在Spring Boot 應(yīng)用程序中,兩個或多個類之間存在彼此依賴的情況,形成一個循環(huán)依賴鏈。這篇文章主要介紹了SpringBoot循環(huán)依賴的癥狀和解決方法
    2023-04-04
  • Java基礎(chǔ)之初識Maven

    Java基礎(chǔ)之初識Maven

    這篇文章主要介紹了Java基礎(chǔ)之初識Maven,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-05-05
  • 詳解5種Java中常見限流算法

    詳解5種Java中常見限流算法

    在高并發(fā)系統(tǒng)中,出于系統(tǒng)保護(hù)角度考慮,通常會對流量進(jìn)行限流;不但在工作中要頻繁使用,而且也是面試中的高頻考點。本文就為大家整理了5種Java中常見限流算法,需要的可以參考一下
    2023-04-04
  • 關(guān)于BigDecimal類型之間比較問題

    關(guān)于BigDecimal類型之間比較問題

    這篇文章主要介紹了關(guān)于BigDecimal類型之間比較問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • JPA之映射mysql text類型的問題

    JPA之映射mysql text類型的問題

    這篇文章主要介紹了JPA之映射mysql text類型的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • MyBatis框架與參數(shù)案例解析

    MyBatis框架與參數(shù)案例解析

    MyBatis是一款Java持久層框架,實現(xiàn)對象與數(shù)據(jù)庫的映射,文章介紹了MyBatis的基本概念,并給出了一個完整的使用案例,結(jié)合實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2025-09-09
  • 詳解Java中的靜態(tài)代理模式

    詳解Java中的靜態(tài)代理模式

    這篇文章主要為大家介紹了Java中的靜態(tài)代理模式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-12-12
  • Java 對 Cookie增刪改查的實現(xiàn)示例

    Java 對 Cookie增刪改查的實現(xiàn)示例

    這篇文章主要介紹了Java 對 Cookie增刪改查的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • spring-boot @Component和@Bean的區(qū)別詳解

    spring-boot @Component和@Bean的區(qū)別詳解

    這篇文章主要介紹了spring-boot @Component和@Bean的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07

最新評論

高密市| 昌都县| 万山特区| 新巴尔虎右旗| 贵定县| 宁阳县| 喀喇沁旗| 三台县| 盐源县| 津市市| 齐河县| 石景山区| 扎囊县| 余江县| 岗巴县| 沙湾县| 抚州市| 赤水市| 阳朔县| 安新县| 上栗县| 珲春市| 瑞金市| 南开区| 汉阴县| 乌什县| 天镇县| 拜泉县| 方正县| 延安市| 永仁县| 上高县| 玉田县| 永年县| 沙洋县| 镇平县| 岗巴县| 巴林左旗| 邵阳市| 清水县| 新津县|