forked from zeromicro/go-zero
/
histogram.go
57 lines (48 loc) · 1.04 KB
/
histogram.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
package metric
import (
"github.com/3Rivers/go-zero/core/proc"
prom "github.com/prometheus/client_golang/prometheus"
)
type (
HistogramVecOpts struct {
Namespace string
Subsystem string
Name string
Help string
Labels []string
Buckets []float64
}
HistogramVec interface {
Observe(v int64, lables ...string)
close() bool
}
promHistogramVec struct {
histogram *prom.HistogramVec
}
)
func NewHistogramVec(cfg *HistogramVecOpts) HistogramVec {
if cfg == nil {
return nil
}
vec := prom.NewHistogramVec(prom.HistogramOpts{
Namespace: cfg.Namespace,
Subsystem: cfg.Subsystem,
Name: cfg.Name,
Help: cfg.Help,
Buckets: cfg.Buckets,
}, cfg.Labels)
prom.MustRegister(vec)
hv := &promHistogramVec{
histogram: vec,
}
proc.AddShutdownListener(func() {
hv.close()
})
return hv
}
func (hv *promHistogramVec) Observe(v int64, labels ...string) {
hv.histogram.WithLabelValues(labels...).Observe(float64(v))
}
func (hv *promHistogramVec) close() bool {
return prom.Unregister(hv.histogram)
}