# 动手写prometheus的exporter-01-Gauge（仪表盘）

46 篇文章 17 订阅
18 篇文章 5 订阅
7 篇文章 1 订阅

#### 文章目录

特点：数据可增减（反应当前状态）。

## 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 (
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())
}

• 结果如下
我们访问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 给指标取值

#### 语法

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 (
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 {
time.Sleep(time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler())
}

• 结果

# 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 (
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())
}

• 结果显示
有lab的Counter没有赋值是看不见的（而上例可知，没有tag的Counter模式值是0。）

### 2.2 取值

#### 语法

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

• 取值

#### 完整示例

• 代码如下
package main

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

//自定义端口

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 {
time.Sleep(time.Second)
}
}()
go func() {
for true {
time.Sleep(time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler())
}

• 结果
# 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
评论
07-21 655
03-27 1554
05-19 377
04-28 640
10-05 2789
12-26 4540
08-09 266
08-07 979
10-25 2672
07-17 1520

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

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