forked from 31z4/ethereum-prometheus-exporter
/
eth_height_difference.go
78 lines (68 loc) · 1.93 KB
/
eth_height_difference.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
package collector
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/prometheus/client_golang/prometheus"
)
type EthHeightDiff struct {
rpc *rpc.Client
desc *prometheus.Desc
}
type RPC2 struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id"`
Result string `json:"result"`
}
func NewEthHeightDiff(rpc *rpc.Client) *EthHeightDiff {
return &EthHeightDiff{
rpc: rpc,
desc: prometheus.NewDesc(
"eth_block_height_diff",
"The difference between current node's block height and external source (Etherscan)",
nil,
nil,
),
}
}
func (collector *EthHeightDiff) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.desc
}
func (collector *EthHeightDiff) Collect(ch chan<- prometheus.Metric) {
var our_height hexutil.Uint64
if err := collector.rpc.Call(&our_height, "eth_blockNumber"); err != nil {
ch <- prometheus.NewInvalidMetric(collector.desc, err)
return
}
// etherscan RPC2.0 over http (hex)
var etherscan_height int64
respo, err := http.Get("https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey=" + os.Getenv("ETHERSCAN_TOKEN"))
if err != nil || respo.StatusCode != 200 {
fmt.Println("Error getting etherscan result", err)
} else {
defer respo.Body.Close()
cont, err := ioutil.ReadAll(respo.Body)
if err != nil {
fmt.Println("Reading etherscan response body failed")
}
stri := string(cont)
ress := strings.NewReader(stri)
var ee RPC2
err = json.NewDecoder(ress).Decode(&ee)
if err != nil {
fmt.Println("Failed to decode etherscan json response")
}
etherscan_height, err = strconv.ParseInt(ee.Result, 0, 64)
if err != nil {
fmt.Println("Failed to parse etherscan hex into integer")
}
}
value := float64(etherscan_height) - float64(our_height)
ch <- prometheus.MustNewConstMetric(collector.desc, prometheus.GaugeValue, value)
}