• 动手写prometheus的exporter-01-Gauge(仪表盘)


    特点:数据可增减(反应当前状态)。

    1. 不带lab的Gauge

    1.1 定义指标

    语法

    • 实例化
    func NewGauge(opts GaugeOpts) Gauge
    
    • 1
    • GaugeOpts
    type GaugeOpts Opts
    
    • 1
    • 结构体Opts
    type Opts struct {
        Namespace   string
        Subsystem   string
        Name        string
        Help        string
        ConstLabels Labels
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    完整示例

    • 写一段简单的exporter如下:
    package main
    
    import (
    	"flag"
    	"github.com/prometheus/client_golang/prometheus"
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    	"log"
    	"net/http"
    )
    
    //自定义端口
    var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")
    
    var (
    	numOfBuns = prometheus.NewGauge(prometheus.GaugeOpts{
    		Namespace: "xi_shu",
    		Subsystem: "jing_zhou_team",
    		Name:      "guanyu_eaten_num",
    		Help:      "Number of steamed buns eaten by Guan Yu",
    	})
    )
    
    func init() {
    	prometheus.MustRegister(numOfBuns)
    }
    
    func main() {
    	flag.Parse()
    	http.Handle("/metrics", promhttp.Handler())
    	log.Fatal(http.ListenAndServe(*addr, nil))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 结果如下
      我们访问http://127.0.0.1:1840/metrics页面,可以找到如下一段
    # HELP xi_shu_jing_zhou_team_guanyu_eaten_num Number of steamed buns eaten by Guan Yu
    # TYPE xi_shu_jing_zhou_team_guanyu_eaten_num gauge
    xi_shu_jing_zhou_team_guanyu_eaten_num 0
    
    • 1
    • 2
    • 3

    如上可知,我们的指标是用Opts结构体的实例化拼接出来的。

    1.2 给指标取值

    语法

    • add() 增加n
    func (Gauge) Add(float64)
    
    • 1
    • sub() 减去n
    func (Gauge) Sub(float64)
    
    • 1
    • Inc() 增加1
    func (Gauge) Inc()
    
    • 1
    • dec() 减少1
    func (Gauge) Dec()
    
    • 1
    • set() 重新设置
    func (Gauge) Set(float64)
    
    • 1
    • 设置值为当前时间时间戳
    func (Gauge) SetToCurrentTime()
    
    • 1

    完整示例

    • 代码
    package main
    
    import (
    	"flag"
    	"github.com/prometheus/client_golang/prometheus"
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    	"log"
    	"net/http"
    	"time"
    )
    var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")
    
    var (
    	numOfBuns = prometheus.NewGauge(prometheus.GaugeOpts{
    		Namespace: "xi_shu",
    		Subsystem: "jing_zhou_team",
    		Name:      "guanyu_eaten_num",
    		Help:      "Number of steamed buns eaten by Guan Yu",
    	})
    )
    
    func init() {
    	prometheus.MustRegister(numOfBuns)
    }
    
    func main() {
    	flag.Parse()
    	go func() {
    		for true {
    			numOfBuns.Add(2)
    			time.Sleep(time.Second)
    		}
    	}()
    	http.Handle("/metrics", promhttp.Handler())
    	log.Fatal(http.ListenAndServe(*addr, nil))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 结果

    我们不断刷新可见,每秒钟关羽吃的馒头+2

    # HELP xi_shu_jing_zhou_team_guanyu_eaten_num Number of steamed buns eaten by Guan Yu
    # TYPE xi_shu_jing_zhou_team_guanyu_eaten_num gauge
    xi_shu_jing_zhou_team_guanyu_eaten_num 24
    
    • 1
    • 2
    • 3

    2. 带lab的Gauge

    2.1 定义指标

    语法

    • 实例化
    func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec
    
    • 1
    • GaugeOpts 见上例

    完整示例

    package main
    
    import (
    	"flag"
    	"github.com/prometheus/client_golang/prometheus"
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    	"log"
    	"net/http"
    	"time"
    )
    
    //自定义端口
    var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")
    
    var (
    	numOfBuns = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    		Namespace: "xi_shu",
    		Subsystem: "jing_zhou_team",
    		Name:      "eaten_num",
    		Help:      "Number of steamed buns eaten by Guan Yu",
    	},[]string{"name","age"})
    )
    
    func init() {
    	prometheus.MustRegister(numOfBuns)
    }
    
    func main() {
    	flag.Parse()
    	http.Handle("/metrics", promhttp.Handler())
    	log.Fatal(http.ListenAndServe(*addr, nil))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 结果显示
      有lab的Counter没有赋值是看不见的(而上例可知,没有tag的Counter模式值是0。)

    2.2 取值

    语法

    • 添加lab
      实例化之后,需要先给实例打上lab,因为不同的lab会单独作为一个数据显示。
    func (v *GaugeVec) WithLabelValues(lvs ...string) Gauge
    
    • 1
    • 取值

    见前文,同“不带lab的Gauge”

    完整示例

    • 代码如下
    package main
    
    import (
    	"flag"
    	"github.com/prometheus/client_golang/prometheus"
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    	"log"
    	"net/http"
    	"time"
    )
    
    //自定义端口
    var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")
    
    var (
    	numOfBuns = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    		Namespace: "xi_shu",
    		Subsystem: "jing_zhou_team",
    		Name:      "eaten_num",
    		Help:      "Number of steamed buns eaten by Guan Yu",
    	},[]string{"name","age"})
    )
    
    func init() {
    	prometheus.MustRegister(numOfBuns)
    }
    
    func main() {
    	flag.Parse()
    	go func() {
    		for true {
    			numOfBuns.WithLabelValues("guan_yu","25").Add(2)
    			time.Sleep(time.Second)
    		}
    	}()
    	go func() {
    		for true {
    			numOfBuns.WithLabelValues("zhang_fei","22").Add(3)
    			time.Sleep(time.Second)
    		}
    	}()
    	http.Handle("/metrics", promhttp.Handler())
    	log.Fatal(http.ListenAndServe(*addr, nil))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 结果
    # HELP xi_shu_jing_zhou_team_eaten_num Number of steamed buns eaten by Guan Yu
    # TYPE xi_shu_jing_zhou_team_eaten_num gauge
    xi_shu_jing_zhou_team_eaten_num{age="22",name="zhang_fei"} 21
    xi_shu_jing_zhou_team_eaten_num{age="25",name="guan_yu"} 14
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    最详细、最全面的【Java日志框架】介绍,建议收藏,包含JUL、log4j、logback、log4j2等所有主流框架
    记账工具:轻松添加新账户并记录明细
    2. 慢查询、索引、执行计划详解
    通过memberlist库实现gossip管理集群以及集群数据交互
    关于网络协议的若干问题(二)
    spynet(一):光流估计代码介绍
    reduce方法的使用以及一些使用场景
    暑假怎么保护孩子的眼睛?预防近视护眼灯推荐
    基于SSM的高校勤工助学系统
    智慧工地云平台源码 人工智能AI+多系统集成+智能预警平台源码
  • 原文地址:https://blog.csdn.net/xingzuo_1840/article/details/126172991