一文帶你玩轉(zhuǎn)Golang Prometheus Eexporter開(kāi)發(fā)
本篇內(nèi)容有點(diǎn)長(zhǎng),代碼有點(diǎn)多。有興趣的可以堅(jiān)持看下去,并動(dòng)手實(shí)踐,沒(méi)興趣的可以劃走。本文分兩大塊,一是搞清楚prometheus四種類(lèi)型的指標(biāo)Counter,Gauge,Histogram,Summary用golang語(yǔ)言如何構(gòu)造這4種類(lèi)型對(duì)應(yīng)的指標(biāo),二是搞清楚修改指標(biāo)值的場(chǎng)景和方式。
| 指標(biāo)類(lèi)型 | 類(lèi)別 | 描述 | 可應(yīng)用場(chǎng)景 |
|---|---|---|---|
| Counter | 計(jì)數(shù)類(lèi) | 使用在累計(jì)指標(biāo)單調(diào)遞增或遞減情況下,只能在目標(biāo)重啟后自動(dòng)歸零 | 服務(wù)請(qǐng)求處理數(shù)量、已完成任務(wù)數(shù)量、錯(cuò)誤數(shù)量 |
| Guage | 測(cè)量類(lèi) | 使用可增可減的的數(shù)據(jù)情況下 | 當(dāng)前內(nèi)存/CPU使用情況、并發(fā)請(qǐng)求數(shù)量 |
| Histogram | 直方圖類(lèi) | 使用統(tǒng)計(jì)指標(biāo)信息在不同區(qū)間內(nèi)的統(tǒng)計(jì)數(shù)量 | 延遲時(shí)間、響應(yīng)大小。例如:0-1秒內(nèi)的延遲時(shí)間、、0-5秒內(nèi)的延遲時(shí)間、例如0-1kb之內(nèi)的響應(yīng)大小、0-5kb之內(nèi)的響應(yīng)大小 |
| Summary | 摘要類(lèi) | 類(lèi)似于直方圖,在客戶(hù)端對(duì)百分位進(jìn)行統(tǒng)計(jì) | 延遲時(shí)間、響應(yīng)大小。例如:超過(guò)百分之多少的人要滿(mǎn)足需求的話,需要多長(zhǎng)時(shí)間完成。 |
1. Gauge指標(biāo)類(lèi)型
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)名稱(chēng)
??Help:?"this?is?test?metrics?cpu?usage",?//?幫助信息
?})
?//?給指標(biāo)設(shè)置值
?cpuUsage.Set(89.56)
?//?注冊(cè)指標(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)名稱(chēng)
??Help:????????"this?is?test?metrics?cpu?usage",?//?幫助信息
??ConstLabels:?prometheus.Labels{"MachineType":?"host"},?//?label
?})
?//?給指標(biāo)設(shè)置值
?cpuUsage.Set(89.56)
?//?注冊(cè)指標(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)
?//?注冊(cè)指標(biāo)
?prometheus.MustRegister(mtu)
?//?暴露指標(biāo)
?http.Handle("/metrics",?promhttp.Handler())
?http.ListenAndServe(":9900",?nil)
}
2. Counter指標(biāo)類(lèi)型
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)前請(qǐng)求總數(shù)",
?})
?//?注冊(cè)指標(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:????????"請(qǐng)求成功的總數(shù)",
??ConstLabels:?prometheus.Labels{"StatusCode":?"200"},
?})
?//?注冊(cè)指標(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請(qǐng)求總數(shù)",
?},?[]string{"path"})
?//?注冊(cè)指標(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)類(lèi)型
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:????"請(qǐng)求延遲,單位秒",
??Buckets:?prometheus.LinearBuckets(0,?3,?6),?//?調(diào)用LinearBuckets生成區(qū)間,從0開(kāi)始,寬度3,共6個(gè)Bucket
?})
?//?注冊(cè)指標(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:????????"請(qǐng)求延遲,單位秒",
??Buckets:?????prometheus.LinearBuckets(0,?3,?6),?//?調(diào)用LinearBuckets生成區(qū)間,從0開(kāi)始,寬度3,共6個(gè)Bucket
??ConstLabels:?prometheus.Labels{"path":?"/api"},
?})
?//?注冊(cè)指標(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:????"請(qǐng)求延遲,單位秒",
??Buckets:?prometheus.LinearBuckets(0,?3,?6),?//?調(diào)用LinearBuckets生成區(qū)間,從0開(kāi)始,寬度3,共6個(gè)Bucket
?},?[]string{"path"})
?//?注冊(cè)指標(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)類(lèi)型
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:???????"請(qǐng)求延遲",
??Objectives:?map[float64]float64{0.5:?0.05,?0.90:?0.01,?0.99:?0.001},?//?百分比:精度
?})
?//?注冊(cè)指標(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:????????"請(qǐng)求延遲",
??Objectives:??map[float64]float64{0.5:?0.05,?0.90:?0.01,?0.99:?0.001},?//?百分比:精度
??ConstLabels:?prometheus.Labels{"path":?"/api"},
?})
?//?注冊(cè)指標(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:???????"請(qǐng)求延遲",
??Objectives:?map[float64]float64{0.5:?0.05,?0.90:?0.01,?0.99:?0.001},?//?百分比:精度
?},?[]string{"path"})
?//?注冊(cè)指標(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ā)來(lái)修改值,比如每訪問(wèn)1次/api就增1
基于事件的觸發(fā)對(duì)指標(biāo)的值進(jìn)行修改,通常大多數(shù)是來(lái)自業(yè)務(wù)方面的指標(biāo)需求,如自研的應(yīng)用需要暴露相關(guān)指標(biāo)給promethus進(jìn)行監(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請(qǐng)求累計(jì)?單位?次",
?},?[]string{"path"})
?http.HandleFunc("/api",?func(w?http.ResponseWriter,?r?*http.Request)?{
??urlRequestTotal.WithLabelValues(r.URL.Path).Inc()?//?使用Inc函數(shù)進(jìn)行增1
??fmt.Fprintf(w,?"Welcome?to?the?api")
?})
?prometheus.MustRegister(urlRequestTotal)
?http.Handle("/metrics",?promhttp.Handler())
?http.ListenAndServe(":9900",?nil)
}
5.2 基于時(shí)間周期的觸發(fā)來(lái)修改值,比如下面的示例中,是每間隔1秒獲取cpu負(fù)載指標(biāo)
基于時(shí)間周期的觸發(fā),可以是多少秒、分、時(shí)、日、月、周。
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負(fù)載",
?},?[]string{"time"})
?//?開(kāi)啟一個(gè)子協(xié)程執(zhí)行該匿名函數(shù)內(nèi)的邏輯來(lá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 基于每訪問(wèn)一次獲取指標(biāo)的URI才修改值,比如每次訪問(wèn)/metrics才去修改某些指標(biāo)的值
下面的這個(gè)示例是,每訪問(wèn)一次/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)類(lèi)型有對(duì)應(yīng)的函數(shù),分別是NewGaugeFunc和NewCounterFunc,而且是固定的label。
到此這篇關(guān)于一文帶你玩轉(zhuǎn)Golang Prometheus Eexporter開(kāi)發(fā)的文章就介紹到這了,更多相關(guān)Golang Prometheus Eexporter開(kāi)發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
GoFrame框架gset使用對(duì)比PHP?Java?Redis優(yōu)勢(shì)
這篇文章主要為大家介紹了GoFrame框架gset對(duì)比PHP?Java?Redis的使用優(yōu)勢(shì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Golang時(shí)間及時(shí)間戳的獲取轉(zhuǎn)換超全面詳細(xì)講解
說(shuō)實(shí)話,golang的時(shí)間轉(zhuǎn)化還是很麻煩的,最起碼比php麻煩很多,下面這篇文章主要給大家介紹了關(guān)于golang時(shí)間/時(shí)間戳的獲取與轉(zhuǎn)換的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
golang實(shí)現(xiàn)ip訪問(wèn)限制及提交次數(shù)
在?Web?應(yīng)用中,通常會(huì)需要對(duì)?IP?訪問(wèn)進(jìn)行限制以及控制提交次數(shù),本文將使用中間件或者基于?Redis?這樣的緩存服務(wù)來(lái)實(shí)現(xiàn),感興趣的可以了解下2024-10-10
Go語(yǔ)言多人聊天室項(xiàng)目實(shí)戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言多人聊天室項(xiàng)目實(shí)戰(zhàn),實(shí)現(xiàn)單撩或多撩等多種功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
基于Go語(yǔ)言簡(jiǎn)單實(shí)現(xiàn)事件管理器
在編程中,事件管理器是一種常見(jiàn)的工具,用于通過(guò)通知來(lái)觸發(fā)操作,本文將介紹一個(gè)簡(jiǎn)單的Go事件管理器的實(shí)現(xiàn),并通過(guò)異步改進(jìn)提高其性能,感興趣的可以了解下2023-11-11
Go 1.23中Timer無(wú)buffer的實(shí)現(xiàn)方式詳解
在 Go 1.23 中,Timer 的實(shí)現(xiàn)通常是通過(guò) time 包提供的 time.Timer 類(lèi)型來(lái)實(shí)現(xiàn)的,本文主要介紹了Go 1.23中Timer無(wú)buffer的實(shí)現(xiàn)方式,需要的可以了解下2025-03-03
Golang無(wú)限緩存channel的設(shè)計(jì)與實(shí)現(xiàn)解析
這篇文章主要為大家介紹了Golang無(wú)限緩存channel的設(shè)計(jì)與實(shí)現(xiàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

