-
Notifications
You must be signed in to change notification settings - Fork 178
/
mempool.go
75 lines (58 loc) · 1.92 KB
/
mempool.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package metrics
import (
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/module"
)
type MempoolCollector struct {
unit *engine.Unit
entries *prometheus.GaugeVec
interval time.Duration
delay time.Duration
entriesFuncs map[string]module.EntriesFunc // keeps map of registered EntriesFunc of mempools
}
func NewMempoolCollector(interval time.Duration) *MempoolCollector {
mc := &MempoolCollector{
unit: engine.NewUnit(),
interval: interval,
delay: 0,
entriesFuncs: make(map[string]module.EntriesFunc),
entries: promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "entries_total",
Namespace: namespaceStorage,
Subsystem: subsystemMempool,
Help: "the number of entries in the mempool",
}, []string{LabelResource}),
}
return mc
}
func (mc *MempoolCollector) MempoolEntries(resource string, entries uint) {
mc.entries.With(prometheus.Labels{LabelResource: resource}).Set(float64(entries))
}
// Register registers entriesFunc for a resource
func (mc *MempoolCollector) Register(resource string, entriesFunc module.EntriesFunc) error {
mc.unit.Lock()
defer mc.unit.Unlock()
if _, ok := mc.entriesFuncs[resource]; ok {
return fmt.Errorf("cannot register resource, already exists: %s", resource)
}
mc.entriesFuncs[resource] = entriesFunc
return nil
}
func (mc *MempoolCollector) Ready() <-chan struct{} {
mc.unit.LaunchPeriodically(mc.gaugeEntries, mc.interval, mc.delay)
return mc.unit.Ready()
}
func (mc *MempoolCollector) Done() <-chan struct{} {
return mc.unit.Done()
}
// gaugeEntries iterates over the registered entries functions
// and calls MempoolEntries on them to capture the size of registered mempools
func (mc *MempoolCollector) gaugeEntries() {
for r, f := range mc.entriesFuncs {
mc.MempoolEntries(r, f())
}
}