/
smoothing-pool-collector.go
67 lines (54 loc) · 1.95 KB
/
smoothing-pool-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
package collectors
import (
"fmt"
"github.com/Seb369888/poolsea-go/rocketpool"
"github.com/Seb369888/poolsea-go/utils/eth"
"github.com/Seb369888/smartnode/shared/services"
"github.com/prometheus/client_golang/prometheus"
)
// Represents the collector for Smoothing Pool metrics
type SmoothingPoolCollector struct {
// the ETH balance on the smoothing pool
ethBalanceOnSmoothingPool *prometheus.Desc
// The Rocket Pool contract manager
rp *rocketpool.RocketPool
// The EC client
ec *services.ExecutionClientManager
// The thread-safe locker for the network state
stateLocker *StateLocker
// Prefix for logging
logPrefix string
}
// Create a new SmoothingPoolCollector instance
func NewSmoothingPoolCollector(rp *rocketpool.RocketPool, ec *services.ExecutionClientManager, stateLocker *StateLocker) *SmoothingPoolCollector {
subsystem := "smoothing_pool"
return &SmoothingPoolCollector{
ethBalanceOnSmoothingPool: prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystem, "eth_balance"),
"The ETH balance on the smoothing pool",
nil, nil,
),
rp: rp,
ec: ec,
stateLocker: stateLocker,
logPrefix: "SP Collector",
}
}
// Write metric descriptions to the Prometheus channel
func (collector *SmoothingPoolCollector) Describe(channel chan<- *prometheus.Desc) {
channel <- collector.ethBalanceOnSmoothingPool
}
// Collect the latest metric values and pass them to Prometheus
func (collector *SmoothingPoolCollector) Collect(channel chan<- prometheus.Metric) {
// Get the latest state
state := collector.stateLocker.GetState()
if state == nil {
return
}
ethBalanceOnSmoothingPool := eth.WeiToEth(state.NetworkDetails.SmoothingPoolBalance)
channel <- prometheus.MustNewConstMetric(
collector.ethBalanceOnSmoothingPool, prometheus.GaugeValue, ethBalanceOnSmoothingPool)
}
// Log error messages
func (collector *SmoothingPoolCollector) logError(err error) {
fmt.Printf("[%s] %s\n", collector.logPrefix, err.Error())
}