一文帶你玩轉(zhuǎn)Golang Prometheus Eexporter開發(fā)
本篇內(nèi)容有點長,代碼有點多。有興趣的可以堅持看下去,并動手實踐,沒興趣的可以劃走。本文分兩大塊,一是搞清楚prometheus四種類型的指標(biāo)Counter,Gauge,Histogram,Summary用golang語言如何構(gòu)造這4種類型對應(yīng)的指標(biāo),二是搞清楚修改指標(biāo)值的場景和方式。
指標(biāo)類型 | 類別 | 描述 | 可應(yīng)用場景 |
---|---|---|---|
Counter | 計數(shù)類 | 使用在累計指標(biāo)單調(diào)遞增或遞減情況下,只能在目標(biāo)重啟后自動歸零 | 服務(wù)請求處理數(shù)量、已完成任務(wù)數(shù)量、錯誤數(shù)量 |
Guage | 測量類 | 使用可增可減的的數(shù)據(jù)情況下 | 當(dāng)前內(nèi)存/CPU使用情況、并發(fā)請求數(shù)量 |
Histogram | 直方圖類 | 使用統(tǒng)計指標(biāo)信息在不同區(qū)間內(nèi)的統(tǒng)計數(shù)量 | 延遲時間、響應(yīng)大小。例如:0-1秒內(nèi)的延遲時間、、0-5秒內(nèi)的延遲時間、例如0-1kb之內(nèi)的響應(yīng)大小、0-5kb之內(nèi)的響應(yīng)大小 |
Summary | 摘要類 | 類似于直方圖,在客戶端對百分位進行統(tǒng)計 | 延遲時間、響應(yīng)大小。例如:超過百分之多少的人要滿足需求的話,需要多長時間完成。 |
1. Gauge指標(biāo)類型
1.1 不帶label的基本例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?cpuUsage?:=?prometheus.NewGauge(prometheus.GaugeOpts{ ??Name:?"cpu_usage",??????????????????????//?指標(biāo)名稱 ??Help:?"this?is?test?metrics?cpu?usage",?//?幫助信息 ?}) ?//?給指標(biāo)設(shè)置值 ?cpuUsage.Set(89.56) ?//?注冊指標(biāo) ?prometheus.MustRegister(cpuUsage) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
1.2 帶有固定label指標(biāo)的例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?cpuUsage?:=?prometheus.NewGauge(prometheus.GaugeOpts{ ??Name:????????"cpu_usage",??????????????????????//?指標(biāo)名稱 ??Help:????????"this?is?test?metrics?cpu?usage",?//?幫助信息 ??ConstLabels:?prometheus.Labels{"MachineType":?"host"},?//?label ?}) ?//?給指標(biāo)設(shè)置值 ?cpuUsage.Set(89.56) ?//?注冊指標(biāo) ?prometheus.MustRegister(cpuUsage) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
1.3 帶有非固定label指標(biāo)的例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//定義帶有不固定label的指標(biāo) ?mtu?:=?prometheus.NewGaugeVec(prometheus.GaugeOpts{ ??Name:?"interface_mtu", ??Help:?"網(wǎng)卡接口MTU", ?},?[]string{"interface",?"Machinetype"}) ?//?給指標(biāo)設(shè)置值 ?mtu.WithLabelValues("lo",?"host").Set(1500) ?mtu.WithLabelValues("ens32",?"host").Set(1500) ?mtu.WithLabelValues("eth0",?"host").Set(1500) ?//?注冊指標(biāo) ?prometheus.MustRegister(mtu) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
2. Counter指標(biāo)類型
2.1 不帶label的基本例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?reqTotal?:=?prometheus.NewCounter(prometheus.CounterOpts{ ??Name:?"current_request_total", ??Help:?"當(dāng)前請求總數(shù)", ?}) ?//?注冊指標(biāo) ?prometheus.MustRegister(reqTotal) ?//?設(shè)置值 ?reqTotal.Add(10) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
2.2 帶有固定label指標(biāo)的例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?suceReqTotal?:=?prometheus.NewCounter(prometheus.CounterOpts{ ??Name:????????"sucess_request_total", ??Help:????????"請求成功的總數(shù)", ??ConstLabels:?prometheus.Labels{"StatusCode":?"200"}, ?}) ?//?注冊指標(biāo) ?prometheus.MustRegister(suceReqTotal) ?//?設(shè)置值 ?suceReqTotal.Add(5675) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
2.3 帶有非固定label指標(biāo)的例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?pathReqTotal?:=?prometheus.NewCounterVec(prometheus.CounterOpts{ ??Name:?"path_request_total", ??Help:?"path請求總數(shù)", ?},?[]string{"path"}) ?//?注冊指標(biāo) ?prometheus.MustRegister(pathReqTotal) ?//?設(shè)置值 ?pathReqTotal.WithLabelValues("/token").Add(37) ?pathReqTotal.WithLabelValues("/auth").Add(23) ?pathReqTotal.WithLabelValues("/user").Add(90) ?pathReqTotal.WithLabelValues("/api").Add(67) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
3. Histogram指標(biāo)類型
3.1 不帶label的基本例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?reqDelay?:=?prometheus.NewHistogram(prometheus.HistogramOpts{ ??Name:????"request_delay", ??Help:????"請求延遲,單位秒", ??Buckets:?prometheus.LinearBuckets(0,?3,?6),?//?調(diào)用LinearBuckets生成區(qū)間,從0開始,寬度3,共6個Bucket ?}) ?//?注冊指標(biāo) ?prometheus.MustRegister(reqDelay) ?//?設(shè)置值 ?reqDelay.Observe(6) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
3.2 帶固定label的例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?reqDelay?:=?prometheus.NewHistogram(prometheus.HistogramOpts{ ??Name:????????"request_delay", ??Help:????????"請求延遲,單位秒", ??Buckets:?????prometheus.LinearBuckets(0,?3,?6),?//?調(diào)用LinearBuckets生成區(qū)間,從0開始,寬度3,共6個Bucket ??ConstLabels:?prometheus.Labels{"path":?"/api"}, ?}) ?//?注冊指標(biāo) ?prometheus.MustRegister(reqDelay) ?//?設(shè)置值 ?reqDelay.Observe(6) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
3.3 帶有非固定label的例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?reqDelay?:=?prometheus.NewHistogramVec(prometheus.HistogramOpts{ ??Name:????"request_delay", ??Help:????"請求延遲,單位秒", ??Buckets:?prometheus.LinearBuckets(0,?3,?6),?//?調(diào)用LinearBuckets生成區(qū)間,從0開始,寬度3,共6個Bucket ?},?[]string{"path"}) ?//?注冊指標(biāo) ?prometheus.MustRegister(reqDelay) ?//?設(shè)置值 ?reqDelay.WithLabelValues("/api").Observe(6) ?reqDelay.WithLabelValues("/user").Observe(3) ?reqDelay.WithLabelValues("/delete").Observe(2) ?reqDelay.WithLabelValues("/get_token").Observe(13) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
4. Summary指標(biāo)類型
4.1 不帶label的例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?reqDelay?:=?prometheus.NewSummary(prometheus.SummaryOpts{ ??Name:???????"request_delay", ??Help:???????"請求延遲", ??Objectives:?map[float64]float64{0.5:?0.05,?0.90:?0.01,?0.99:?0.001},?//?百分比:精度 ?}) ?//?注冊指標(biāo) ?prometheus.MustRegister(reqDelay) ?//?設(shè)置值 ?reqDelay.Observe(4) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
4.2 帶有固定label的例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?reqDelay?:=?prometheus.NewSummary(prometheus.SummaryOpts{ ??Name:????????"request_delay", ??Help:????????"請求延遲", ??Objectives:??map[float64]float64{0.5:?0.05,?0.90:?0.01,?0.99:?0.001},?//?百分比:精度 ??ConstLabels:?prometheus.Labels{"path":?"/api"}, ?}) ?//?注冊指標(biāo) ?prometheus.MustRegister(reqDelay) ?//?設(shè)置值 ?reqDelay.Observe(4) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
4.3 帶有非固定label的例子
package?main import?( ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?//?定義指標(biāo) ?reqDelay?:=?prometheus.NewSummaryVec(prometheus.SummaryOpts{ ??Name:???????"request_delay", ??Help:???????"請求延遲", ??Objectives:?map[float64]float64{0.5:?0.05,?0.90:?0.01,?0.99:?0.001},?//?百分比:精度 ?},?[]string{"path"}) ?//?注冊指標(biāo) ?prometheus.MustRegister(reqDelay) ?//?設(shè)置值 ?reqDelay.WithLabelValues("/api").Observe(4) ?reqDelay.WithLabelValues("/token").Observe(2) ?reqDelay.WithLabelValues("/user").Observe(3) ?//?暴露指標(biāo) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
5. 值的修改
5.1 基于事件的觸發(fā)來修改值,比如每訪問1次/api就增1
基于事件的觸發(fā)對指標(biāo)的值進行修改,通常大多數(shù)是來自業(yè)務(wù)方面的指標(biāo)需求,如自研的應(yīng)用需要暴露相關(guān)指標(biāo)給promethus進行監(jiān)控、展示,那么指標(biāo)采集的代碼(指標(biāo)定義、設(shè)置值)就要嵌入到自研應(yīng)用代碼里了。
package?main import?( ?"fmt" ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ) func?main()?{ ?urlRequestTotal?:=?prometheus.NewCounterVec(prometheus.CounterOpts{ ??Name:?"urlRequestTotal", ??Help:?"PATH請求累計?單位?次", ?},?[]string{"path"}) ?http.HandleFunc("/api",?func(w?http.ResponseWriter,?r?*http.Request)?{ ??urlRequestTotal.WithLabelValues(r.URL.Path).Inc()?//?使用Inc函數(shù)進行增1 ??fmt.Fprintf(w,?"Welcome?to?the?api") ?}) ?prometheus.MustRegister(urlRequestTotal) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
5.2 基于時間周期的觸發(fā)來修改值,比如下面的示例中,是每間隔1秒獲取cpu負載指標(biāo)
基于時間周期的觸發(fā),可以是多少秒、分、時、日、月、周。
package?main import?( ?"net/http" ?"time" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ?"github.com/shirou/gopsutil/load" ) func?main()?{ ?cpuUsage?:=?prometheus.NewGaugeVec(prometheus.GaugeOpts{ ??Name:?"CpuLoad", ??Help:?"CPU負載", ?},?[]string{"time"}) ?//?開啟一個子協(xié)程執(zhí)行該匿名函數(shù)內(nèi)的邏輯來給指標(biāo)設(shè)置值,且每秒獲取一次 ?go?func()?{ ??for?range?time.Tick(time.Second)?{ ???info,?_?:=?load.Avg() ???cpuUsage.WithLabelValues("Load1").Set(float64(info.Load1)) ???cpuUsage.WithLabelValues("Load5").Set(float64(info.Load5)) ???cpuUsage.WithLabelValues("Load15").Set(float64(info.Load15)) ??} ?}() ?prometheus.MustRegister(cpuUsage) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
5.3 基于每訪問一次獲取指標(biāo)的URI才修改值,比如每次訪問/metrics才去修改某些指標(biāo)的值
下面的這個示例是,每訪問一次/metrics就獲取一次內(nèi)存總?cè)萘?/p>
package?main import?( ?"fmt" ?"net/http" ?"github.com/prometheus/client_golang/prometheus" ?"github.com/prometheus/client_golang/prometheus/promhttp" ?"github.com/shirou/gopsutil/mem" ) func?main()?{ ?MemTotal?:=?prometheus.NewGaugeFunc(prometheus.GaugeOpts{ ??Name:?"MemTotal", ??Help:?"內(nèi)存總?cè)萘?單位?GB", ?},?func()?float64?{ ??fmt.Println("call?MemTotal?...") ??info,?_?:=?mem.VirtualMemory() ??return?float64(info.Total?/?1024?/?1024?/?1024) ?}) ?prometheus.MustRegister(MemTotal) ?http.Handle("/metrics",?promhttp.Handler()) ?http.ListenAndServe(":9900",?nil) }
目前只有Gauge和Counter的指標(biāo)類型有對應(yīng)的函數(shù),分別是NewGaugeFunc和NewCounterFunc,而且是固定的label。
到此這篇關(guān)于一文帶你玩轉(zhuǎn)Golang Prometheus Eexporter開發(fā)的文章就介紹到這了,更多相關(guān)Golang Prometheus Eexporter開發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang?gorm框架數(shù)據(jù)庫的連接操作示例
這篇文章主要為大家介紹了golang?gorm框架數(shù)據(jù)庫操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04Go標(biāo)準(zhǔn)庫strconv實現(xiàn)string類型與其他基本數(shù)據(jù)類型之間轉(zhuǎn)換
這篇文章主要為大家介紹了Go標(biāo)準(zhǔn)庫strconv實現(xiàn)string類型與其他基本數(shù)據(jù)類型之間轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11golang通過反射設(shè)置結(jié)構(gòu)體變量的值
這篇文章主要介紹了golang通過反射設(shè)置結(jié)構(gòu)體變量的值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04