-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance-collector.go
117 lines (100 loc) · 3.95 KB
/
performance-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
package collectors
import (
"fmt"
"github.com/Seb369888/poolsea-go/rocketpool"
"github.com/Seb369888/poolsea-go/utils/eth"
"github.com/prometheus/client_golang/prometheus"
)
// Represents the collector for the Performance metrics
type PerformanceCollector struct {
// The ETH utilization rate (%)
ethUtilizationRate *prometheus.Desc
// The total amount of ETH staked
totalStakingBalanceEth *prometheus.Desc
// The ETH / rETH ratio
ethRethExchangeRate *prometheus.Desc
// The total amount of ETH locked (TVL)
totalValueLockedEth *prometheus.Desc
// The total rETH supply
totalRethSupply *prometheus.Desc
// The ETH balance of the rETH contract address
rethContractBalance *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 PerformanceCollector instance
func NewPerformanceCollector(rp *rocketpool.RocketPool, stateLocker *StateLocker) *PerformanceCollector {
subsystem := "performance"
return &PerformanceCollector{
ethUtilizationRate: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "eth_utilization_rate"),
"The ETH utilization rate (%)",
nil, nil,
),
totalStakingBalanceEth: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "total_staking_balance_eth"),
"The total amount of ETH staked",
nil, nil,
),
ethRethExchangeRate: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "eth_reth_exchange_rate"),
"The ETH / rETH ratio",
nil, nil,
),
totalValueLockedEth: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "total_value_locked_eth"),
"The total amount of ETH locked (TVL)",
nil, nil,
),
rethContractBalance: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "reth_contract_balance"),
"The ETH balance of the rETH contract address",
nil, nil,
),
totalRethSupply: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "total_reth_supply"),
"The total rETH supply",
nil, nil,
),
rp: rp,
stateLocker: stateLocker,
logPrefix: "Performance Collector",
}
}
// Write metric descriptions to the Prometheus channel
func (collector *PerformanceCollector) Describe(channel chan<- *prometheus.Desc) {
channel <- collector.ethUtilizationRate
channel <- collector.totalStakingBalanceEth
channel <- collector.ethRethExchangeRate
channel <- collector.totalValueLockedEth
channel <- collector.rethContractBalance
channel <- collector.totalRethSupply
}
// Collect the latest metric values and pass them to Prometheus
func (collector *PerformanceCollector) Collect(channel chan<- prometheus.Metric) {
// Get the latest state
state := collector.stateLocker.GetState()
if state == nil {
return
}
ethUtilizationRate := state.NetworkDetails.ETHUtilizationRate
balanceFloat := eth.WeiToEth(state.NetworkDetails.StakingETHBalance)
exchangeRate := state.NetworkDetails.RETHExchangeRate
tvlFloat := eth.WeiToEth(state.NetworkDetails.TotalETHBalance)
rETHBalance := eth.WeiToEth(state.NetworkDetails.RETHBalance)
rethFloat := eth.WeiToEth(state.NetworkDetails.TotalRETHSupply)
channel <- prometheus.MustNewConstMetric(
collector.ethUtilizationRate, prometheus.GaugeValue, ethUtilizationRate)
channel <- prometheus.MustNewConstMetric(
collector.totalStakingBalanceEth, prometheus.GaugeValue, balanceFloat)
channel <- prometheus.MustNewConstMetric(
collector.ethRethExchangeRate, prometheus.GaugeValue, exchangeRate)
channel <- prometheus.MustNewConstMetric(
collector.totalValueLockedEth, prometheus.GaugeValue, tvlFloat)
channel <- prometheus.MustNewConstMetric(
collector.rethContractBalance, prometheus.GaugeValue, rETHBalance)
channel <- prometheus.MustNewConstMetric(
collector.totalRethSupply, prometheus.GaugeValue, rethFloat)
}
// Log error messages
func (collector *PerformanceCollector) logError(err error) {
fmt.Printf("[%s] %s\n", collector.logPrefix, err.Error())
}