-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller.go
226 lines (199 loc) · 5.18 KB
/
poller.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"context"
"errors"
"fmt"
"sync"
"time"
tz "github.com/ecadlabs/gotez/v2"
client "github.com/ecadlabs/gotez/v2/clientv2"
"github.com/ecadlabs/gotez/v2/clientv2/mempool"
"github.com/ecadlabs/gotez/v2/clientv2/network"
"github.com/ecadlabs/gotez/v2/clientv2/utils"
"github.com/ecadlabs/gotez/v2/protocol/latest"
"github.com/ecadlabs/gotez/v2/protocol/proto_016_PtMumbai"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type PollerConfig struct {
Client *client.Client
ChainID *tz.ChainID
Timeout time.Duration
Interval time.Duration
Reg prometheus.Registerer
NextProtocolFunc func() *tz.ProtocolHash
}
type Poller struct {
cfg PollerConfig
mtx sync.RWMutex
status utils.BootstrappedResponse
cancel context.CancelFunc
done chan struct{}
bsGauge prometheus.Gauge
connGauge *prometheus.GaugeVec
opsGauge *prometheus.GaugeVec
}
func (c *PollerConfig) New() *Poller {
b := &Poller{
cfg: *c,
bsGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "tezos",
Subsystem: "node",
Name: "bootstrapped",
Help: "Returns 1 if the node has synchronized its chain with a few peers.",
}),
connGauge: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "tezos",
Subsystem: "node",
Name: "connections",
Help: "Current number of connections to/from this node.",
}, []string{"direction", "private"}),
opsGauge: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "tezos",
Subsystem: "node",
Name: "mempool_operations",
Help: "The current number of mempool operations.",
}, []string{"kind", "pool", "proto"}),
}
if c.Reg != nil {
c.Reg.MustRegister(b.bsGauge)
c.Reg.MustRegister(b.connGauge)
c.Reg.MustRegister(b.opsGauge)
}
return b
}
func (p *Poller) Start() {
ctx, cancel := context.WithCancel(context.Background())
p.cancel = cancel
p.done = make(chan struct{})
go p.loop(ctx)
}
func (p *Poller) Stop(ctx context.Context) error {
p.cancel()
select {
case <-p.done:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (p *Poller) Status() utils.BootstrappedResponse {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.status
}
func (p *Poller) loop(ctx context.Context) {
t := time.NewTicker(p.cfg.Interval)
defer func() {
t.Stop()
close(p.done)
}()
pollers := []func(context.Context, chan<- error){
p.pollBootstrapped,
p.pollConnections,
p.pollMempoolOperations,
}
errCh := make(chan error, len(pollers))
for {
for _, poller := range pollers {
go poller(ctx, errCh)
}
done := false
for range pollers {
err := <-errCh
if err != nil {
if errors.Is(err, context.Canceled) {
done = true
} else {
log.WithField("chain_id", p.cfg.ChainID).Warn(err)
}
}
}
if done {
return
}
select {
case <-t.C:
case <-ctx.Done():
return
}
}
}
func (p *Poller) pollBootstrapped(ctx context.Context, errCh chan<- error) {
var err error
defer func() { errCh <- err }()
c, cancel := context.WithTimeout(ctx, p.cfg.Timeout)
defer cancel()
resp, err := utils.IsBootstrapped(c, p.cfg.Client, p.cfg.ChainID)
if err != nil {
return
}
p.mtx.Lock()
p.status = *resp
p.mtx.Unlock()
v := 0.0
if resp.SyncState == utils.SyncStateSynced && resp.Bootstrapped {
v = 1
}
p.bsGauge.Set(v)
}
func (p *Poller) pollConnections(ctx context.Context, errCh chan<- error) {
var err error
defer func() { errCh <- err }()
c, cancel := context.WithTimeout(ctx, p.cfg.Timeout)
defer cancel()
resp, err := network.Connections(c, p.cfg.Client)
if err != nil {
return
}
p.connGauge.Reset()
for _, conn := range resp.Connections {
var dir string
if conn.Incoming {
dir = "incoming"
} else {
dir = "outgoing"
}
p.connGauge.With(prometheus.Labels{"direction": dir, "private": fmt.Sprintf("%t", conn.Private)}).Inc()
}
}
func updatePool(g *prometheus.GaugeVec, list []*proto_016_PtMumbai.OperationWithoutMetadata[latest.OperationContents]) {
for _, grp := range list {
for _, op := range grp.Operations() {
g.With(prometheus.Labels{"kind": op.OperationKind()}).Inc()
}
}
}
func (p *Poller) pollMempoolOperations(ctx context.Context, errCh chan<- error) {
var err error
defer func() { errCh <- err }()
c, cancel := context.WithTimeout(ctx, p.cfg.Timeout)
defer cancel()
resp, err := mempool.PendingOperations(c, p.cfg.Client, p.cfg.ChainID)
if err != nil {
return
}
p.opsGauge.Reset()
gauge := p.opsGauge.MustCurryWith(prometheus.Labels{"proto": p.cfg.NextProtocolFunc().String()})
g := gauge.MustCurryWith(prometheus.Labels{"pool": "validated"})
for _, list := range resp.Validated {
updatePool(g, list.Contents)
}
pools := [][]*mempool.PendingOperationsListWithError{
resp.Refused,
resp.Outdated,
resp.BranchRefused,
resp.BranchDelayed,
}
poolNames := []string{"refused", "outdated", "branch_refused", "branch_delayed"}
for i, pool := range pools {
g := gauge.MustCurryWith(prometheus.Labels{"pool": poolNames[i]})
for _, list := range pool {
updatePool(g, list.Contents)
}
}
g = gauge.MustCurryWith(prometheus.Labels{"pool": "unprocessed"})
for _, list := range resp.Unprocessed {
updatePool(g, list.Contents)
}
}