您现在的位置是:主页 > news > 论坛建立网站/大学生网页设计作业

论坛建立网站/大学生网页设计作业

admin2025/5/25 9:53:07news

简介论坛建立网站,大学生网页设计作业,国家公示网营业执照,域名地址prometheus grafana配置统计图配置普通折线图在代码中添加metrics例如我们想要展示耗时的统计图,需要在代码中记录开始处理和结束处理的时间点,得出时间差start : time.Now()resp, err : cli.DoSth(ctx, req)gap : time.Since(start)log.Infof("ke…

论坛建立网站,大学生网页设计作业,国家公示网营业执照,域名地址prometheus grafana配置统计图配置普通折线图在代码中添加metrics例如我们想要展示耗时的统计图,需要在代码中记录开始处理和结束处理的时间点,得出时间差start : time.Now()resp, err : cli.DoSth(ctx, req)gap : time.Since(start)log.Infof("ke…

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

配置图形为直方图

成功

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