add health check endpoint (#120)

This commit is contained in:
zjubfd 2021-03-22 14:57:57 +08:00 committed by GitHub
parent 7a1262ca14
commit 205a28f503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

@ -47,6 +47,8 @@ import (
"github.com/tyler-smith/go-bip39"
)
const UnHealthyTimeout = 5 * time.Second
// PublicEthereumAPI provides an API to access Ethereum related information.
// It offers only methods that operate on public data that is freely available to anyone.
type PublicEthereumAPI struct {
@ -657,6 +659,13 @@ func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, hash common.Ha
return nil, err
}
func (s *PublicBlockChainAPI) Health() bool {
if rpc.RpcServingTimer != nil {
return rpc.RpcServingTimer.Percentile(0.75) < float64(UnHealthyTimeout)
}
return true
}
// GetUncleByBlockNumberAndIndex returns the uncle block for the given block hash and index. When fullTx is true
// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
func (s *PublicBlockChainAPI) GetUncleByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error) {

@ -340,7 +340,7 @@ func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage
} else {
successfulRequestGauge.Inc(1)
}
rpcServingTimer.UpdateSince(start)
RpcServingTimer.UpdateSince(start)
newRPCRequestGauge(msg.Method).Inc(1)
newRPCServingTimer(msg.Method, answer.Error == nil).UpdateSince(start)
}

@ -26,7 +26,7 @@ var (
rpcRequestGauge = metrics.NewRegisteredGauge("rpc/requests", nil)
successfulRequestGauge = metrics.NewRegisteredGauge("rpc/success", nil)
failedReqeustGauge = metrics.NewRegisteredGauge("rpc/failure", nil)
rpcServingTimer = metrics.NewRegisteredTimer("rpc/duration/all", nil)
RpcServingTimer = metrics.NewRegisteredTimer("rpc/duration/all", nil)
)
func newRPCServingTimer(method string, valid bool) metrics.Timer {