prometheus + grafana配置統計圖

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部分的代碼: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

在grafana中添加prometheus數據源

  • 添加新的panel

image.png

image.png

  • 選擇prometheus數據源

image.png

  • 選擇metrics

使用代碼中summary的Name字段便可
image.png
注意,target必定要與代碼中的target一致code

  • 配置圖例的顯式方式 host-分位數

image.png

image.png

  • 配置座標軸

Left Y指的是最左側縱座標的相關配置
Right Y指的是最右側縱座標的相關配置
X-Axis指的是橫座標的相關配置
image.pngblog

  • 配置最右側的圖例

image.png

image.png

  • 最後配置標題,描述等信息

image.png

  • 配置按小時劃分的增量圖

  • 代碼中添加metrics:
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"},
    )
  • 統計zip每小時增量的語句:
sum(increase(size_counter{sample_type="zip"}[1h]))

注意,Min step必定要填1h,不然不會按小時聚合,會默認按分鐘聚合ip

image.png

  • 配置圖形爲直方圖

image.png

  • 成功

image.png

大功告成,以後有時間再系統學習從零開始的部署以及各類高級語句。部署

相關文章
相關標籤/搜索