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检查是否有匹配项。

相关推荐
鼠鼠我捏,要死了捏11 小时前
大规模集群下 Prometheus 监控架构实战经验分享
prometheus·monitoring·devops
山岚的运维笔记11 小时前
AlpineLinux使用docker部署prometheus
docker·容器·prometheus
DaxiaLeeSuper1 天前
Prometheus+Grafana+node_exporter监控linux服务器资源的方案
linux·grafana·prometheus
core5122 天前
Grafana接入Prometheus实战
grafana·prometheus
奈斯ing3 天前
【prometheus+Grafana篇】PromQL核心函数解析:rate()与irate()函数详解
运维·grafana·prometheus
lingRJ7774 天前
从混沌到掌控:基于OpenTelemetry与Prometheus构建分布式调用链监控告警体系
java·springboot·prometheus·backend·opentelemetry·jaeger·microservices
码上淘金6 天前
【Prometheus 】通过 Pushgateway 上报指标数据
prometheus
JAVA拾贝8 天前
Prometheus+Grafana运维监控并实现钉钉告警
运维·钉钉·grafana·prometheus·运维监控
TDengine (老段)21 天前
使用 Prometheus 访问 TDengine ---
大数据·数据库·prometheus·时序数据库·iot·tdengine·涛思数据
枫桥听月21 天前
16.大数据监控
大数据·prometheus