-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.go
More file actions
63 lines (49 loc) · 1.08 KB
/
Copy pathtesting.go
File metadata and controls
63 lines (49 loc) · 1.08 KB
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
61
62
63
package ph
import (
"sync"
"github.com/prometheus/client_golang/prometheus"
)
type TrackingRegistry struct {
prometheus.Registerer
prometheus.Gatherer
mu sync.Mutex
descs []*prometheus.Desc
}
func NewTrackingRegistry() *TrackingRegistry {
reg := prometheus.NewRegistry()
return &TrackingRegistry{
Registerer: reg,
Gatherer: reg,
}
}
func (r *TrackingRegistry) Register(cs prometheus.Collector) error {
r.track(cs)
return r.Registerer.(prometheus.Registerer).Register(cs)
}
func (r *TrackingRegistry) MustRegister(cs ...prometheus.Collector) {
for _, c := range cs {
r.track(c)
}
r.Registerer.(prometheus.Registerer).MustRegister(cs...)
}
func (r *TrackingRegistry) track(c prometheus.Collector) {
ch := make(chan *prometheus.Desc, 10)
go func() {
c.Describe(ch)
close(ch)
}()
for d := range ch {
r.mu.Lock()
r.descs = append(r.descs, d)
r.mu.Unlock()
}
}
func (r *TrackingRegistry) MetricNames() []string {
r.mu.Lock()
defer r.mu.Unlock()
names := make([]string, len(r.descs))
for i, d := range r.descs {
names[i] = d.String()
}
return names
}