-
Notifications
You must be signed in to change notification settings - Fork 0
/
trusted-node-collector.go
340 lines (292 loc) · 10.2 KB
/
trusted-node-collector.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package collectors
import (
"fmt"
"log"
"math/big"
"strconv"
"sync"
"time"
"github.com/Seb369888/poolsea-go/dao"
"github.com/Seb369888/poolsea-go/dao/trustednode"
"github.com/Seb369888/poolsea-go/node"
"github.com/Seb369888/poolsea-go/rocketpool"
"github.com/Seb369888/poolsea-go/tokens"
"github.com/Seb369888/poolsea-go/types"
"github.com/Seb369888/poolsea-go/utils/eth"
"github.com/Seb369888/smartnode/shared/services/beacon"
"github.com/Seb369888/smartnode/shared/services/config"
"github.com/ethereum/go-ethereum/common"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
)
// Represents the collector for the user's trusted node
type TrustedNodeCollector struct {
// The number of proposals per state
proposalCount *prometheus.Desc
// The number of votable proposals this node has yet to vote on
unvotedProposalCount *prometheus.Desc
// Tabular data for the votes for and against each proposal
proposalTable *prometheus.Desc
// The ETH balance of each trusted node
ethBalance *prometheus.Desc
// The balances submission participation of the ODAO members
balancesParticipation *prometheus.Desc
// The prices submission participation of the ODAO members
pricesParticipation *prometheus.Desc
// Whether or not ODAO collection is enabled
enabled bool
// The Rocket Pool contract manager
rp *rocketpool.RocketPool
// The beacon client
bc beacon.Client
// The node's address
nodeAddress common.Address
// Cached data
cacheTime time.Time
cachedMetrics []prometheus.Metric
// The event log interval for the current eth1 client
eventLogInterval *big.Int
// The thread-safe locker for the network state
stateLocker *StateLocker
// Prefix for logging
logPrefix string
}
// Create a new NodeCollector instance
func NewTrustedNodeCollector(rp *rocketpool.RocketPool, bc beacon.Client, nodeAddress common.Address, cfg *config.RocketPoolConfig, stateLocker *StateLocker) *TrustedNodeCollector {
// Get the event log interval
eventLogInterval, err := cfg.GetEventLogInterval()
if err != nil {
log.Printf("Error getting event log interval: %s\n", err.Error())
return nil
}
subsystem := "trusted_node"
return &TrustedNodeCollector{
proposalCount: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "proposal_count"),
"The number of proposals in each state",
[]string{"state"}, nil,
),
unvotedProposalCount: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "unvoted_proposal_count"),
"How many active proposals has this trusted node has not voted on",
nil, nil,
),
proposalTable: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "proposal_table"),
"Tabular data of each active proposal's for and against votes",
[]string{"proposal", "category"}, nil,
),
ethBalance: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "eth_balance"),
"The ETH balance of each trusted node",
[]string{"member"}, nil,
),
balancesParticipation: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "balances_participation"),
"Whether each member has participated in the current balances update interval",
[]string{"member"}, nil,
),
pricesParticipation: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "prices_participation"),
"Whether each member has participated in the current prices update interval",
[]string{"member"}, nil,
),
enabled: cfg.EnableODaoMetrics.Value.(bool),
rp: rp,
bc: bc,
nodeAddress: nodeAddress,
eventLogInterval: big.NewInt(int64(eventLogInterval)),
stateLocker: stateLocker,
logPrefix: "ODAO Stats Collector",
}
}
// Write metric descriptions to the Prometheus channel
func (collector *TrustedNodeCollector) Describe(channel chan<- *prometheus.Desc) {
channel <- collector.proposalCount
channel <- collector.unvotedProposalCount
channel <- collector.proposalTable
channel <- collector.ethBalance
channel <- collector.balancesParticipation
channel <- collector.pricesParticipation
}
// Caches slow to process metrics so it doesn't have to be processed every second
func (collector *TrustedNodeCollector) collectSlowMetrics(memberIds map[common.Address]string) {
if !collector.enabled {
return
}
// Create a new cached metrics array to populate
collector.cachedMetrics = make([]prometheus.Metric, 0)
// Sync
var wg errgroup.Group
var balancesParticipation map[common.Address]bool
var pricesParticipation map[common.Address]bool
// Get the balances participation data
wg.Go(func() error {
var err error
balancesParticipation, err = node.GetTrustedNodeLatestBalancesParticipation(collector.rp, nil, nil)
if err != nil {
return fmt.Errorf("Error getting trusted node balances participation data: %w", err)
}
return nil
})
// Get the prices participation data
wg.Go(func() error {
var err error
pricesParticipation, err = node.GetTrustedNodeLatestPricesParticipation(collector.rp, nil, nil)
if err != nil {
return fmt.Errorf("Error getting trusted node prices participation data: %w", err)
}
return nil
})
// Wait for data
if err := wg.Wait(); err != nil {
collector.logError(err)
return
}
// Balances participation
for member, status := range balancesParticipation {
value := float64(0)
if status {
value = 1
}
collector.cachedMetrics = append(collector.cachedMetrics, prometheus.MustNewConstMetric(collector.balancesParticipation, prometheus.GaugeValue, value, memberIds[member]))
}
// Prices participation
for member, status := range pricesParticipation {
value := float64(0)
if status {
value = 1
}
collector.cachedMetrics = append(collector.cachedMetrics, prometheus.MustNewConstMetric(collector.pricesParticipation, prometheus.GaugeValue, value, memberIds[member]))
}
}
// Collect the latest metric values and pass them to Prometheus
func (collector *TrustedNodeCollector) Collect(channel chan<- prometheus.Metric) {
if !collector.enabled {
return
}
// Sync
var wg errgroup.Group
var err error
var proposals []dao.ProposalDetails
memberIds := make(map[common.Address]string)
ethBalances := make(map[string]float64)
unvotedCount := float64(0)
pendingCount := float64(0)
activeCount := float64(0)
succeededCount := float64(0)
executedCount := float64(0)
cancelledCount := float64(0)
defeatedCount := float64(0)
expiredCount := float64(0)
// Get the total staked RPL
wg.Go(func() error {
proposals, err = dao.GetProposalsWithMember(collector.rp, collector.nodeAddress, nil)
if err != nil {
return fmt.Errorf("Error getting proposals: %w", err)
}
return nil
})
// Get member IDs
wg.Go(func() error {
members, err := trustednode.GetMembers(collector.rp, nil)
if err != nil {
return fmt.Errorf("Error getting members: %w", err)
}
for _, member := range members {
memberIds[member.Address] = member.ID
}
return nil
})
// Only collect fresh participation metrics from chain every 60 seconds as it updates infrequently and takes longer to collect
now := time.Now()
if now.Unix() > collector.cacheTime.Add(time.Second*60).Unix() {
collector.collectSlowMetrics(memberIds)
collector.cacheTime = now
}
// Wait for data
if err := wg.Wait(); err != nil {
collector.logError(err)
return
}
lock := sync.Mutex{}
// Get member ETH balances
for address, memberId := range memberIds {
wg.Go(func(address common.Address, id string) func() error {
return func() error {
// Get balances
balances, err := tokens.GetBalances(collector.rp, address, nil)
if err != nil {
return fmt.Errorf("Error getting node balances: %w", err)
}
lock.Lock()
ethBalances[id] = eth.WeiToEth(balances.ETH)
lock.Unlock()
return nil
}
}(address, memberId))
}
// Wait for data
if err := wg.Wait(); err != nil {
collector.logError(err)
return
}
// Calculate metrics
for _, proposal := range proposals {
switch proposal.State {
case types.Pending:
pendingCount++
case types.Active:
activeCount++
if !proposal.MemberVoted {
unvotedCount++
}
case types.Succeeded:
succeededCount++
case types.Executed:
executedCount++
case types.Cancelled:
cancelledCount++
case types.Defeated:
defeatedCount++
case types.Expired:
expiredCount++
}
}
// Update all the metrics
channel <- prometheus.MustNewConstMetric(
collector.unvotedProposalCount, prometheus.GaugeValue, unvotedCount)
channel <- prometheus.MustNewConstMetric(
collector.proposalCount, prometheus.GaugeValue, pendingCount, "pending")
channel <- prometheus.MustNewConstMetric(
collector.proposalCount, prometheus.GaugeValue, activeCount, "active")
channel <- prometheus.MustNewConstMetric(
collector.proposalCount, prometheus.GaugeValue, succeededCount, "succeeded")
channel <- prometheus.MustNewConstMetric(
collector.proposalCount, prometheus.GaugeValue, executedCount, "executed")
channel <- prometheus.MustNewConstMetric(
collector.proposalCount, prometheus.GaugeValue, cancelledCount, "cancelled")
channel <- prometheus.MustNewConstMetric(
collector.proposalCount, prometheus.GaugeValue, defeatedCount, "defeated")
channel <- prometheus.MustNewConstMetric(
collector.proposalCount, prometheus.GaugeValue, expiredCount, "expired")
// Update balance metrics
for memberId, balance := range ethBalances {
channel <- prometheus.MustNewConstMetric(
collector.ethBalance, prometheus.GaugeValue, balance, memberId)
}
// Update proposal metrics
for _, proposal := range proposals {
if proposal.State != types.Active {
continue
}
channel <- prometheus.MustNewConstMetric(
collector.proposalTable, prometheus.GaugeValue, proposal.VotesFor, strconv.FormatUint(proposal.ID, 10), "for")
channel <- prometheus.MustNewConstMetric(
collector.proposalTable, prometheus.GaugeValue, proposal.VotesAgainst, strconv.FormatUint(proposal.ID, 10), "against")
channel <- prometheus.MustNewConstMetric(
collector.proposalTable, prometheus.GaugeValue, proposal.VotesRequired, strconv.FormatUint(proposal.ID, 10), "required")
}
// Include cached metrics
for _, metric := range collector.cachedMetrics {
channel <- metric
}
}
// Log error messages
func (collector *TrustedNodeCollector) logError(err error) {
fmt.Printf("[%s] %s\n", collector.logPrefix, err.Error())
}