/
eth_hashrate.go
39 lines (33 loc) · 894 Bytes
/
eth_hashrate.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
package collector
import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/prometheus/client_golang/prometheus"
)
type EthHashrate struct {
rpc *rpc.Client
desc *prometheus.Desc
}
func NewEthHashrate(rpc *rpc.Client) *EthHashrate {
return &EthHashrate{
rpc: rpc,
desc: prometheus.NewDesc(
"eth_hashrate",
"hashes per second that this node is mining with",
nil,
nil,
),
}
}
func (collector *EthHashrate) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.desc
}
func (collector *EthHashrate) Collect(ch chan<- prometheus.Metric) {
var result hexutil.Uint64
if err := collector.rpc.Call(&result, "eth_hashrate"); err != nil {
ch <- prometheus.NewInvalidMetric(collector.desc, err)
return
}
value := float64(result)
ch <- prometheus.MustNewConstMetric(collector.desc, prometheus.GaugeValue, value)
}