grafana 分位规则_prometheus + grafana配置统计图

本文介绍了如何使用Prometheus记录耗时指标并配合Grafana展示统计图。通过在代码中添加metrics,如DoSthSummary,配置Objectives以获取分位数信息。在Grafana中,连接Prometheus数据源,选择对应的metrics,并配置面板,包括图例、坐标轴、横坐标以及标题和描述。同时,展示了如何配置按小时划分的增量直方图,通过increase函数和时间范围实现小时级聚合。
摘要由CSDN通过智能技术生成

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.MustRegister(DoSthSummary)

}

至此,代码中的metrics添加完毕了

在grafana中添加prometheus数据源

添加新的panel

选择prometheus数据源

选择metrics

使用代码中summary的Name字段即可

注意,target一定要与代码中的target一致

配置图例的显式方式 host-分位数

配置坐标轴

Left Y指的是最左侧纵坐标的相关配置

Right Y指的是最右侧纵坐标的相关配置

X-Axis指的是横坐标的相关配置

配置最右侧的图例

最后配置标题,描述等信息

配置按小时划分的增量图

代码中添加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,否则不会按小时聚合,会默认按分钟聚合

配置图形为直方图

成功

大功告成,之后有时间再系统学习从零开始的部署以及各种高级语句。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值