prometheus 自己实现exporter

1.  看源代码可以发现Collector 接口定义了两个方法,实现这两个方法就可以暴露数据了。

 

 

2. 示列代码

package main

import (
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/client_golang/prometheus"
)

type fooCollector struct {
fooMetric *prometheus.Desc
barMetric *prometheus.Desc
}


func newFooCollector() *fooCollector {
m1 := make(map[string]string)
m1["env"] = "prod"
v := []string{"hostname"}
return &fooCollector{
fooMetric: prometheus.NewDesc("fff_metrics","Show metrics a for mysql",nil,nil),
barMetric:prometheus.NewDesc("bbb_metrics","Show metrics a bar occu",v,m1),
}
}

func (collect *fooCollector) Describe(ch chan <- *prometheus.Desc) {
ch <- collect.barMetric
ch <- collect.fooMetric

}

func (collect *fooCollector) Collect(ch chan<- prometheus.Metric) {
var metricValue float64

if 1 == 1 {
metricValue = 1
}


ch <- prometheus.MustNewConstMetric(collect.fooMetric,prometheus.GaugeValue, metricValue)
ch <- prometheus.MustNewConstMetric(collect.barMetric,prometheus.CounterValue, metricValue,"kk")
}


func main() {
foo := newFooCollector()
prometheus.MustRegister(foo)
log.Info("beging to server on Port: 18080")
http.Handle("/metrics",promhttp.Handler())
log.Fatal(http.ListenAndServe(":18080",nil))
}


# 一个变量label, 一个是常量label.

  

转载于:https://www.cnblogs.com/nika86/p/10552234.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值