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

相关推荐
chaodaibing3 天前
elasticsearch_exporter启动报错 failed to fetch and decode node stats
elasticsearch·prometheus
陌殇殇殇4 天前
Prometheus监控MySQL主从数据库
运维·数据库·mysql·prometheus
福大大架构师每日一题4 天前
19.1 使用k8s的sdk编写一个项目获取pod和node信息
云原生·容器·kubernetes·prometheus
福大大架构师每日一题4 天前
19.3 打镜像部署到k8s中,prometheus配置采集并在grafana看图
kubernetes·grafana·prometheus
福大大架构师每日一题5 天前
21.2 k8s中etcd的tls双向认证原理解析
容器·kubernetes·prometheus·etcd
我的运维人生5 天前
基于Prometheus和Grafana的现代服务器监控体系构建
服务器·运维开发·grafana·prometheus·技术共享
BUG弄潮儿5 天前
k8s 部署 prometheus
容器·kubernetes·prometheus
iQM756 天前
基于Prometheus和Grafana的现代服务器监控体系构建
服务器·grafana·prometheus
kayotin7 天前
使用Prometheus进行系统监控,包括Mysql、Redis,并使用Grafana图形化表示
redis·mysql·prometheus
人类群星闪耀时9 天前
监控和日志管理:深入了解Nagios、Zabbix和Prometheus
zabbix·prometheus