metrics: add pending tx and peer count

This commit is contained in:
Mohamed Sohail 2021-12-15 12:57:47 +03:00
parent 1f43c3410a
commit c50906ede1
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
2 changed files with 49 additions and 6 deletions

View File

@ -20,6 +20,16 @@ type (
BatchMetricsResponse struct {
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
}

View File

@ -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,7 +40,7 @@ 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
@ -49,5 +49,9 @@ func metricsHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"gasPrice": data.GasPrice,
"blockNumber": data.BlockNumber,
"peerCount": data.PeerCount,
"pendingTransactionsCount": data.PendingTxCount,
"pendingTransactions": data.PendingTx,
"enode": data.Enode,
})
}