add: some useful metrics
This commit is contained in:
parent
6ce4b88a5a
commit
b20ad7c443
@ -10,13 +10,20 @@ var (
|
|||||||
RpcClient rpc.RpcClient
|
RpcClient rpc.RpcClient
|
||||||
)
|
)
|
||||||
|
|
||||||
type BatchResponse struct {
|
type (
|
||||||
SyncComplete bool
|
BatchHealthResponse struct {
|
||||||
ChainName string
|
SyncComplete bool
|
||||||
}
|
ChainName string
|
||||||
|
}
|
||||||
|
|
||||||
func batchCall() (bool, BatchResponse) {
|
BatchMetricsResponse struct {
|
||||||
var batchResponse BatchResponse
|
GasPrice string
|
||||||
|
BlockNumber string
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func batchHealthCall() (bool, BatchHealthResponse) {
|
||||||
|
var batchResponse BatchHealthResponse
|
||||||
|
|
||||||
rpcResponses, err := RpcClient.EthBatchRpcCall(jsonrpc.RPCRequests{
|
rpcResponses, err := RpcClient.EthBatchRpcCall(jsonrpc.RPCRequests{
|
||||||
// The rpc call id here is guranteed to be in order of appearance starting from index 0
|
// 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
|
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
|
||||||
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -20,9 +19,8 @@ func Start(port string, ginMode string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func healthHandler(c *gin.Context) {
|
func healthHandler(c *gin.Context) {
|
||||||
allOk, data := batchCall()
|
allOk, data := batchHealthCall()
|
||||||
|
|
||||||
fmt.Println(data.SyncComplete)
|
|
||||||
if !allOk {
|
if !allOk {
|
||||||
// choose a better status code if available
|
// choose a better status code if available
|
||||||
c.JSON(http.StatusExpectationFailed, gin.H{
|
c.JSON(http.StatusExpectationFailed, gin.H{
|
||||||
@ -38,7 +36,18 @@ func healthHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func metricsHandler(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{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"ok": true,
|
"gasPrice": data.GasPrice,
|
||||||
|
"blockNumber": data.BlockNumber,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user