-
Notifications
You must be signed in to change notification settings - Fork 12
/
mailbox_prom.go
96 lines (81 loc) · 2.09 KB
/
mailbox_prom.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package mailbox
import (
"context"
"strconv"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils"
)
var mailboxLoad = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "mailbox_load_percent",
Help: "Percent of mailbox capacity used",
},
[]string{"appID", "name", "capacity"},
)
const mailboxPromInterval = 5 * time.Second
type Monitor struct {
services.StateMachine
appID string
lggr logger.Logger
mailboxes sync.Map
stop func()
done chan struct{}
}
func NewMonitor(appID string, lggr logger.Logger) *Monitor {
return &Monitor{appID: appID, lggr: logger.Named(lggr, "Monitor")}
}
func (m *Monitor) Name() string { return m.lggr.Name() }
func (m *Monitor) Start(context.Context) error {
return m.StartOnce("Monitor", func() error {
t := time.NewTicker(utils.WithJitter(mailboxPromInterval))
ctx, cancel := context.WithCancel(context.Background())
m.stop = func() {
t.Stop()
cancel()
}
m.done = make(chan struct{})
go m.monitorLoop(ctx, t.C)
return nil
})
}
func (m *Monitor) Close() error {
return m.StopOnce("Monitor", func() error {
m.stop()
<-m.done
return nil
})
}
func (m *Monitor) HealthReport() map[string]error {
return map[string]error{m.Name(): m.Healthy()}
}
func (m *Monitor) monitorLoop(ctx context.Context, c <-chan time.Time) {
defer close(m.done)
for {
select {
case <-ctx.Done():
return
case <-c:
m.mailboxes.Range(func(k, v any) bool {
name, mb := k.(string), v.(mailbox)
c, p := mb.load()
capacity := strconv.FormatUint(c, 10)
mailboxLoad.WithLabelValues(m.appID, name, capacity).Set(p)
return true
})
}
}
}
type mailbox interface {
load() (capacity uint64, percent float64)
onClose(func())
}
func (m *Monitor) Monitor(mb mailbox, name ...string) {
n := strings.Join(name, ".")
m.mailboxes.Store(n, mb)
mb.onClose(func() { m.mailboxes.Delete(n) })
}