prometheus直方图实践

目录

1.简介

2.方案


1.简介

Prometheus提供了Counter、Gauge、Histogram、Summary四类指标(详见Metric types | Prometheus),可以通过"github.com/prometheus/client_golang/prometheus"自定义采集指标、注册、采集数据、发布URL,以上步骤即构成exporter,发布后创建Service、ServiceMonitor即可。

复制代码
 

2.部署介绍

定义指标

定义指标,如下定义为直方图类型,需要注意的是Buckets需按照实际要求调整参数,示例中Buckets: prometheus.LinearBuckets(0, 1, 4), 表示从0开始统计,桶宽为1,统计4个桶,实际包含6个桶,即0~1,1~2,2~3,3~4,>4;

复制代码
var (
    PodCpuHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
			Name: "PodCpuHistogram",
			Help: "pod_cpu_histogram ",
			ConstLabels: map[string]string{
				"metric":          "PodCpuHistogram",
				"namespace":       pod.GetNamespace(),
				"pod_name":        pod.GetName(),
			},
			Buckets: prometheus.LinearBuckets(0, 1, 5),
		})
)

注册

将定义的指标注册到prometheus

复制代码
prometheus.MustRegister(PodCpuHistogram)

统计指标

拿到实时数据(示例中data即为拿到的pod CPU实时数据)后通过PodCpuHistogram.Observe方法进行统计;

复制代码
PodCpuHistogram.Observe(data)

发布metrics地址

复制代码
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080",nil))

exporter构建示例可参考https://www.cnblogs.com/makelu/p/11082485.html

exporter构建好后可以进行部署,创建对应Service和ServiceMonitor。

ServiceMonitor中可以定义采集周期、metrics地址和端口。

  • 验证ServiceMonitor

登录p8s,验证p8s是否成功进行服务发现。

检查targets页面是否有对应endpoint,在p8s查询窗口输入PodCpuHistogram检查是否有匹配项。

相关推荐
云游19 小时前
大模型性能指标的监控系统(prometheus3.5.0)和可视化工具(grafana12.1.0)基础篇
grafana·prometheus·可视化·监控
qq_232045572 天前
非容器方式安装Prometheus和Grafana,以及nginx配置访问Grafana
nginx·grafana·prometheus
夜莺云原生监控2 天前
Prometheus 监控 Kubernetes Cluster 最新极简教程
容器·kubernetes·prometheus
SRETalk3 天前
Prometheus 监控 Kubernetes Cluster 最新极简教程
kubernetes·prometheus
川石课堂软件测试3 天前
JMeter并发测试与多进程测试
功能测试·jmeter·docker·容器·kubernetes·单元测试·prometheus
SRETalk4 天前
夜莺监控的几种架构模式详解
prometheus·victoriametrics·nightingale·夜莺监控
Ditglu.4 天前
使用Prometheus + Grafana + node_exporter实现Linux服务器性能监控
服务器·grafana·prometheus
SRETalk5 天前
监控系统如何选型:Zabbix vs Prometheus
zabbix·prometheus
睡觉z5 天前
云原生环境Prometheus企业级监控
云原生·prometheus
归梧谣5 天前
云原生环境 Prometheus 企业级监控实战
云原生·prometheus