基于Prometheus的client_golang开发自己的exporter

在 go get github.com/prometheus/client_golang   时可能会遇到有些库拉不下来,此时可以直接去GitHub网站拉取源码包,可能需要用到的库如下:

https://github.com/prometheus/common.git

https://github.com/prometheus/client_model.git

https://github.com/prometheus/client_golang.git
https://github.com/matttproud/golang_protobuf_extensions.git

https://github.com/protocolbuffers/protobuf-go.git

https://github.com/golang/sys.git

将拉取到的包解压到GOPATH/src 目录的相应位置(目录名称需要修改)

准备工作完成后开始编码

https://prometheus.io/docs/instrumenting/clientlibs/    这个是Prometheus官网给出的支持的编码语言

https://prometheus.io/docs/guides/go-application/   官网也是相应给出了一个使用client_golang的demo。英语好的可以直接看官网。

如下是用到的所有库

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

自定义的exporter其实就是两步

1、封装一个Prometheus规定的数据格式。

2、将封装好的数据通过HTTP API提供出去。

Prometheus根据配置的API地址、以及拉取时间拉取数据,最终做展示。

 

如下,定义了两种数据类型的采集数据,一种是gauge(cpuTemp、cpuTemp2),一种是counter(hdFailures)

Guage 类型代表一种样本数据可以任意变化的指标,即可增可减。

Counter 类型代表一种样本数据单调递增的指标,即只增不减,除非监控系统发生了重置。

var (
   cpuTemp = prometheus.NewGaugeVec(prometheus.GaugeOpts{
      Name: "cpu_temperature_celsius",
      Help: "Current temperature of the CPU.",
   },
   []string{
      // Which user has requested the operation?
      "endpoint",
      "metric_group",
      // Of what type is the operation?
      "component",
   },
   )
   cpuTemp2 = prometheus.NewGaugeVec(prometheus.GaugeOpts{
      Name: "cpu_temperature_celsius_ppp",
      Help: "Current temperature of the CPU.",
   },
      []string{
         // Which user has requested the operation?
         "endpoint",
         "metric_group",
         // Of what type is the operation?
         "component",
      },
   )
   hdFailures = prometheus.NewCounterVec(
      prometheus.CounterOpts{
         Name: "hd_errors_total",
         Help: "Number of hard-disk errors.",
      },
      []string{"device"},
   )
)

Prometheus定义的数据格式如下

即要包括三大块   

1、HELP 描述指标具体含义 

2、TYPE标识数据类型   

3、指标名称、大括号中的label额外信息以及具体数值(数值均为float64类型)

代码构成

提供一个初始化方法,在初始化的时候就将数据注册进去

func init() {
   // Metrics have to be registered to be exposed:
   mv := []prometheus.GaugeVec{}
   mv = append(mv, *cpuTemp,*cpuTemp2)
   prometheus.MustRegister(mv[0],mv[1])
   log.Println(len(mv))
   prometheus.MustRegister(hdFailures)
}

最终main方法如下

func main() {
   //cpuTemp.Set(65.3)
   // Increase a value using compact (but order-sensitive!) WithLabelValues().
   cpuTemp.WithLabelValues("aiops1","CpuMonitor", "CPU").Add(4)
   cpuTemp2.WithLabelValues("hhhh","bbbbb","ttttt").Add(8)
   hdFailures.With(prometheus.Labels{"device":"/dev/sda"}).Inc()
   
   http.Handle("/metrics", promhttp.Handler())
   log.Fatal(http.ListenAndServe(":2112", nil))
}

第一部分即把具体值写入到指标中

第二部分即通过client_golang提供的处理HTTP方法将数据通过API方式提供出去 ,如上

最终通过 http://localhost:2112/metrics     可以获取到指标。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值