-
Notifications
You must be signed in to change notification settings - Fork 107
/
odao-collector.go
96 lines (82 loc) · 3.33 KB
/
odao-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
package collectors
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/rocket-pool/rocketpool-go/rocketpool"
)
// Represents the collector for the ODAO metrics
type OdaoCollector struct {
// The latest block reported by the ETH1 client at the time of collecting the metrics
currentEth1Block *prometheus.Desc
// The ETH1 block that was used when reporting the latest prices
pricesBlock *prometheus.Desc
// The ETH1 block where the Effective RPL Stake was last updated
effectiveRplStakeBlock *prometheus.Desc
// The latest ETH1 block where network prices were reportable by the ODAO
latestReportableBlock *prometheus.Desc
// The Rocket Pool contract manager
rp *rocketpool.RocketPool
// The thread-safe locker for the network state
stateLocker *StateLocker
// Prefix for logging
logPrefix string
}
// Create a new DemandCollector instance
func NewOdaoCollector(rp *rocketpool.RocketPool, stateLocker *StateLocker) *OdaoCollector {
subsystem := "odao"
return &OdaoCollector{
currentEth1Block: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "current_eth1_block"),
"The latest block reported by the ETH1 client at the time of collecting the metrics",
nil, nil,
),
pricesBlock: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "prices_block"),
"The ETH1 block that was used when reporting the latest prices",
nil, nil,
),
effectiveRplStakeBlock: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "effective_rpl_stake_block"),
"The ETH1 block where the Effective RPL Stake was last updated",
nil, nil,
),
latestReportableBlock: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "latest_reportable_block"),
"The latest ETH1 block where network prices were reportable by the ODAO",
nil, nil,
),
rp: rp,
stateLocker: stateLocker,
logPrefix: "ODAO Collector",
}
}
// Write metric descriptions to the Prometheus channel
func (collector *OdaoCollector) Describe(channel chan<- *prometheus.Desc) {
channel <- collector.currentEth1Block
channel <- collector.pricesBlock
channel <- collector.effectiveRplStakeBlock
channel <- collector.latestReportableBlock
}
// Collect the latest metric values and pass them to Prometheus
func (collector *OdaoCollector) Collect(channel chan<- prometheus.Metric) {
// Get the latest state
state := collector.stateLocker.GetState()
if state == nil {
return
}
blockNumberFloat := float64(state.ElBlockNumber)
pricesBlockFloat := float64(state.NetworkDetails.PricesBlock)
effectiveRplStakeBlockFloat := pricesBlockFloat
var latestReportableBlockFloat float64
if !state.IsHoustonDeployed {
latestReportableBlockFloat = float64(state.NetworkDetails.LatestReportablePricesBlock)
channel <- prometheus.MustNewConstMetric(
collector.latestReportableBlock, prometheus.GaugeValue, latestReportableBlockFloat)
}
channel <- prometheus.MustNewConstMetric(
collector.currentEth1Block, prometheus.GaugeValue, blockNumberFloat)
channel <- prometheus.MustNewConstMetric(
collector.pricesBlock, prometheus.GaugeValue, pricesBlockFloat)
channel <- prometheus.MustNewConstMetric(
collector.effectiveRplStakeBlock, prometheus.GaugeValue, effectiveRplStakeBlockFloat)
}
// Log error messages
func (collector *OdaoCollector) logError(err error) {
fmt.Printf("[%s] %s\n", collector.logPrefix, err.Error())
}