Merge pull request #3 from benzcash/ben/add-chaintips

Addded metrics for getchaintips
This commit is contained in:
Ben Wilson 2019-12-04 08:19:02 -05:00 committed by GitHub
commit bd4165ab0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions

View File

@ -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)
}

28
main.go
View File

@ -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)
}
}

11
rpc.go
View File

@ -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"`
}