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

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

1. 不带lab的Gauge

1.1 定义指标

语法

  • 实例化
func NewGauge(opts GaugeOpts) Gauge
  • GaugeOpts
type GaugeOpts Opts
  • 结构体Opts
type Opts struct {
    Namespace   string
    Subsystem   string
    Name        string
    Help        string
    ConstLabels Labels
}

完整示例

  • 写一段简单的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))
}
  • 结果如下
    我们访问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

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

1.2 给指标取值

语法

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

完整示例

  • 代码
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))
}
  • 结果

我们不断刷新可见,每秒钟关羽吃的馒头+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

2. 带lab的Gauge

2.1 定义指标

语法

  • 实例化
func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec
  • 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))
}
  • 结果显示
    有lab的Counter没有赋值是看不见的(而上例可知,没有tag的Counter模式值是0。)

2.2 取值

语法

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

见前文,同“不带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))
}
  • 结果
# 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
  • 0
    点赞
  • 1
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:Age of Ai 设计师:meimeiellie 返回首页
评论

打赏作者

开发运维玄德公

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值