特点
:数据可增减(反应当前状态)。
func NewGauge(opts GaugeOpts) Gauge
type GaugeOpts Opts
type Opts struct {
Namespace string
Subsystem string
Name string
Help string
ConstLabels Labels
}
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结构体的实例化
拼接出来的。
func (Gauge) Add(float64)
func (Gauge) Sub(float64)
func (Gauge) Inc()
func (Gauge) Dec()
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
func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec
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))
}
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