最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文帶你玩轉(zhuǎn)Golang Prometheus Eexporter開(kāi)發(fā)

 更新時(shí)間:2023年02月16日 14:19:44   作者:TtrOps  
本文分兩大塊,一是搞清楚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)景和方式,感興趣的可以了解一下

本篇內(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ì)

    這篇文章主要為大家介紹了GoFrame框架gset對(duì)比PHP?Java?Redis的使用優(yōu)勢(shì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 一文詳解如何使用Go語(yǔ)言生成二維碼

    一文詳解如何使用Go語(yǔ)言生成二維碼

    使用Go語(yǔ)言編程時(shí),生成任意內(nèi)容的二維碼是非常方便的,下面這篇文章主要給大家介紹了關(guān)于如何使用Go語(yǔ)言生成二維碼的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • Golang時(shí)間及時(shí)間戳的獲取轉(zhuǎn)換超全面詳細(xì)講解

    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ù)

    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 Comparable Type原理深入解析

    Go Comparable Type原理深入解析

    這篇文章主要為大家介紹了Go Comparable Type原理深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Go語(yǔ)言多人聊天室項(xiàng)目實(shí)戰(zhàn)

    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)事件管理器

    基于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無(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
  • Go目錄文件路徑操作的實(shí)現(xiàn)

    Go目錄文件路徑操作的實(shí)現(xiàn)

    在Go語(yǔ)言中,可以使用絕對(duì)路徑或相對(duì)路徑來(lái)表示文件路徑,本文就來(lái)介紹一下Go目錄文件路徑操作,感興趣的可以了解一下
    2023-10-10
  • Golang無(wú)限緩存channel的設(shè)計(jì)與實(shí)現(xiàn)解析

    Golang無(wú)限緩存channel的設(shè)計(jì)與實(shí)現(xiàn)解析

    這篇文章主要為大家介紹了Golang無(wú)限緩存channel的設(shè)計(jì)與實(shí)現(xiàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評(píng)論

揭东县| 远安县| 九龙坡区| 朝阳区| 凤台县| 武夷山市| 蕲春县| 全州县| 永顺县| 濉溪县| 阜城县| 乐亭县| 西乌珠穆沁旗| 民权县| 青州市| 丹棱县| 华安县| 融水| 石柱| 合肥市| 秦皇岛市| 松阳县| 堆龙德庆县| 元氏县| 建始县| 故城县| 广丰县| 库尔勒市| 弥渡县| 磴口县| 名山县| 抚顺县| 荣昌县| 永宁县| 上饶县| 清远市| 仁怀市| 沙雅县| 彰武县| 哈尔滨市| 扎囊县|