Prometheus Exportor向Collector接口传递Request对象

本文介绍了一种基于Prometheus的集群资源监控实现方案,包括内存使用情况和OOM崩溃次数的收集与描述。通过定制化的ClusterManager结构体,文章详细阐述了如何设置外部参数、收集并报告集群状态评估结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

type ClusterManager struct {
	Zone         string
	OOMCountDesc *prometheus.Desc
	RAMUsageDesc *prometheus.Desc

	// request
	req chan *http.Request
}

// Describe simply sends the two Descs in the struct to the channel.
func (c *ClusterManager) Describe(ch chan<- *prometheus.Desc) {
	log.Println("describe")

	ch <- c.OOMCountDesc
	ch <- c.RAMUsageDesc
}

func (c *ClusterManager) SetExParameter(req *http.Request) {
	c.req <- req
}

func (c *ClusterManager) Collect(ch chan<- prometheus.Metric) {
	req := <-c.req

	oomCountByHost, ramUsageByHost := c.ReallyExpensiveAssessmentOfTheSystemState()
	for host, oomCount := range oomCountByHost {
		ch <- prometheus.MustNewConstMetric(
			c.OOMCountDesc,
			prometheus.CounterValue,
			float64(oomCount),
			host,
		)
	}
	for host, ramUsage := range ramUsageByHost {
		ch <- prometheus.MustNewConstMetric(
			c.RAMUsageDesc,
			prometheus.GaugeValue,
			ramUsage,
			host,
		)
	}

}

func NewClusterManager(zone string) *ClusterManager {
	return &ClusterManager{
		Zone: zone,
		OOMCountDesc: prometheus.NewDesc(
			"clustermanager_oom_crashes_total",
			"Number of OOM crashes.",
			[]string{"host"},
			prometheus.Labels{"zone": zone},
		),
		RAMUsageDesc: prometheus.NewDesc(
			"clustermanager_ram_usage_bytes",
			"RAM usage as reported to the cluster manager.",
			[]string{"host"},
			prometheus.Labels{"zone": zone},
		),
		req: make(chan int, 256),
	}
}

type Registry struct {
	*prometheus.Registry
	collectors []ParameterSetter
}

func NewRegistry() *Registry {
	r := prometheus.NewRegistry()
	return &Registry{Registry: r}
}

func (r *Registry) SetExParameter(req *http.Request) {
	for _, c := range r.collectors {
		c.SetExParameter(req)
	}
}

func (r *Registry) MustRegister(vs ...interface{}) {
	for _, v := range vs {
		if c, ok := v.(ParameterSetter); ok {
			r.collectors = append(r.collectors , c)
		}
		if c, ok := v.(prometheus.Collector); ok {
			r.Registry.MustRegister(c)
		}
	}
}

type ParameterSetter interface {
	SetExParameter(req *http.Request)
}

type Gatherer interface {
	prometheus.Gatherer
	ParameterSetter
}

func HandlerFor(reg Gatherer, opts promhttp.HandlerOpts) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		reg.SetExParameter(req)

		promhttp.HandlerFor(reg, opts).ServeHTTP(w, req)
	})
}


func main() {
	workerDB := NewClusterManager("db")
	workerCA := NewClusterManager("ca")

	// Since we are dealing with custom Collector implementations, it might
	// be a good idea to try it out with a pedantic registry.
	reg := NewPedanticRegistry()
	reg.MustRegister(workerDB)
	reg.MustRegister(workerCA)

	// Expose the registered metrics via HTTP.
	http.Handle("/metrics", HandlerFor(reg, promhttp.HandlerOpts{}))
	http.ListenAndServe(":8080", nil)
}

 

转载于:https://my.oschina.net/3cwYg4/blog/797002

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值