本篇内容有点长,代码有点多。有趣味的能够保持看上来,并入手实际,没趣味的能够划走。本文分两大块,一是搞清楚 prometheus 四种类型的指标 Counter,Gauge,Histogram,Summary 用 golang 语言如何结构这 4 种类型对应的指标,二是搞清楚批改指标值的场景和形式。
指标类型 | 类别 | 形容 | 可利用场景 |
---|---|---|---|
Counter | 计数类 | 应用在累计指标枯燥递增或递加状况下,只能在指标重启后主动归零 | 服务申请解决数量、已实现工作数量、谬误数量 |
Guage | 测量类 | 应用可增可减的的数据状况下 | 以后内存 /CPU 应用状况、并发申请数量 |
Histogram | 直方图类 | 应用统计指标信息在不同区间内的统计数量 | 延迟时间、响应大小。例如:0- 1 秒内的延迟时间、、0- 5 秒内的延迟时间、例如 0 -1kb 之内的响应大小、0-5kb 之内的响应大小 |
Summary | 摘要类 | 相似于直方图,在客户端对百分位进行统计 | 延迟时间、响应大小。例如:超过百分之多少的人要满足需要的话,须要多长时间实现。 |
1. Gauge 指标类型
1.1 不带 label 的根本例子
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 定义指标
cpuUsage := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cpu_usage", // 指标名称
Help: "this is test metrics cpu usage", // 帮忙信息
})
// 给指标设置值
cpuUsage.Set(89.56)
// 注册指标
prometheus.MustRegister(cpuUsage)
// 裸露指标
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9900", nil)
}
1.2 带有固定 label 指标的例子
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 定义指标
cpuUsage := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cpu_usage", // 指标名称
Help: "this is test metrics cpu usage", // 帮忙信息
ConstLabels: prometheus.Labels{"MachineType": "host"}, // label
})
// 给指标设置值
cpuUsage.Set(89.56)
// 注册指标
prometheus.MustRegister(cpuUsage)
// 裸露指标
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9900", nil)
}
1.3 带有非固定 label 指标的例子
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 定义带有不固定 label 的指标
mtu := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "interface_mtu",
Help: "网卡接口 MTU",
}, []string{"interface", "Machinetype"})
// 给指标设置值
mtu.WithLabelValues("lo", "host").Set(1500)
mtu.WithLabelValues("ens32", "host").Set(1500)
mtu.WithLabelValues("eth0", "host").Set(1500)
// 注册指标
prometheus.MustRegister(mtu)
// 裸露指标
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9900", nil)
}
2. Counter 指标类型
2.1 不带 label 的根本例子
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 定义指标
reqTotal := prometheus.NewCounter(prometheus.CounterOpts{
Name: "current_request_total",
Help: "以后申请总数",
})
// 注册指标
prometheus.MustRegister(reqTotal)
// 设置值
reqTotal.Add(10)
// 裸露指标
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9900", nil)
}
2.2 带有固定 label 指标的例子
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 定义指标
suceReqTotal := prometheus.NewCounter(prometheus.CounterOpts{
Name: "sucess_request_total",
Help: "申请胜利的总数",
ConstLabels: prometheus.Labels{"StatusCode": "200"},
})
// 注册指标
prometheus.MustRegister(suceReqTotal)
// 设置值
suceReqTotal.Add(5675)
// 裸露指标
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9900", nil)
}
2.3 带有非固定 label 指标的例子
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 定义指标
pathReqTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "path_request_total",
Help: "path 申请总数",
}, []string{"path"})
// 注册指标
prometheus.MustRegister(pathReqTotal)
// 设置值
pathReqTotal.WithLabelValues("/token").Add(37)
pathReqTotal.WithLabelValues("/auth").Add(23)
pathReqTotal.WithLabelValues("/user").Add(90)
pathReqTotal.WithLabelValues("/api").Add(67)
// 裸露指标
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9900", nil)
}
3. Histogram 指标类型
3.1 不带 label 的根本例子
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 定义指标
reqDelay := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "request_delay",
Help: "申请提早,单位秒",
Buckets: prometheus.LinearBuckets(0, 3, 6), // 调用 LinearBuckets 生成区间,从 0 开始,宽度 3,共 6 个 Bucket
})
// 注册指标
prometheus.MustRegister(reqDelay)
// 设置值
reqDelay.Observe(6)
// 裸露指标
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() {
// 定义指标
reqDelay := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "request_delay",
Help: "申请提早,单位秒",
Buckets: prometheus.LinearBuckets(0, 3, 6), // 调用 LinearBuckets 生成区间,从 0 开始,宽度 3,共 6 个 Bucket
ConstLabels: prometheus.Labels{"path": "/api"},
})
// 注册指标
prometheus.MustRegister(reqDelay)
// 设置值
reqDelay.Observe(6)
// 裸露指标
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() {
// 定义指标
reqDelay := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "request_delay",
Help: "申请提早,单位秒",
Buckets: prometheus.LinearBuckets(0, 3, 6), // 调用 LinearBuckets 生成区间,从 0 开始,宽度 3,共 6 个 Bucket
}, []string{"path"})
// 注册指标
prometheus.MustRegister(reqDelay)
// 设置值
reqDelay.WithLabelValues("/api").Observe(6)
reqDelay.WithLabelValues("/user").Observe(3)
reqDelay.WithLabelValues("/delete").Observe(2)
reqDelay.WithLabelValues("/get_token").Observe(13)
// 裸露指标
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9900", nil)
}
4. Summary 指标类型
4.1 不带 label 的例子
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 定义指标
reqDelay := prometheus.NewSummary(prometheus.SummaryOpts{
Name: "request_delay",
Help: "申请提早",
Objectives: map[float64]float64{0.5: 0.05, 0.90: 0.01, 0.99: 0.001}, // 百分比: 精度
})
// 注册指标
prometheus.MustRegister(reqDelay)
// 设置值
reqDelay.Observe(4)
// 裸露指标
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() {
// 定义指标
reqDelay := prometheus.NewSummary(prometheus.SummaryOpts{
Name: "request_delay",
Help: "申请提早",
Objectives: map[float64]float64{0.5: 0.05, 0.90: 0.01, 0.99: 0.001}, // 百分比: 精度
ConstLabels: prometheus.Labels{"path": "/api"},
})
// 注册指标
prometheus.MustRegister(reqDelay)
// 设置值
reqDelay.Observe(4)
// 裸露指标
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() {
// 定义指标
reqDelay := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "request_delay",
Help: "申请提早",
Objectives: map[float64]float64{0.5: 0.05, 0.90: 0.01, 0.99: 0.001}, // 百分比: 精度
}, []string{"path"})
// 注册指标
prometheus.MustRegister(reqDelay)
// 设置值
reqDelay.WithLabelValues("/api").Observe(4)
reqDelay.WithLabelValues("/token").Observe(2)
reqDelay.WithLabelValues("/user").Observe(3)
// 裸露指标
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9900", nil)
}
5. 值的批改
5.1 基于事件的触发来批改值,比方每拜访 1 次 /api 就增 1
基于事件的触发对指标的值进行批改,通常大多数是来自业务方面的指标需要,如自研的利用须要裸露相干指标给 promethus 进行监控、展现,那么指标采集的代码(指标定义、设置值)就要嵌入到自研利用代码里了。
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 申请累计 单位 次",
}, []string{"path"})
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {urlRequestTotal.WithLabelValues(r.URL.Path).Inc() // 应用 Inc 函数进行增 1
fmt.Fprintf(w, "Welcome to the api")
})
prometheus.MustRegister(urlRequestTotal)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9900", nil)
}
5.2 基于工夫周期的触发来批改值,比方上面的示例中,是每距离 1 秒获取 cpu 负载指标
基于工夫周期的触发,能够是多少秒、分、时、日、月、周。
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 负载",
}, []string{"time"})
// 开启一个子协程执行该匿名函数内的逻辑来给指标设置值,且每秒获取一次
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 基于每拜访一次获取指标的 URI 才批改值,比方每次拜访 /metrics 才去批改某些指标的值
上面的这个示例是,每拜访一次 /metrics 就获取一次内存总容量
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: "内存总容量 单位 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 的指标类型有对应的函数,别离是 NewGaugeFunc 和 NewCounterFunc,而且是固定的 label。
本文转载于(喜爱的盆友关注咱们):https://mp.weixin.qq.com/s/zb…