prometheus?client_go為應(yīng)用程序自定義監(jiān)控指標(biāo)
使用prometheus client_go為應(yīng)用程序添加監(jiān)控指標(biāo)
使用prometheus client_go為應(yīng)用程序添加監(jiān)控指標(biāo)時(shí),通常為http注冊(cè)一個(gè)client_go默認(rèn)的handler,這樣就可以通過(guò)/metrics接口,拉取應(yīng)用程序的metrics指標(biāo)了:
http.Handle("/metrics", promhttp.Handler())但是,該默認(rèn)的handler會(huì)自動(dòng)引入go的指標(biāo)和proc的指標(biāo):
go的指標(biāo):
go_gc_duration_seconds go_goroutines go_info ....
proc的指標(biāo)
process_start_time_seconds process_cpu_seconds_total ....
默認(rèn)handler為啥會(huì)引入go指標(biāo)和proc指標(biāo),如果不需要要,可以去掉它們嗎?
原因
從源碼中找原因,http handler:
http.Handle("/metrics", promhttp.Handler())client_go中該handler的實(shí)現(xiàn):
// prometheus/client_golang/prometheus/promhttp/http.go
func Handler() http.Handler {
return InstrumentMetricHandler(
prometheus.DefaultRegisterer, HandlerFor(prometheus.DefaultGatherer, HandlerOpts{}),
)
}其中DefaultRegister、DefaultGather指向同一個(gè)Registry對(duì)象,即defaultRegistry:
// prometheus/client_golang/prometheus/registry.go
var (
defaultRegistry = NewRegistry()
DefaultRegisterer Registerer = defaultRegistry
DefaultGatherer Gatherer = defaultRegistry
)
func init() {
MustRegister(NewProcessCollector(ProcessCollectorOpts{})) // 采集Proc指標(biāo)
MustRegister(NewGoCollector()) // 采集Go指標(biāo)
}
func MustRegister(cs ...Collector) {
DefaultRegisterer.MustRegister(cs...)
}該Registry對(duì)象在init()中,被注入了:
- NewProcessCollector:采集進(jìn)程的指標(biāo)信息;
- NewGoCollector: 采集go runtime的指標(biāo)信息;
去掉Proc和Go指標(biāo)
在實(shí)現(xiàn)自己的exporter或?yàn)閼?yīng)用程序添加指標(biāo)時(shí),若不需要Proc/Go指標(biāo),可以:
- 不使用 defaultRegister,自己 NewRegister,自定義使用哪些collector,即可去掉 Proc/Go 指標(biāo);
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 創(chuàng)建一個(gè)自定義的注冊(cè)表
registry := prometheus.NewRegistry()
// 可選: 添加 process 和 Go 運(yùn)行時(shí)指標(biāo)到我們自定義的注冊(cè)表中
registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
registry.MustRegister(prometheus.NewGoCollector())
// 創(chuàng)建一個(gè)簡(jiǎn)單的 gauge 指標(biāo)。
temp := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "home_temperature_celsius",
Help: "The current temperature in degrees Celsius.",
})
// 使用我們自定義的注冊(cè)表注冊(cè) gauge
registry.MustRegister(temp)
// 設(shè)置 gague 的值為 39
temp.Set(39)
// 暴露自定義指標(biāo)
http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{Registry: registry}))
http.ListenAndServe(":8080", nil)
}其中:
prometheus.NewRegistry()創(chuàng)建自己的注冊(cè)表(不使用defaultRegistry);
registry.MustRegister():
- 若添加了ProcessCollector,會(huì)自動(dòng)添加process_*監(jiān)控指標(biāo);
- 若添加了GoCollector,會(huì)自動(dòng)添加go_*監(jiān)控指標(biāo);
- promhttp.HandlerFor創(chuàng)建針對(duì)registry的http handler;
- promhttp.HandlerOpts{Registry: registry}: 將添加promhttp_*相關(guān)的指標(biāo);
參考: https://github.com/prometheus...
以上就是prometheus client_go為應(yīng)用程序自定義監(jiān)控指標(biāo)的詳細(xì)內(nèi)容,更多關(guān)于prometheus client_go監(jiān)控指標(biāo)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- golang調(diào)試bug及性能監(jiān)控方式實(shí)踐總結(jié)
- golang?pprof?監(jiān)控goroutine?thread統(tǒng)計(jì)原理詳解
- golang?pprof監(jiān)控memory?block?mutex統(tǒng)計(jì)原理分析
- golang?pprof監(jiān)控memory?block?mutex使用指南
- golang?pprof?監(jiān)控系列?go?trace統(tǒng)計(jì)原理與使用解析
- web項(xiàng)目中g(shù)olang性能監(jiān)控解析
- Go語(yǔ)言metrics應(yīng)用監(jiān)控指標(biāo)基本使用說(shuō)明
- Skywalking-go自動(dòng)監(jiān)控增強(qiáng)使用探究
相關(guān)文章
Go語(yǔ)言HTTP請(qǐng)求流式寫入body的示例代碼
這篇文章主要介紹了Go語(yǔ)言HTTP請(qǐng)求流式寫入body,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
sublime3+Golang+代碼補(bǔ)全的實(shí)現(xiàn)
本文主要介紹了sublime3+Golang+代碼補(bǔ)全的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
詳解在Go語(yǔ)言單元測(cè)試中如何解決Redis存儲(chǔ)依賴問(wèn)題
在編寫單元測(cè)試時(shí),除了?MySQL?這個(gè)外部存儲(chǔ)依賴,Redis?應(yīng)該是另一個(gè)最為常見(jiàn)的外部存儲(chǔ)依賴了,本文就來(lái)講解下如何解決?Redis?外部依賴,文章通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
GoLang語(yǔ)法之標(biāo)準(zhǔn)庫(kù)fmt.Printf的使用
fmt包實(shí)現(xiàn)了類似C語(yǔ)言printf和scanf的格式化I/O,主要分為向外輸出內(nèi)容和獲取輸入內(nèi)容兩大部分,本文就來(lái)介紹一下GoLang語(yǔ)法之標(biāo)準(zhǔn)庫(kù)fmt.Printf的使用,感興趣的可以了解下2023-10-10
關(guān)于go語(yǔ)言載入json可能遇到的一個(gè)坑
Go 語(yǔ)言從新手到大神,每個(gè)人多少都會(huì)踩一些坑,那么下面這篇文章主要給大家介紹了關(guān)于go語(yǔ)言載入json可能遇到的一個(gè)坑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07
Golang實(shí)現(xiàn)Md5校驗(yàn)的示例代碼
本文主要介紹了Golang實(shí)現(xiàn)Md5校驗(yàn)的示例代碼,要求接收方需要文件的md5值,和接收到的文件做比對(duì),以免文件不完整,但引起bug,下面就一起來(lái)解決一下2024-08-08

