例如咱們想要展現耗時的統計圖,須要在代碼中記錄開始處理和結束處理的時間點,得出時間差學習
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
大功告成,以後有時間再系統學習從零開始的部署以及各類高級語句。部署