詳解prometheus監(jiān)控golang服務(wù)實(shí)踐記錄
一、prometheus基本原理介紹
prometheus是基于metric采樣的監(jiān)控,可以自定義監(jiān)控指標(biāo),如:服務(wù)每秒請(qǐng)求數(shù)、請(qǐng)求失敗數(shù)、請(qǐng)求執(zhí)行時(shí)間等,每經(jīng)過(guò)一個(gè)時(shí)間間隔,數(shù)據(jù)都會(huì)從運(yùn)行的服務(wù)中流出,存儲(chǔ)到一個(gè)時(shí)間序列數(shù)據(jù)庫(kù)中,之后可通過(guò)PromQL語(yǔ)法查詢。
主要特點(diǎn):
多維數(shù)據(jù)模型,時(shí)間序列數(shù)據(jù)通過(guò)metric名以key、value的形式標(biāo)識(shí);
使用PromQL語(yǔ)法靈活地查詢數(shù)據(jù);
不需要依賴分布式存儲(chǔ),各服務(wù)器節(jié)點(diǎn)是獨(dú)立自治的;
時(shí)間序列的收集,通過(guò) HTTP 調(diào)用,基于pull 模型進(jìn)行拉??;
通過(guò)push gateway推送時(shí)間序列;
通過(guò)服務(wù)發(fā)現(xiàn)或者靜態(tài)配置,來(lái)發(fā)現(xiàn)目標(biāo)服務(wù)對(duì)象;
多種繪圖和儀表盤的可視化支持;
二、prometheus使用docker部署
查看是否有鏡像
sudo docker search prometheus
新建prometheus.yaml
global: scrape_interval: 10s evaluation_interval: 60s scrape_configs: - job_name: prometheus static_configs: - targets: ['localhost:9090'] - job_name: integral static_configs: - targets: ['10.20.xx.xx:8001']
執(zhí)行:
docker run --name prometheus -p 9090:9090 -v ~/prometheus.yaml:/etc/prometheus/prometheus.yml prom/prometheus
進(jìn)入容器中可以看到配置文件已映射到容器指定目錄:
踩坑: prometheus官方鏡像指定的配置文件是prometheus.yml 所以映射到容器內(nèi)的文件名一定要保持一致 否則會(huì)出現(xiàn)指定的配置文件不生效
三、prometheus整體架構(gòu)及各組件
Prometheus Server :主程序,負(fù)責(zé)抓取和存儲(chǔ)時(shí)序數(shù)據(jù);
Client Libraries:客戶端庫(kù),負(fù)責(zé)檢測(cè)應(yīng)用程序代碼;
Push Gateway:Push 網(wǎng)關(guān),接收短生命周期的 Job 主動(dòng)推送的時(shí)序數(shù)據(jù);
Exporters:為不同服務(wù)定制的Exporter(如:HAProxy、StatsD、Graphite等) ,從而抓取它們的Metris指標(biāo)數(shù)據(jù);
Alert Manage:告警管理器,處理不同的告警;
四、prometheus客戶端調(diào)用示例
自定義prometheus的gin中間件
package ginprometheus import ( "strconv" "sync" "time" "github.com/gin-gonic/gin" "github.com/prometheus/client_golang/prometheus" ) const ( metricsPath = "/metrics" faviconPath = "/favicon.ico" ) var ( // httpHistogram prometheus 模型 httpHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: "http_server", Subsystem: "", Name: "requests_seconds", Help: "Histogram of response latency (seconds) of http handlers.", ConstLabels: nil, Buckets: nil, }, []string{"method", "code", "uri"}) ) // init 初始化prometheus模型 func init() { prometheus.MustRegister(httpHistogram) } // handlerPath 定義采樣路由struct type handlerPath struct { sync.Map } // get 獲取path func (hp *handlerPath) get(handler string) string { v, ok := hp.Load(handler) if !ok { return "" } return v.(string) } // set 保存path到sync.Map func (hp *handlerPath) set(ri gin.RouteInfo) { hp.Store(ri.Handler, ri.Path) } // GinPrometheus gin調(diào)用Prometheus的struct type GinPrometheus struct { engine *gin.Engine ignored map[string]bool pathMap *handlerPath updated bool } type Option func(*GinPrometheus) // Ignore 添加忽略的路徑 func Ignore(path ...string) Option { return func(gp *GinPrometheus) { for _, p := range path { gp.ignored[p] = true } } } // New new gin prometheus func New(e *gin.Engine, options ...Option) *GinPrometheus { if e == nil { return nil } gp := &GinPrometheus{ engine: e, ignored: map[string]bool{ metricsPath: true, faviconPath: true, }, pathMap: &handlerPath{}, } for _, o := range options { o(gp) } return gp } // updatePath 更新path func (gp *GinPrometheus) updatePath() { gp.updated = true for _, ri := range gp.engine.Routes() { gp.pathMap.set(ri) } } // Middleware set gin middleware func (gp *GinPrometheus) Middleware() gin.HandlerFunc { return func(c *gin.Context) { if !gp.updated { gp.updatePath() } // 過(guò)濾請(qǐng)求 if gp.ignored[c.Request.URL.String()] { c.Next() return } start := time.Now() c.Next() httpHistogram.WithLabelValues( c.Request.Method, strconv.Itoa(c.Writer.Status()), gp.pathMap.get(c.HandlerName()), ).Observe(time.Since(start).Seconds()) } }
gin路由初始化prometheus,使用中間件采樣
gp := ginprometheus.New(r) r.Use(gp.Middleware()) // metrics采樣 r.GET("/metrics", gin.WrapH(promhttp.Handler()))
查看target
選取指標(biāo)對(duì)應(yīng)的graph,這里以gc采樣的時(shí)間為例:
如果需要展示更為豐富的可視化看板,可以將prometheus與grafana結(jié)合,將prometheus數(shù)據(jù)接入到grafana中,此處不再過(guò)多闡述
到此這篇關(guān)于詳解prometheus監(jiān)控golang服務(wù)實(shí)踐記錄的文章就介紹到這了,更多相關(guān)prometheus監(jiān)控golang服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析如何利用Go的plugin機(jī)制實(shí)現(xiàn)熱更新
熱更新,或稱熱重載或動(dòng)態(tài)更新,是一種軟件更新技術(shù),允許程序在運(yùn)行時(shí),不停機(jī)更新代碼或資源,本文主要來(lái)討論下GO語(yǔ)言是否可以利用plugin機(jī)制實(shí)現(xiàn)熱更新,感興趣的可以了解下2024-04-04Golang 實(shí)現(xiàn)獲取當(dāng)前函數(shù)名稱和文件行號(hào)等操作
這篇文章主要介紹了Golang 實(shí)現(xiàn)獲取當(dāng)前函數(shù)名稱和文件行號(hào)等操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05golang?對(duì)象深拷貝的常見(jiàn)方式及性能
這篇文章主要介紹了golang?對(duì)象深拷貝的常見(jiàn)方式及性能,Go語(yǔ)言中所有賦值操作都是值傳遞,如果結(jié)構(gòu)中不含指針,則直接賦值就是深度拷貝,文章圍繞主題展開(kāi)更多相關(guān)資料,需要的小伙伴可以參考一下2022-06-06Golang中urlencode與urldecode編碼解碼詳解
這篇文章主要給大家介紹了關(guān)于Golang中urlencode與urldecode編碼解碼的相關(guān)資料,在Go語(yǔ)言中轉(zhuǎn)碼操作非常方便,可以使用內(nèi)置的encoding包來(lái)快速完成轉(zhuǎn)碼操作,Go語(yǔ)言中的encoding包提供了許多常用的編碼解碼方式,需要的朋友可以參考下2023-09-09jenkins配置golang?代碼工程自動(dòng)發(fā)布的實(shí)現(xiàn)方法
這篇文章主要介紹了jenkins配置golang?代碼工程自動(dòng)發(fā)布,jks是個(gè)很好的工具,使用方法也很多,我只用了它簡(jiǎn)單的功能,對(duì)jenkins配置golang相關(guān)知識(shí)感興趣的朋友一起看看吧2022-07-07Go接口構(gòu)建可擴(kuò)展Go應(yīng)用示例詳解
本文深入探討了Go語(yǔ)言中接口的概念和實(shí)際應(yīng)用場(chǎng)景。從基礎(chǔ)知識(shí)如接口的定義和實(shí)現(xiàn),到更復(fù)雜的實(shí)戰(zhàn)應(yīng)用如解耦與抽象、多態(tài)、錯(cuò)誤處理、插件架構(gòu)以及資源管理,文章通過(guò)豐富的代碼示例和詳細(xì)的解釋,展示了Go接口在軟件開(kāi)發(fā)中的強(qiáng)大功能和靈活性2023-10-10