From c50906ede1a4c4ae441b8794972edad14b0c3a3a Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Wed, 15 Dec 2021 12:57:47 +0300 Subject: [PATCH] metrics: add pending tx and peer count --- internal/server/handlers.go | 43 +++++++++++++++++++++++++++++++++++-- internal/server/router.go | 12 +++++++---- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 6a3dbc6..92bc421 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -18,8 +18,18 @@ type ( } BatchMetricsResponse struct { - GasPrice uint64 - BlockNumber uint64 + GasPrice uint64 + BlockNumber uint64 + PeerCount uint64 + PendingTx []pendingTx + PendingTxCount int + Enode string + } + + pendingTx struct { + Hash string `json:"hash"` + From string `json:"from"` + To string `json:"to"` } ) @@ -68,6 +78,9 @@ func batchMetricsCall() (bool, BatchMetricsResponse) { // The rpc call id here is guranteed to be in order of appearance starting from index 0 jsonrpc.NewRequest("eth_gasPrice"), jsonrpc.NewRequest("eth_blockNumber"), + jsonrpc.NewRequest("net_peerCount"), + jsonrpc.NewRequest("parity_pendingTransactions"), + jsonrpc.NewRequest("parity_enode"), }) if err != nil { @@ -97,5 +110,31 @@ func batchMetricsCall() (bool, BatchMetricsResponse) { batchResponse.BlockNumber = util.Hex2Int(val) } + if peerCount, ok := rpcResponses[2]; ok { + val, err := peerCount.GetString() + if err != nil { + return false, batchResponse + } + batchResponse.PeerCount = util.Hex2Int(val) + } + + if pendingTransactionsArray, ok := rpcResponses[3]; ok { + var pendingTransactions = []pendingTx{} + err := pendingTransactionsArray.GetObject(&pendingTransactions) + if err != nil { + return false, batchResponse + } + batchResponse.PendingTxCount = len(pendingTransactions) + batchResponse.PendingTx = pendingTransactions + } + + if enode, ok := rpcResponses[4]; ok { + val, err := enode.GetString() + if err != nil { + return false, batchResponse + } + batchResponse.Enode = val + } + return true, batchResponse } diff --git a/internal/server/router.go b/internal/server/router.go index fd65c86..3d6fe91 100644 --- a/internal/server/router.go +++ b/internal/server/router.go @@ -23,7 +23,7 @@ func healthHandler(c *gin.Context) { if !allOk { // choose a better status code if available - c.JSON(http.StatusExpectationFailed, gin.H{ + c.JSON(http.StatusServiceUnavailable, gin.H{ "message": "could not get node health status", }) return @@ -40,14 +40,18 @@ func metricsHandler(c *gin.Context) { if !allOk { // choose a better status code if available - c.JSON(http.StatusExpectationFailed, gin.H{ + c.JSON(http.StatusServiceUnavailable, gin.H{ "message": "could not get node metrics", }) return } c.JSON(http.StatusOK, gin.H{ - "gasPrice": data.GasPrice, - "blockNumber": data.BlockNumber, + "gasPrice": data.GasPrice, + "blockNumber": data.BlockNumber, + "peerCount": data.PeerCount, + "pendingTransactionsCount": data.PendingTxCount, + "pendingTransactions": data.PendingTx, + "enode": data.Enode, }) }