forked from zeromicro/go-zero
/
gauge.go
60 lines (49 loc) · 1.08 KB
/
gauge.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package metric
import (
"github.com/dylanNew/go-zero/core/proc"
prom "github.com/prometheus/client_golang/prometheus"
)
type (
GaugeVecOpts VectorOpts
GuageVec interface {
Set(v float64, labels ...string)
Inc(labels ...string)
Add(v float64, labels ...string)
close() bool
}
promGuageVec struct {
gauge *prom.GaugeVec
}
)
func NewGaugeVec(cfg *GaugeVecOpts) GuageVec {
if cfg == nil {
return nil
}
vec := prom.NewGaugeVec(
prom.GaugeOpts{
Namespace: cfg.Namespace,
Subsystem: cfg.Subsystem,
Name: cfg.Name,
Help: cfg.Help,
}, cfg.Labels)
prom.MustRegister(vec)
gv := &promGuageVec{
gauge: vec,
}
proc.AddShutdownListener(func() {
gv.close()
})
return gv
}
func (gv *promGuageVec) Inc(labels ...string) {
gv.gauge.WithLabelValues(labels...).Inc()
}
func (gv *promGuageVec) Add(v float64, lables ...string) {
gv.gauge.WithLabelValues(lables...).Add(v)
}
func (gv *promGuageVec) Set(v float64, lables ...string) {
gv.gauge.WithLabelValues(lables...).Set(v)
}
func (gv *promGuageVec) close() bool {
return prom.Unregister(gv.gauge)
}