-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
85 lines (73 loc) · 1.88 KB
/
main.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
package main
import (
"bytes"
"encoding/hex"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
tmhttp "github.com/tendermint/tendermint/rpc/client/http"
"log"
"net/http"
"os"
"time"
)
var (
valAddrHex = os.Getenv("ADDRESS")
rpcHost = os.Getenv("RPC")
lAddr = os.Getenv("LADDR")
)
func main() {
go pollRoutine()
http.Handle("/metrics", promhttp.Handler())
panic(http.ListenAndServe(lAddr, nil))
}
func pollRoutine() {
c, err := tmhttp.NewWithClient(rpcHost, "/ws", http.DefaultClient)
if err != nil {
log.Fatalf("failed to create rpc client: %v", err)
}
missCounter := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "tm",
Subsystem: "mon",
Name: "misses_count",
Help: "Amount of missed blocks since exporter start",
ConstLabels: nil,
})
prometheus.MustRegister(missCounter)
heightGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "tm",
Subsystem: "mon",
Name: "height",
Help: "Current block height that the monitor has processed",
ConstLabels: nil,
})
prometheus.MustRegister(heightGauge)
validatorAddress, err := hex.DecodeString(valAddrHex)
if err != nil {
panic(err)
}
ticker := time.NewTicker(time.Second)
var lastHeight int64
OUTER:
for {
select {
case <-ticker.C:
res, err := c.Block(nil)
if err != nil {
log.Printf("block could not be requested; err=%v\n", err)
continue
}
if res.Block.Height > lastHeight {
lastHeight = res.Block.Height
heightGauge.Set(float64(lastHeight))
for _, pre := range res.Block.LastCommit.Signatures {
if bytes.Equal(validatorAddress, pre.ValidatorAddress) && pre.ForBlock() {
// We signed and the vote is not zero (nil votes also count as downtime)
continue OUTER
}
}
// Seems like we missed the block
missCounter.Inc()
}
}
}
}