-
Notifications
You must be signed in to change notification settings - Fork 15
/
registerer.go
60 lines (51 loc) · 1 KB
/
registerer.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/prometheus/client_golang/prometheus"
)
type Registerer struct {
wrap prometheus.Registerer
cs map[prometheus.Collector]struct{}
}
func WrapWithRegisterer(reg prometheus.Registerer) *Registerer {
return &Registerer{
wrap: reg,
cs: make(map[prometheus.Collector]struct{}),
}
}
func (u *Registerer) Register(c prometheus.Collector) error {
if u.wrap == nil {
return nil
}
err := u.wrap.Register(c)
if err != nil {
return err
}
u.cs[c] = struct{}{}
return nil
}
func (u *Registerer) Unregister(c prometheus.Collector) bool {
if u.wrap != nil && u.wrap.Unregister(c) {
delete(u.cs, c)
return true
}
return false
}
func (u *Registerer) UnregisterAll() bool {
success := true
for c := range u.cs {
if !u.Unregister(c) {
success = false
}
}
return success
}
func (u *Registerer) RegisterAll(cs []prometheus.Collector) error {
var result error
for i := range cs {
err := u.Register(cs[i])
if err != nil {
result = err
}
}
return result
}