From 3c8aef1cb00d4cde72c5da6e8bbda464c14afa34 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Thu, 14 Nov 2019 12:22:22 -0500 Subject: [PATCH] Addded metrics for getchaintips --- collector.go | 7 +++++++ main.go | 28 ++++++++++++++++++++++++++++ rpc.go | 11 +++++++++++ 3 files changed, 46 insertions(+) diff --git a/collector.go b/collector.go index cd989c4..d5d0932 100644 --- a/collector.go +++ b/collector.go @@ -27,6 +27,12 @@ var ( []string{ "type", }) + zcashdChainTips = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "zcash_chain_tip", + Help: "Return information about all known tips in the block tree, including the main chain as well as orphaned branches."}, + []string{"height", "hash", "branchlen", "status"}, + ) ) // ZCASH_PEERS = Gauge("zcash_peers", "Number of peers") @@ -61,4 +67,5 @@ func init() { prometheus.MustRegister(zcashdMemPoolBytes) prometheus.MustRegister(zcashdMemPoolUsage) prometheus.MustRegister(zcashdWalletBalance) + prometheus.MustRegister(zcashdChainTips) } diff --git a/main.go b/main.go index 25065cc..fb6545f 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,7 @@ func main() { go getBlockchainInfo() go getMemPoolInfo() go getWalletInfo() + go getChainTips() log.Infoln("Listening on", *listenAddress) if err := http.ListenAndServe(*listenAddress, nil); err != nil { log.Fatal(err) @@ -133,3 +134,30 @@ func getWalletInfo() { } } + +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.Height) + zcashdChainTips.WithLabelValues( + strconv.FormatFloat(ct.Height, 'f', 2, 64), + ct.Hash, + strconv.FormatFloat(ct.Branchlen, 'f', 2, 64), + ct.Status).Set(ct.Height) + } + } + time.Sleep(time.Duration(30) * time.Second) + } + +} diff --git a/rpc.go b/rpc.go index 4ea6496..e6d517a 100644 --- a/rpc.go +++ b/rpc.go @@ -25,3 +25,14 @@ type ZGetTotalBalance struct { Private string `json:"private"` Total string `json:"total"` } + +// GetChainTips Return information about all known tips in the block tree, including the main chain as well as orphaned branches. +// https://zcash-rpc.github.io/getchaintips.html +type GetChainTips []ChainTip + +type ChainTip struct { + Height float64 `json:"height"` + Hash string `json:"hash"` + Branchlen float64 `json:"branchlen"` + Status string `json:"status"` +}