Prometheus Go client library 詳解
介紹
Prometheus 支持 4 種 指標(biāo)類型,分別是 Counter、Gauge、Histogram 和 Summary。
- Counter 指標(biāo)類型,指標(biāo)值是只能遞增,不能遞減的數(shù)值。需要注意的是,當(dāng) Prometheus server 重啟時,指標(biāo)值會被重置為 0。該指標(biāo)類型可用于統(tǒng)計接口的請求數(shù)、錯誤數(shù)等使用場景。
- Gauge 指標(biāo)類型,指標(biāo)值是可增可減的數(shù)值。該指標(biāo)類型可用于統(tǒng)計 CPU、內(nèi)存和硬盤的使用情況,goroutine 的數(shù)量等使用場景。
- Histogram 指標(biāo)類型,指標(biāo)值基于桶分布。開發(fā)者可以自定義桶的區(qū)間。該指標(biāo)類型可用于統(tǒng)計接口的延時請求數(shù)等使用場景。
- Summary 指標(biāo)類型,與 Histogram 類似,區(qū)別是 Histogram 直接統(tǒng)計了不同區(qū)間中的指標(biāo)數(shù)值,而 Summary 是基于客戶端級別,因此不能統(tǒng)計多個實例的聚合數(shù)據(jù)。該指標(biāo)類型可用于預(yù)先不知道指標(biāo)桶劃分區(qū)間的場景。
使用方式
一般在實際應(yīng)用場景中,通常一個指標(biāo)需要對應(yīng)多條時序數(shù)據(jù)(Label Name 為維度),此時就需要使用支持標(biāo)簽的指標(biāo)類型。
Prometheus 有 4 種支持標(biāo)簽的指標(biāo)類型,分別是 ConterVec、GaugeVec、HistogramVec、SummaryVec。
1.CounterVec
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)
2.GaugeVec
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)建實例。
3.HistogramVec
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,
)
)
4.SummaryVec
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)建實例。
總結(jié)
本文我們主要介紹 4 種指標(biāo)類型的含義,通過 Label 可以將 4 種類型的指標(biāo)數(shù)據(jù),按照 Label 的維度分組統(tǒng)計,我們以支持 Label 的 CounterVec 為例,介紹了它的使用方式,其余 3 種支持 Label 的指標(biāo)也提供了簡單的使用示例。