prometheus + grafana配置统计图
配置普通折线图
在代码中添加metrics
例如我们想要展示耗时的统计图,需要在代码中记录开始处理和结束处理的时间点,得出时间差
start := time.Now()
resp, err := cli.DoSth(ctx, req)
gap := time.Since(start)
log.Infof("key:%+v, do sth cost:%+v", sha1, gap.Nanoseconds()/1000000)
DoSthSummary.With(prometheus.Labels{"target": "do_sth"}).Observe(float64(gap))
if err != nil {
return nil, err
}
metrics部分的代码:
DoSthSummary = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "do_sth_cost_summary",
Help: "do sth request cost time summary. unit: ns",
Objectives: map[float64]float64{0.5: 0.05, 0.75: 0.05, 0.9: 0.01, 0.99: 0.001},
MaxAge: time.Minute,
}, []string{"target"})
func init() {
prometheus