prometheus一些学习
每分钟请求与响应数
# 定义数据类型
// 请求数统计
var ReqCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "fwd_req",
Name: "request_counter",
Help: "Total number of FWD_REQ",
},
[]string{
"ip",
})
// 响应数统计
var RespCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "fwd_res",
Name: "response_counter",
Help: "Total number of FWD_RES",
},
[]string{
"ip",
})
// 请求最大时间统计
var ReqTimeCost = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Subsystem: "fwd_req",
Name: "time_cost_gauge",
Help: "Requests handling time cost.",
}, func() float64 {
v := ReqTimeCostVar.GetMaxValue()
ReqTimeCostVar.ResetToZero()
return v
},
)
var ReqTimeCostVar = MaxValue{}
// 响应最大时间统计
var RespTimeCost = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Subsystem: "fwd_res",
Name: "time_cost_gauge",
Help: "responses handling time cost.",
}, func() float64 {
v := RespTimeCostVar.GetMaxValue()
RespTimeCostVar.ResetToZero()
return v
},
)
var RespTimeCostVar = MaxValue{}
// 请求最大时间分布统计
var ReqTimeCostHistogram = prometheus.NewHistogram(
prometheus.HistogramOpts{
Subsystem: "fwd_req",
Name: "time_cost_histogram",
Help: "A histogram of the request durations in microsecond.",
Buckets: []float64{0.020, 0.030, 0.040, 0.050, 0.060, 0.1, 0.2, 1, 10, 100},
},
)
// 响应最大时间分布统计
var RespTimeCostHistogram = prometheus.NewHistogram(
prometheus.HistogramOpts{
Subsystem: "fwd_res",
Name: "time_cost_histogram",
Help: "A histogram of the responses durations in microsecond.",
Buckets: []float64{0.020, 0.030, 0.040, 0.050, 0.060, 0.1, 0.2, 1, 10, 100},
},
)
# 注册数据
prometheus.MustRegister(ReqCounter)
prometheus.MustRegister(RespCounter)
prometheus.MustRegister(ReqTimeCost)
prometheus.MustRegister(RespTimeCost)
prometheus.MustRegister(ReqTimeCostHistogram)
prometheus.MustRegister(RespTimeCostHistogram)
// 请求数统计
ReqCounter.With(
prom.Labels{
"ip": ip,
},
).Inc()
fTimeCost, _ := strconv.ParseFloat(timeCost, 64)
// 请求最大时间统计
ReqTimeCostVar.TrySetMaxValue(fTimeCost)
// 请求最大时间分布统计
ReqTimeCostHistogram.Observe(fTimeCost)
// 响应数统计
RespCounter.With(
prom.Labels{
"ip": ip,
},
).Inc()
fTimeCost, _ := strconv.ParseFloat(timeCost, 64)
// 响应最大时间统计
RespTimeCostVar.TrySetMaxValue(fTimeCost)
// 响应最大时间分布统计
RespTimeCostHistogram.Observe(fTimeCost)
# 数据定义
type MaxValue struct {
atomic.Float64
}
func (m *MaxValue) GetMaxValue() float64 {
return m.Load()
}
func (m *MaxValue) TrySetMaxValue(v float64) {
for {
current := m.Load()
if v > current {
ok := m.CompareAndSwap(current, v)
if ok {
return
}
} else {
return
}
}
}
func (m *MaxValue) ResetToZero() {
m.Store(0)
return
}
prometheus与grafana
# 每分钟请求/响应数
sum(increase(fwd_req_request_counter[1m]))
sum(increase(fwd_res_response_counter[1m]))
# qps
sum(irate(fwd_req_request_counter{}[1m]))
sum(irate(fwd_res_response_counter{}[1m]))
# 请求P9x图
histogram_quantile(0.90, sum(rate(fwd_req_time_cost_histogram_bucket[1m])) by (le))
histogram_quantile(0.95, sum(rate(fwd_req_time_cost_histogram_bucket[1m])) by (le))
histogram_quantile(0.99, sum(rate(fwd_req_time_cost_histogram_bucket[1m])) by (le))
# 响应P9x图
histogram_quantile(0.90, sum(rate(fwd_res_time_cost_histogram_bucket[1m])) by (le))
histogram_quantile(0.95, sum(rate(fwd_res_time_cost_histogram_bucket[1m])) by (le))
histogram_quantile(0.99, sum(rate(fwd_res_time_cost_histogram_bucket[1m])) by (le))
# 请求最大耗时
sum(max_over_time(fwd_req_time_cost_gauge[15s]))
# 响应最大耗时
sum(max_over_time(fwd_res_time_cost_gauge[15s]))
# 每分钟ip数
count(increase(fwd_req_request_counter[1m])>0)
count(increase(fwd_res_response_counter[1m])>0)
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: