例如咱们想要展现耗时的统计图,须要在代码中记录开始处理和结束处理的时间点,得出时间差学习
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部分的代码:spa
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.MustRegister(DoSthSummary) }
至此,代码中的metrics添加完毕了3d
使用代码中summary的Name字段便可
注意,target必定要与代码中的target一致code
Left Y指的是最左侧纵坐标的相关配置
Right Y指的是最右侧纵坐标的相关配置
X-Axis指的是横坐标的相关配置blog
common.SampleSizeCounter.With(prometheus.Labels{"sample_type": "raw"}).Add(float64(intSize) / 1024) common.SampleSizeCounter.With(prometheus.Labels{"sample_type": "zip"}).Add(float64(intZipSize) / 1024)
SampleSizeCounter = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "size_counter", Help: "sample size total.(KB)", }, []string{"sample_type"}, )
sum(increase(size_counter{sample_type="zip"}[1h]))
注意,Min step必定要填1h,不然不会按小时聚合,会默认按分钟聚合ip
大功告成,以后有时间再系统学习从零开始的部署以及各类高级语句。部署