Added initial chaintip collections

This commit is contained in:
Ben Wilson 2019-12-04 12:45:02 -05:00
parent e43beec110
commit 6f40fe7135
3 changed files with 47 additions and 0 deletions

View File

@ -52,6 +52,13 @@ var (
Help: "Bytes received from peer node."},
[]string{"addr", "addrlocal", "inbound", "banscore", "subver"},
)
zcashdChainTips = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "zcash_chainips_at_height",
Help: "Information about all known tips in the block tree.",
},
[]string{"hash", "branchlen", "status"},
)
)
// ZCASH_PEERS = Gauge("zcash_peers", "Number of peers")
@ -90,4 +97,5 @@ func init() {
prometheus.MustRegister(zcashdPeerConnTime)
prometheus.MustRegister(zcashdPeerBytesSent)
prometheus.MustRegister(zcashdPeerBytesRecv)
prometheus.MustRegister(zcashdChainTips)
}

28
main.go
View File

@ -56,6 +56,7 @@ func main() {
go getMemPoolInfo()
go getWalletInfo()
go getPeerInfo()
go getChainTips()
log.Infoln("Listening on", *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
log.Fatal(err)
@ -205,3 +206,30 @@ func getPeerInfo() {
}
}
func getChainTips() {
basicAuth := base64.StdEncoding.EncodeToString([]byte(*rpcUser + ":" + *rpcPassword))
rpcClient := jsonrpc.NewClientWithOpts("http://"+*rpcHost+":"+*rpcPort,
&jsonrpc.RPCClientOpts{
CustomHeaders: map[string]string{
"Authorization": "Basic " + basicAuth,
}})
var chaintips *GetChainTips
for {
if err := rpcClient.CallFor(&chaintips, "getchaintips"); err != nil {
log.Warnln("Error calling getchaintips", err)
} else {
for _, ct := range *chaintips {
log.Infoln("Got chaintip: ", ct.Hash)
zcashdChainTips.WithLabelValues(
ct.Hash,
strconv.Itoa(ct.Branchlen),
ct.Status,
).Set(float64(ct.Height))
}
}
time.Sleep(time.Duration(30) * time.Second)
}
}

11
rpc.go
View File

@ -51,3 +51,14 @@ type PeerInfo struct {
SyncedHeaders int `json:"synced_headers"`
SyncedBlocks int `json:"synced_blocks"`
}
// GetChainTips Return information about all known tips in the block tree
// https://zcash-rpc.github.io/getchaintips.html
type GetChainTips []ChainTip
type ChainTip struct {
Height int `json:"height"`
Hash string `json:"hash"`
Branchlen int `json:"branchlen"`
Status string `json:"status"`
}