Golang自定義開發(fā)Prometheus?exporter詳解
1.介紹
Exporter是基于Prometheus實(shí)施的監(jiān)控系統(tǒng)中重要的組成部分,承擔(dān)數(shù)據(jù)指標(biāo)的采集工作,官方的exporter列表中已經(jīng)包含了常見的絕大多數(shù)的系統(tǒng)指標(biāo)監(jiān)控,比如用于機(jī)器性能監(jiān)控的node_exporter, 用于網(wǎng)絡(luò)設(shè)備監(jiān)控的snmp_exporter等等。這些已有的exporter對(duì)于監(jiān)控來說,僅僅需要很少的配置工作就能提供完善的數(shù)據(jù)指標(biāo)采集。
prometheus四種類型的指標(biāo)Counter 計(jì)數(shù),Gauge 觀測(cè)類,Histogram 直方,Summary 摘要 用golang語言如何構(gòu)造這4種類型對(duì)應(yīng)的指標(biāo),二是搞清楚修改指標(biāo)值的場(chǎng)景和方式。
不帶label的基本例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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(29.56) // 注冊(cè)指標(biāo) prometheus.MustRegister(cpuUsage) // 暴露指標(biāo) http.Handle("/metrics", promhttp.Handler()) http.ListenAndServe("localhost:9100", nil) }
帶有固定label指標(biāo)的例子
帶有非固定label指標(biāo)的例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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("localhost:9100", nil) }
2. Counter指標(biāo)類型
不帶label的基本例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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("localhost:9100", nil) }
帶有固定label指標(biāo)的例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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("localhost:9100", nil) }
帶有非固定label指標(biāo)的例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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("localhost:9100", nil) }
3. Histogram指標(biāo)類型
不帶label的基本例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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開始,寬度3,共6個(gè)Bucket }) // 注冊(cè)指標(biāo) prometheus.MustRegister(reqDelay) // 設(shè)置值 reqDelay.Observe(6) // 暴露指標(biāo) http.Handle("/metrics", promhttp.Handler()) http.ListenAndServe("localhost:9100", nil) }
帶固定label的例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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開始,寬度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("localhost:9100", nil) }
帶有非固定label的例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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開始,寬度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("localhost:9100", nil) }
4.Summary指標(biāo)類型
不帶label的例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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("localhost:9100", nil) }
帶有固定label的例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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("localhost:9100", nil) }
帶有非固定label的例子
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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("localhost:9100", nil) }
5. 值的修改
5.1 基于事件的觸發(fā)來修改值,比如每訪問1次/api就增1
基于事件的觸發(fā)對(duì)指標(biāo)的值進(jìn)行修改,通常大多數(shù)是來自業(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" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) 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("localhost:9100", nil) }
基于時(shí)間周期的觸發(fā)來修改值,比如下面的示例中,是每間隔1秒獲取cpu負(fù)載指標(biāo)
基于時(shí)間周期的觸發(fā),可以是多少秒、分、時(shí)、日、月、周。
package main import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/shirou/gopsutil/load" "net/http" "time" ) func main() { cpuUsage := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Name: "CpuLoad", Help: "CPU負(fù)載", }, []string{"time"}) // 開啟一個(gè)子協(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("localhost:9100", nil) }
基于每訪問一次獲取指標(biāo)的URI才修改值,比如每次訪問/metrics才去修改某些指標(biāo)的值
下面的這個(gè)示例是,每訪問一次/metrics就獲取一次內(nèi)存總?cè)萘?/p>
package main import ( "fmt" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/shirou/gopsutil/mem" "net/http" ) 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("localhost:9100", nil) }
到此這篇關(guān)于Golang自定義開發(fā)Prometheus exporter詳解的文章就介紹到這了,更多相關(guān)Golang Prometheus exporter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
K8s部署發(fā)布Golang應(yīng)用程序的實(shí)現(xiàn)方法
本文主要介紹了K8s部署發(fā)布Golang應(yīng)用程序的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07Go語言實(shí)現(xiàn)基于websocket瀏覽器通知功能
這篇文章主要介紹了Go語言實(shí)現(xiàn)基于websocket瀏覽器通知功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07一文帶你搞懂Golang結(jié)構(gòu)體內(nèi)存布局
結(jié)構(gòu)體在Go語言中是一個(gè)很重要的部分,在項(xiàng)目中會(huì)經(jīng)常用到。這篇文章主要帶大家看一下結(jié)構(gòu)體在內(nèi)存中是怎么分布的?通過對(duì)內(nèi)存布局的了解,可以幫助我們寫出更優(yōu)質(zhì)的代碼。感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助2022-10-10Golang程序漏洞檢測(cè)器govulncheck的安裝和使用
govulncheck 是一個(gè)命令行工具,可以幫助 Golang 開發(fā)者快速找到項(xiàng)目代碼和依賴的模塊中的安全漏洞,該工具可以分析源代碼和二進(jìn)制文件,識(shí)別代碼中對(duì)這些漏洞的任何直接或間接調(diào)用,本文就給大家介紹一下govulncheck安裝和使用,需要的朋友可以參考下2023-09-09Go實(shí)現(xiàn)短url項(xiàng)目的方法示例
這篇文章主要介紹了Go實(shí)現(xiàn)短url項(xiàng)目的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03利用 Go 語言編寫一個(gè)簡單的 WebSocket 推送服務(wù)
這篇文章主要介紹了利用 Go 語言編寫一個(gè)簡單的 WebSocket 推送服務(wù),需要的朋友可以參考下2018-04-04