springboot 2.x + prometheus +grafana

一、springboot2.x 集成prometheus

1.pom 文件引入prometheus的jar包

springboot2.x與prometheus的集成與springboot1.x區(qū)別比較大。prometheus官方提供的Java client不能支持2.x的處理。

springboot 2.x 中與prometheus集成

2.application中配置

3.項(xiàng)目注冊(cè)配置

4.在prometheus中配置自定義的項(xiàng)目

5.啟動(dòng)springbot 項(xiàng)目,且到prometheus的安裝目錄下啟動(dòng)prometheus。訪問(wèn)http://localhost:9090可看到如下界面。點(diǎn)擊status 下targets??煽吹椒?wù)狀態(tài)。點(diǎn)擊各個(gè)endpoint可在其中看到他們的指標(biāo)。

二、prometheus中一些指標(biāo)

metric type

prometheus 定義了4種不同的指標(biāo)類型。

counter(計(jì)數(shù)器):只增不減的計(jì)數(shù)器。它通常用于記錄服務(wù)的請(qǐng)求數(shù)量、完成的任務(wù)數(shù)量、錯(cuò)誤的發(fā)生數(shù)量等等。

方式一:

?Counter counter = Counter.builder("counter_job_counter1") .tags(new String[]{"name", "tag_job_counter1"}) .description("counter job counter").register(meterRegistry);

counter.increment();

方式二:

Counter counter = registry.counter("counter");

gauge(儀表盤):是可任意加減。Gauge通常用于變動(dòng)的測(cè)量值,如當(dāng)前的內(nèi)存使用情況,同時(shí)也可以測(cè)量上下移動(dòng)的"計(jì)數(shù)",比如隊(duì)列中的消息數(shù)量、

方式一:

Gauge.builder("gn.temp.gauge", new AtomicInteger(1), AtomicInteger::get)

方式二:

registry.gauge("gn.temp.gauge", Tags.of("site", "SiteA", "cab", "cab01"), new AtomicInteger(1));

timer(計(jì)時(shí)器):同時(shí)測(cè)量一個(gè)特定的代碼邏輯塊的調(diào)用(執(zhí)行)速度和它的時(shí)間分布。簡(jiǎn)單來(lái)說(shuō),就是在調(diào)用結(jié)束的時(shí)間點(diǎn)記錄整個(gè)調(diào)用塊執(zhí)行的總時(shí)間,適用于測(cè)量短時(shí)間執(zhí)行的事件的耗時(shí)分布,例如消息隊(duì)列消息的消費(fèi)速率

Timer timer = Timer .builder("my.timer").description("a description of what this timer does").tags("region", "test") .register(registry);

histogram(直方圖):柱狀圖,用于觀察結(jié)果采樣,分組及統(tǒng)計(jì),如:請(qǐng)求持續(xù)時(shí)間,響應(yīng)大小。其主要用于表示一段時(shí)間內(nèi)對(duì)數(shù)據(jù)的采樣,并能夠?qū)ζ渲付▍^(qū)間及總數(shù)進(jìn)行統(tǒng)計(jì)。根據(jù)統(tǒng)計(jì)區(qū)間計(jì)算

histogram.builder("my.ratio").scale(100).sla(70, 80, 90).register(registry)

summary(摘要):用于表示一段時(shí)間內(nèi)數(shù)據(jù)采樣結(jié)果,其直接存儲(chǔ)quantile數(shù)據(jù),而不是根據(jù)統(tǒng)計(jì)區(qū)間計(jì)算出來(lái)的。不需要計(jì)算,直接存儲(chǔ)結(jié)果

方式一:

DistributionSummary summary = registry.summary("response.size");

方式二:

DistributionSummary summary = DistributionSummary.builder("response.size").description("a description of what this summary does") .baseUnit("bytes").tags("region", "test").scale(100).register(registry);

幾種內(nèi)置的Registry

SimpleMeterRegistry:保留最新數(shù)據(jù)到內(nèi)存中。默認(rèn)Spring會(huì)幫忙autowire一個(gè)

CompositeMeterRegistry:用組合模式幫你將多個(gè)注冊(cè)表串聯(lián)成一個(gè)對(duì)外接口。全局的Metrics.globalRegistry就是這么個(gè)組合模式接口

GlobalRegistry:也是一種CompositeMeterRegistry

三、項(xiàng)目中也可以自定義一些指標(biāo)。例如計(jì)入http訪問(wèn)請(qǐng)求數(shù)量

1.初始化counter指標(biāo),并繼承攔截器。在每次請(qǐng)求后請(qǐng)求數(shù)量加1.

2.將過(guò)濾器添加到過(guò)濾鏈中

3.隨意寫(xiě)一個(gè)controller.添加方法。項(xiàng)目重啟啟動(dòng)后,訪問(wèn)htto后,可在prometheus中看到統(tǒng)計(jì)的指標(biāo)。

四、將自定義指標(biāo)展示在grafana中

啟動(dòng)grafa .訪問(wèn)localhost:8088.new dashbord.選擇要顯示的指標(biāo)。如圖


引用:https://segmentfault.com/a/1190000018642077?utm_source=tag-newes

https://cloud.tencent.com/developer/article/1477642

https://segmentfault.com/a/1190000018642077?utm_source=tag-newest

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容