add: some useful metrics

This commit is contained in:
Mohamed Sohail 2021-12-14 18:29:30 +03:00
parent 6ce4b88a5a
commit b20ad7c443
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
2 changed files with 65 additions and 10 deletions

View File

@ -10,13 +10,20 @@ var (
RpcClient rpc.RpcClient
)
type BatchResponse struct {
SyncComplete bool
ChainName string
}
type (
BatchHealthResponse struct {
SyncComplete bool
ChainName string
}
func batchCall() (bool, BatchResponse) {
var batchResponse BatchResponse
BatchMetricsResponse struct {
GasPrice string
BlockNumber string
}
)
func batchHealthCall() (bool, BatchHealthResponse) {
var batchResponse BatchHealthResponse
rpcResponses, err := RpcClient.EthBatchRpcCall(jsonrpc.RPCRequests{
// The rpc call id here is guranteed to be in order of appearance starting from index 0
@ -52,3 +59,42 @@ func batchCall() (bool, BatchResponse) {
return true, batchResponse
}
func batchMetricsCall() (bool, BatchMetricsResponse) {
var batchResponse BatchMetricsResponse
rpcResponses, err := RpcClient.EthBatchRpcCall(jsonrpc.RPCRequests{
// 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"),
})
if err != nil {
log.Error().
Err(err).
Str("module", "server").
Msg("error in batch responses")
return false, batchResponse
}
// Order and format isn't guranteed with batch calls therefore we individually process/transform each result in the map
if gasPrice, ok := rpcResponses[0]; ok {
// TODO: decode hex
val, err := gasPrice.GetString()
if err != nil {
return false, batchResponse
}
// If sync is complete the rpc result is false, however this call dones't gurantee all blocks have been imported
batchResponse.GasPrice = val
}
if blockNumber, ok := rpcResponses[1]; ok {
val, err := blockNumber.GetString()
if err != nil {
return false, batchResponse
}
batchResponse.BlockNumber = val
}
return true, batchResponse
}

View File

@ -1,7 +1,6 @@
package server
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
@ -20,9 +19,8 @@ func Start(port string, ginMode string) error {
}
func healthHandler(c *gin.Context) {
allOk, data := batchCall()
allOk, data := batchHealthCall()
fmt.Println(data.SyncComplete)
if !allOk {
// choose a better status code if available
c.JSON(http.StatusExpectationFailed, gin.H{
@ -38,7 +36,18 @@ func healthHandler(c *gin.Context) {
}
func metricsHandler(c *gin.Context) {
allOk, data := batchMetricsCall()
if !allOk {
// choose a better status code if available
c.JSON(http.StatusExpectationFailed, gin.H{
"message": "could not get node metrics",
})
return
}
c.JSON(http.StatusOK, gin.H{
"ok": true,
"gasPrice": data.GasPrice,
"blockNumber": data.BlockNumber,
})
}