日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Prometheus自定义线程池指标暴露

發(fā)布時間:2023/12/20 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Prometheus自定义线程池指标暴露 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

場景

SkyWalking Java Agent 在8.10版本中支持了對線程池的監(jiān)控,但是僅支持tomcat的線程池以及數(shù)據(jù)庫的連接池,因此希望拓展出自定義想要監(jiān)控的線程池,在線上運(yùn)行環(huán)境中對系統(tǒng)了解的更加清晰,

SkyWalking我們更偏向于專注于trace追蹤的使用,因此在了解了SkyWalking的實(shí)現(xiàn)原理后,我們在Prometheus中基于差不多的實(shí)現(xiàn)暴露出系統(tǒng)的指標(biāo),集成到Prometheus中。

其SkyWalking的原理就是基于相應(yīng)的點(diǎn)做出攔截,得到線程池對象ThreadPoolExecutor,之后通過 getCorePoolSize 等方法獲取線程池的狀態(tài)然后進(jìn)行上報(bào),

因此Prometheus中可以基于同樣的思想原理實(shí)現(xiàn),在某個契機(jī)得到線程池對象,然后將其暴露到指標(biāo)中。

暴露指標(biāo)

暴露指標(biāo)有兩種方式,一種是通過引入spring boot actuator的形式,另一種是通過Prometheus Java Client形式暴露,前者其實(shí)就是對后者的封裝,

前者的好處是使用方便,基本不用做太多的API編寫,壞處是安全性問題,可能會暴露出不必要的東西出來,后者與之相反,可根據(jù)自己的需求來決定使用哪種方式。

actuator 安全性問題:www.istt.org.cn/NewsDetail/…

spring boot actuator形式

先在maven項(xiàng)目中引用如下幾個jar包,得到指標(biāo)暴露的支持:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.6.7</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><version>2.6.7</version></dependency><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><version>1.9.0</version></dependency></dependencies> 復(fù)制代碼

項(xiàng)目的yaml配置如下,主要關(guān)心management中的配置,大致的意思就是暴露出Prometheus的相關(guān)指標(biāo)接口:

server:port: 9091 #Prometheus springboot監(jiān)控配置 spring:application:name: hello-demomain:allow-circular-references: true management:endpoints:web:exposure:include: '*'metrics:export:prometheus:enabled: truetags:application: ${spring.application.name} # 暴露的數(shù)據(jù)中添加application label 復(fù)制代碼

最后就是代碼實(shí)現(xiàn),思路是兩步,

  • 將所有的ThreadPool托管給Spring,然后加上自定義注解,最后在項(xiàng)目啟動后獲取帶有自定義注解的bean

  • 獲取到bean后,使用 Metrics.gauge 對指標(biāo)進(jìn)行注冊

  • 最終訪問 http://localhost:9091/actuator/prometheus 可以看到下面的結(jié)果,可以看到通過這個地址可以獲取到我們的指標(biāo)了:

    源碼地址:github.com/qiaomengnan…

    Prometheus Java Client形式

    因?yàn)楣俜酵扑]使用jetty作為暴露出去的服務(wù),所以要引入jetty的包,并且引入 simpleclient 來作為類似上面案例中的 Metrics.gauge 效果。

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.6.7</version></dependency><dependency><groupId>io.prometheus</groupId><artifactId>simpleclient</artifactId><version>0.15.0</version></dependency><!-- Exposition servlet--><dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_servlet</artifactId><version>0.15.0</version></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-servlet</artifactId><version>9.4.44.v20210927</version></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-server</artifactId><version>9.4.44.v20210927</version></dependency> </dependencies> 復(fù)制代碼

    原生的Client API有些復(fù)雜,其需要一個 Collector 接口實(shí)現(xiàn)類,在這個實(shí)現(xiàn)類中對指標(biāo)進(jìn)行添加和進(jìn)行暴露:

    在APP啟動類中,對每個掃描到到的線程池構(gòu)建一個 Collector 實(shí)現(xiàn)類,并進(jìn)行register操作,最終開啟jetty server ,并添加一個 MetricsServlet 進(jìn)去,最后啟動。

    最后對該servlet進(jìn)行訪問,可以看到我們注冊進(jìn)去的指標(biāo)。

    源碼地址:github.com/qiaomengnan…

    使用Prometheus收集指標(biāo)

    應(yīng)用的指標(biāo)暴露出來,就該使用Prometheus暴露指標(biāo)了,我們使用docker啟動一個Prometheus進(jìn)行采集:

    docker run -p 9090:9090 -v /Users/xxxx/Downloads/prometheus.yml:/etc/prometheus/prometheus.yml -d prom/prometheus 復(fù)制代碼

    prometheus.yml文件里面配置我們需要采集的應(yīng)用地址:

    docker 啟動成功后,通過訪問 http://localhost:9090/targets ,即可看見如下界面,說明我們已經(jīng)成功對應(yīng)用進(jìn)行采集了。

    使用 grafana 顯示指標(biāo)

    通過docker啟動grafana:

    docker run -d --name=grafana -p 3000:3000 grafana/grafana 復(fù)制代碼

    啟動完成后,通過 http://localhost:3000/login 訪問登錄界面,默認(rèn)登錄名和密碼都是admin,

    進(jìn)入首頁后,第一步是添加datasource,即數(shù)據(jù)來源,也就是我們的Prometheus

    輸入Prometheus的地址:

    再點(diǎn)擊Save&Test,進(jìn)行保存

    保存之后點(diǎn)擊返回,然后點(diǎn)擊添加Dashboard,

    在添加面板中,選擇數(shù)據(jù)源為Prometheus,之后就可以輸入我們面板中需要展示的指標(biāo)數(shù)據(jù)了:

    輸入完畢后,點(diǎn)擊apply,我們的面板展示則OK展示:

    end。

    總結(jié)

    以上是生活随笔為你收集整理的Prometheus自定义线程池指标暴露的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。