基于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     可以获取到指标。

Prometheus 的 SQL Exporter 是一个通用工具,用于从 SQL 数据库抓取监控数据。如果你想使用它来监控 Tongrds(一个针对 MongoDB 的监控系统),你需要创建一个适配 Tongrds 数据源的查询适配器,然后将其注册为 Promethues 的查询源。 以下是基本步骤: 1. **安装 SQL Exporter**: 首先,你需要从 GitHub 下载 Prometheus 的 SQL Exporter 并按照官方指南安装。 2. **创建适配器**: 编写一个新的 Go 项目,创建一个 `adapter` 包,实现一个函数来查询 Tongrds 提供的 SQL 接口。这可能涉及到查询 MongoDB 的特定表,获取你关心的性能指标,如读写操作次数、延迟等。 ```go import ( "context" "database/sql" "github.com/prometheus/client_golang/prometheus" _ "github.com/go-sql-driver/mysql" // 或者其他的 SQL 驱动,取决于 Tongrds 支持的数据库 ) func (adapter *TongrdsAdapter) queryMetrics(ctx context.Context) (map[string]float64, error) { db, err := sql.Open("mysql", "user:password@tcp(host:port)/db_name") if err != nil { return nil, err } defer db.Close() rows, err := db.QueryContext(ctx, "SELECT metric_name, value FROM metrics") if err != nil { return nil, err } var result map[string]float64 for rows.Next() { var name string var value float64 err = rows.Scan(&name, &value) if err != nil { return nil, err } result[name] = value } return result, nil } ``` 3. **注册适配器**: 将适配器作为 Prometheus 的 SQL 源注册。 ```go sqlExporter := prometheus.NewSQLScraper( prometheus.Config{ QueryConfig: prometheus.QueryConfig{ Database: "your_tongrds_database", // 这里填写适配器查询函数 Query: func(_ context.Context, query string) (prometheus.Response, error) { return adapter.queryMetrics(context.Background()) }, }, }, ) ``` 4. **启动 Prometheus**: 启动 Prometheus 服务器,现在它会从你创建的适配器中拉取 Tongrds 监控数据。 注意:以上示例假设 Tongrds 提供了可以直接SQL化的查询接口。实际操作中,你可能需要查阅 Tongrds 文档来确认具体的查询语法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值