prometheus?client_go為應(yīng)用程序自定義監(jiān)控指標
使用prometheus client_go為應(yīng)用程序添加監(jiān)控指標
使用prometheus client_go為應(yīng)用程序添加監(jiān)控指標時,通常為http注冊一個client_go默認的handler,這樣就可以通過/metrics接口,拉取應(yīng)用程序的metrics指標了:
http.Handle("/metrics", promhttp.Handler())
但是,該默認的handler會自動引入go的指標和proc的指標:
go的指標:
go_gc_duration_seconds go_goroutines go_info ....
proc的指標
process_start_time_seconds process_cpu_seconds_total ....
默認handler為啥會引入go指標和proc指標,如果不需要要,可以去掉它們嗎?
原因
從源碼中找原因,http handler:
http.Handle("/metrics", promhttp.Handler())
client_go中該handler的實現(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指標 MustRegister(NewGoCollector()) // 采集Go指標 } func MustRegister(cs ...Collector) { DefaultRegisterer.MustRegister(cs...) }
該Registry對象在init()中,被注入了:
- NewProcessCollector:采集進程的指標信息;
- NewGoCollector: 采集go runtime的指標信息;
去掉Proc和Go指標
在實現(xiàn)自己的exporter或為應(yīng)用程序添加指標時,若不需要Proc/Go指標,可以:
- 不使用 defaultRegister,自己 NewRegister,自定義使用哪些collector,即可去掉 Proc/Go 指標;
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 運行時指標到我們自定義的注冊表中 registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) registry.MustRegister(prometheus.NewGoCollector()) // 創(chuàng)建一個簡單的 gauge 指標。 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) // 暴露自定義指標 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)控指標;
- 若添加了GoCollector,會自動添加go_*監(jiān)控指標;
- promhttp.HandlerFor創(chuàng)建針對registry的http handler;
- promhttp.HandlerOpts{Registry: registry}: 將添加promhttp_*相關(guān)的指標;
參考: https://github.com/prometheus...
以上就是prometheus client_go為應(yīng)用程序自定義監(jiān)控指標的詳細內(nèi)容,更多關(guān)于prometheus client_go監(jiān)控指標的資料請關(guān)注腳本之家其它相關(guān)文章!
- golang調(diào)試bug及性能監(jiān)控方式實踐總結(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)控指標基本使用說明
- Skywalking-go自動監(jiān)控增強使用探究