@
特点
:数据可增减(反应当前状态)。
1. 不带lab的Gauge
1.1 定义指标
语法
| func NewGauge(opts GaugeOpts) Gauge |
| 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
页面,可以找到如下一段
| |
| |
| xi_shu_jing_zhou_team_guanyu_eaten_num 0 |
如上可知,我们的指标
是用Opts结构体的实例化
拼接出来的。
1.2 给指标取值
语法
| func (Gauge) Add(float64) |
| func (Gauge) Sub(float64) |
| 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
| |
| |
| xi_shu_jing_zhou_team_guanyu_eaten_num 24 |
2. 带lab的Gauge
2.1 定义指标
语法
| 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)) |
| } |
- 结果显示
有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 |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了