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)時,通常為http注冊一個client_go默認(rèn)的handler,這樣就可以通過/metrics接口,拉取應(yīng)用程序的metrics指標(biāo)了:
http.Handle("/metrics", promhttp.Handler())
但是,該默認(rèn)的handler會自動引入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為啥會引入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指向同一個Registry對象,即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對象在init()中,被注入了:
- NewProcessCollector:采集進(jìn)程的指標(biāo)信息;
- NewGoCollector: 采集go runtime的指標(biāo)信息;
去掉Proc和Go指標(biāo)
在實(shí)現(xiàn)自己的exporter或為應(yīng)用程序添加指標(biāo)時,若不需要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)建一個自定義的注冊表 registry := prometheus.NewRegistry() // 可選: 添加 process 和 Go 運(yùn)行時指標(biāo)到我們自定義的注冊表中 registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) registry.MustRegister(prometheus.NewGoCollector()) // 創(chuàng)建一個簡單的 gauge 指標(biāo)。 temp := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "home_temperature_celsius", Help: "The current temperature in degrees Celsius.", }) // 使用我們自定義的注冊表注冊 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)建自己的注冊表(不使用defaultRegistry);
registry.MustRegister():
- 若添加了ProcessCollector,會自動添加process_*監(jiān)控指標(biāo);
- 若添加了GoCollector,會自動添加go_*監(jiān)控指標(biāo);
- promhttp.HandlerFor創(chuàng)建針對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)的資料請關(guān)注腳本之家其它相關(guān)文章!
- golang調(diào)試bug及性能監(jiān)控方式實(shí)踐總結(jié)
- golang?pprof?監(jiān)控goroutine?thread統(tǒng)計原理詳解
- golang?pprof監(jiān)控memory?block?mutex統(tǒng)計原理分析
- golang?pprof監(jiān)控memory?block?mutex使用指南
- golang?pprof?監(jiān)控系列?go?trace統(tǒng)計原理與使用解析
- web項目中g(shù)olang性能監(jiān)控解析
- Go語言metrics應(yīng)用監(jiān)控指標(biāo)基本使用說明
- Skywalking-go自動監(jiān)控增強(qiáng)使用探究
相關(guān)文章
sublime3+Golang+代碼補(bǔ)全的實(shí)現(xiàn)
本文主要介紹了sublime3+Golang+代碼補(bǔ)全的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01GoLang語法之標(biāo)準(zhǔn)庫fmt.Printf的使用
fmt包實(shí)現(xiàn)了類似C語言printf和scanf的格式化I/O,主要分為向外輸出內(nèi)容和獲取輸入內(nèi)容兩大部分,本文就來介紹一下GoLang語法之標(biāo)準(zhǔn)庫fmt.Printf的使用,感興趣的可以了解下2023-10-10