欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • 5分鐘快速學(xué)會spring boot整合JdbcTemplate的方法

    5分鐘快速學(xué)會spring boot整合JdbcTemplate的方法

    這篇文章主要給大家介紹了如何通過5分鐘快速學(xué)會spring boot整合JdbcTemplate的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot整合JdbcTemplate具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Java獲取工程路徑方法詳解

    Java獲取工程路徑方法詳解

    這篇文章主要介紹了Java獲取工程路徑方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Java實現(xiàn)動態(tài)驗證碼生成

    Java實現(xiàn)動態(tài)驗證碼生成

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)動態(tài)驗證碼生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 如何更好的使用Java8中方法引用詳解

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

    在Java8中,我們可以直接通過方法引用來簡寫lambda表達(dá)式中已經(jīng)存在的方法,這種特性就叫做方法引用(Method Reference)。下面這篇文章主要給大家介紹了關(guān)于如何更好的使用Java8中方法引用的相關(guān)資料,需要的朋友可以參考下。
    2017-09-09
  • Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解

    Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解

    這篇文章主要介紹了Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • @Resource和@Autowired兩個注解的區(qū)別及說明

    @Resource和@Autowired兩個注解的區(qū)別及說明

    這篇文章主要介紹了@Resource和@Autowired兩個注解的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • 帶你用Java方法輕松實現(xiàn)樹的同構(gòu)

    帶你用Java方法輕松實現(xiàn)樹的同構(gòu)

    給定兩棵樹T1和T2。如果T1可以通過若干次左右孩子互換就變成T2,則我們稱兩棵樹是“同構(gòu)”的。例如圖1給出的兩棵樹就是同構(gòu)的,因為我們把其中一棵樹的結(jié)點A、B、G的左右孩子互換后,就得到另外一棵樹
    2021-06-06
  • java 服務(wù)器接口快速開發(fā)之servlet詳細(xì)教程

    java 服務(wù)器接口快速開發(fā)之servlet詳細(xì)教程

    Servlet(Server Applet)是Java Servlet的簡稱,稱為小服務(wù)程序或服務(wù)連接器,用Java編寫的服務(wù)器端程序,具有獨立于平臺和協(xié)議的特性,主要功能在于交互式地瀏覽和生成數(shù)據(jù),生成動態(tài)Web內(nèi)容
    2021-06-06
  • Spring Cloud Feign實現(xiàn)動態(tài)URL

    Spring Cloud Feign實現(xiàn)動態(tài)URL

    本文主要介紹了Spring Cloud Feign實現(xiàn)動態(tài)URL,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • jsoup如何爬取圖片到本地

    jsoup如何爬取圖片到本地

    這篇文章主要為大家詳細(xì)介紹了jsoup如何爬取圖片到本地,jsoup爬取網(wǎng)站信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論