Browse Source

add health check endpoint (#120)

zjubfd 4 years ago
parent
commit
205a28f503
3 changed files with 11 additions and 2 deletions
  1. 9 0
      internal/ethapi/api.go
  2. 1 1
      rpc/handler.go
  3. 1 1
      rpc/metrics.go

+ 9 - 0
internal/ethapi/api.go

@@ -47,6 +47,8 @@ import (
 	"github.com/tyler-smith/go-bip39"
 	"github.com/tyler-smith/go-bip39"
 )
 )
 
 
+const UnHealthyTimeout = 5 * time.Second
+
 // PublicEthereumAPI provides an API to access Ethereum related information.
 // PublicEthereumAPI provides an API to access Ethereum related information.
 // It offers only methods that operate on public data that is freely available to anyone.
 // It offers only methods that operate on public data that is freely available to anyone.
 type PublicEthereumAPI struct {
 type PublicEthereumAPI struct {
@@ -657,6 +659,13 @@ func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, hash common.Ha
 	return nil, err
 	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
 // 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.
 // 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) {
 func (s *PublicBlockChainAPI) GetUncleByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error) {

+ 1 - 1
rpc/handler.go

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

+ 1 - 1
rpc/metrics.go

@@ -26,7 +26,7 @@ var (
 	rpcRequestGauge        = metrics.NewRegisteredGauge("rpc/requests", nil)
 	rpcRequestGauge        = metrics.NewRegisteredGauge("rpc/requests", nil)
 	successfulRequestGauge = metrics.NewRegisteredGauge("rpc/success", nil)
 	successfulRequestGauge = metrics.NewRegisteredGauge("rpc/success", nil)
 	failedReqeustGauge     = metrics.NewRegisteredGauge("rpc/failure", 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 {
 func newRPCServingTimer(method string, valid bool) metrics.Timer {