掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
Prometheus 支持 4 種 指標(biāo)類型,分別是 Counter、Gauge、Histogram 和 Summary。

成都創(chuàng)新互聯(lián)成立于2013年,我們提供高端網(wǎng)站建設(shè)、重慶網(wǎng)站制作、成都網(wǎng)站設(shè)計、網(wǎng)站定制、成都全網(wǎng)營銷、小程序開發(fā)、微信公眾號開發(fā)、seo優(yōu)化排名服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計、程序開發(fā)來完成項目落地,為柔性防護網(wǎng)企業(yè)提供源源不斷的流量和訂單咨詢。
一般在實際應(yīng)用場景中,通常一個指標(biāo)需要對應(yīng)多條時序數(shù)據(jù)(Label Name 為維度),此時就需要使用支持標(biāo)簽的指標(biāo)類型。
Prometheus 有 4 種支持標(biāo)簽的指標(biāo)類型,分別是 ConterVec、GaugeVec、HistogramVec、SummaryVec。
CounterVec 與 Counter 的區(qū)別是,它支持 Label,我們可以按照 Lable 維度,將同一個指標(biāo)的數(shù)據(jù)按照 Lable 分組統(tǒng)計。例如,同一個 Api 接口的請求數(shù),我們可以定義 Lable (Code、Method),按照狀態(tài)碼和 HTTP 請求方式,分組統(tǒng)計同一個 Api 接口的請求數(shù)。
示例代碼:
var (
// 標(biāo)簽名
labelNames = []string{"host", "code", "path", "method"}
// HttpReqs 實例化 CounterVec
HttpReqs *prometheus.CounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "http_requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
},
labelNames,
)
)閱讀上面這段代碼,我們使用 NewCounterVec 創(chuàng)建一個實例,它支持多個方法,我們可以使用其中一個性能相對較高的方法 WithLabelValues,返回一個 Counter。
示例代碼:
func Metrics() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
host := c.RemoteIP()
code := fmt.Sprintf("%d", c.Writer.Status())
method := c.Request.Method
labelsByHttpReqs := []string{host, code, c.FullPath(), method}
prometheus_metrics.HttpReqs.WithLabelValues(labelsByHttpReqs...).Inc()
}
}Counter 支持兩個方法,分別是 Inc() 和 Add(),其中 Inc() 將 Counter 增加 1,Add() 將 Counter 增加給定值,需要注意的是,給定值必須為非負(fù)值,否則會引發(fā) panic。
需要注意的是,在我們創(chuàng)建指標(biāo)之后,還需要使用 Register() 接口的 Register() 方法,注冊之后才可以被收集到指標(biāo)數(shù)據(jù)。如果需要注冊多個指標(biāo),可以使用 MustRegister() 方法。
示例代碼:
reg := prometheus.NewRegistry()
reg.MustRegister(prometheus_metrics.HttpReqs, prometheus_metrics.OpsQueued, prometheus_metrics.Latencies, prometheus_metrics.Temps)
GaugeVec 與 Gauge 的區(qū)別是,它支持 Label,我們可以按照 Lable 維度,將同一個指標(biāo)的數(shù)據(jù)按照 Lable 分組統(tǒng)計。
示例代碼:
var (
labelNamesByOpsQueued = []string{
"user",
"type",
}
OpsQueued = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ops_queued",
Help: "Number of blob storage operations waiting to be processed, partitioned by user and type.",
},
labelNamesByOpsQueued,
)
)閱讀上面這段代碼,我們使用 NewGaugeVec 創(chuàng)建實例。
HistogramVec 與 Histogram 的區(qū)別是,它支持 Label,我們可以按照 Lable 維度,將同一個指標(biāo)的數(shù)據(jù)按照 Lable 分組統(tǒng)計。
示例代碼:
var (
labelNamesByLatencies = []string{"method", "code"}
Latencies = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "Tracks the latencies for HTTP requests.",
Buckets: []float64{0.99, 0.9, 0.5},
},
labelNamesByLatencies,
)
)
SummaryVec 與 Summary 的區(qū)別是,它支持 Label,我們可以按照 Lable 維度,將同一個指標(biāo)的數(shù)據(jù)按照 Lable 分組統(tǒng)計。
示例代碼:
var (
labelNamesByTemps = []string{"species"}
Temps = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "pond_temperature_celsius",
Help: "The temperature of the frog pond.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
labelNamesByTemps,
)
)閱讀上面這段代碼,使用 NewSummaryVec 創(chuàng)建實例。
本文我們主要介紹 4 種指標(biāo)類型的含義,通過 Label 可以將 4 種類型的指標(biāo)數(shù)據(jù),按照 Label 的維度分組統(tǒng)計,我們以支持 Label 的 CounterVec 為例,介紹了它的使用方式,其余 3 種支持 Label 的指標(biāo)也提供了簡單的使用示例。

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流