Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R4R]add health check endpoint #120

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down