From 6f40fe71353084c6c1ec8482e1c938fd4a4706f6 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Wed, 4 Dec 2019 12:45:02 -0500 Subject: [PATCH] Added initial chaintip collections --- collector.go | 8 ++++++++ main.go | 28 ++++++++++++++++++++++++++++ rpc.go | 11 +++++++++++ 3 files changed, 47 insertions(+) diff --git a/collector.go b/collector.go index 9fae20f..56e66b8 100644 --- a/collector.go +++ b/collector.go @@ -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) } diff --git a/main.go b/main.go index 07d1b0f..9bb1ed3 100644 --- a/main.go +++ b/main.go @@ -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) + } + +} diff --git a/rpc.go b/rpc.go index 746012d..4269cbe 100644 --- a/rpc.go +++ b/rpc.go @@ -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"` +}