diff --git a/pkg/rpc/server/prometheus.go b/pkg/rpc/server/prometheus.go index 014973daf..1a494dd70 100644 --- a/pkg/rpc/server/prometheus.go +++ b/pkg/rpc/server/prometheus.go @@ -1,249 +1,63 @@ package server -import "github.com/prometheus/client_golang/prometheus" +import ( + "fmt" + + "github.com/prometheus/client_golang/prometheus" +) // Metrics used in monitoring service. var ( - getapplicationlogCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getapplicationlog rpc endpoint", - Name: "getapplicationlog_called", - Namespace: "neogo", - }, - ) - getbestblockhashCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getbestblockhash rpc endpoint", - Name: "getbestblockhash_called", - Namespace: "neogo", - }, - ) + rpcCalls = []string{ + "getaccountstate", + "getapplicationlog", + "getassetstate", + "getbestblock", + "getbestblockhash", + "getblockcount", + "getblockhash", + "getblockheader", + "getblocksysfee", + "getclaimable", + "getconnectioncount", + "getcontractstate", + "getnep5balances", + "getnep5transfers", + "getpeers", + "getrawmempool", + "getrawtransaction", + "getstorage", + "gettransactionheight", + "gettxout", + "getunclaimed", + "getunspents", + "getvalidators", + "getversion", + "sendrawtransaction", + "submitblock", + "validateaddress", + } - getbestblockCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getbestblock rpc endpoint", - Name: "getbestblock_called", - Namespace: "neogo", - }, - ) - - getblockcountCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getblockcount rpc endpoint", - Name: "getblockcount_called", - Namespace: "neogo", - }, - ) - - getblockHashCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getblockhash rpc endpoint", - Name: "getblockhash_called", - Namespace: "neogo", - }, - ) - - getblockheaderCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getblockheader rpc endpoint", - Name: "getblockheader_called", - Namespace: "neogo", - }, - ) - - getblocksysfeeCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getblocksysfee rpc endpoint", - Name: "getblocksysfee_called", - Namespace: "neogo", - }, - ) - - getclaimableCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getclaimable rpc endpoint", - Name: "getclaimable_called", - Namespace: "neogo", - }, - ) - - getconnectioncountCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getconnectioncount rpc endpoint", - Name: "getconnectioncount_called", - Namespace: "neogo", - }, - ) - - getcontractstateCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getcontractstate rpc endpoint", - Name: "getcontractstate_called", - Namespace: "neogo", - }, - ) - getvalidatorsCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getvalidators rpc endpoint", - Name: "getvalidators_called", - Namespace: "neogo", - }, - ) - - getnep5balancesCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getnep5balances rpc endpoint", - Name: "getnep5balances_called", - Namespace: "neogo", - }, - ) - - getnep5transfersCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getnep5transfers rpc endpoint", - Name: "getnep5transfers_called", - Namespace: "neogo", - }, - ) - - getversionCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getversion rpc endpoint", - Name: "getversion_called", - Namespace: "neogo", - }, - ) - - getpeersCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getpeers rpc endpoint", - Name: "getpeers_called", - Namespace: "neogo", - }, - ) - - getrawmempoolCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getrawmempool rpc endpoint", - Name: "getrawmempool_called", - Namespace: "neogo", - }, - ) - - validateaddressCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to validateaddress rpc endpoint", - Name: "validateaddress_called", - Namespace: "neogo", - }, - ) - - getassetstateCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getassetstate rpc endpoint", - Name: "getassetstate_called", - Namespace: "neogo", - }, - ) - - getaccountstateCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getaccountstate rpc endpoint", - Name: "getaccountstate_called", - Namespace: "neogo", - }, - ) - - gettransactionheightCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to gettransactionheight rpc endpoint", - Name: "gettransactionheight_called", - Namespace: "neogo", - }, - ) - - gettxoutCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to gettxout rpc endpoint", - Name: "gettxout_called", - Namespace: "neogo", - }, - ) - - getrawtransactionCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getrawtransaction rpc endpoint", - Name: "getrawtransaction_called", - Namespace: "neogo", - }, - ) - - getunclaimedCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getunclaimed rpc endpoint", - Name: "getunclaimed_called", - Namespace: "neogo", - }, - ) - - getunspentsCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getunspents rpc endpoint", - Name: "getunspents_called", - Namespace: "neogo", - }, - ) - - sendrawtransactionCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to sendrawtransaction rpc endpoint", - Name: "sendrawtransaction_called", - Namespace: "neogo", - }, - ) - - submitblockCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to submitblock rpc endpoint", - Name: "submitblock_called", - Namespace: "neogo", - }, - ) - - getstorageCalled = prometheus.NewCounter( - prometheus.CounterOpts{ - Help: "Number of calls to getstorage rpc endpoint", - Name: "getstorage_called", - Namespace: "neogo", - }, - ) + rpcCounter = map[string]prometheus.Counter{} ) -func init() { - prometheus.MustRegister( - getapplicationlogCalled, - getbestblockhashCalled, - getbestblockCalled, - getblockcountCalled, - getblockHashCalled, - getblockheaderCalled, - getblocksysfeeCalled, - getconnectioncountCalled, - getcontractstateCalled, - getvalidatorsCalled, - getversionCalled, - getpeersCalled, - getrawmempoolCalled, - validateaddressCalled, - getassetstateCalled, - getaccountstateCalled, - getunclaimedCalled, - getunspentsCalled, - gettransactionheightCalled, - gettxoutCalled, - getrawtransactionCalled, - sendrawtransactionCalled, - submitblockCalled, - getstorageCalled, - ) +func incCounter(name string) { + ctr, ok := rpcCounter[name] + if ok { + ctr.Inc() + } +} + +func init() { + for i := range rpcCalls { + ctr := prometheus.NewCounter( + prometheus.CounterOpts{ + Help: fmt.Sprintf("Number of calls to %s rpc endpoint", rpcCalls[i]), + Name: fmt.Sprintf("%s_called", rpcCalls[i]), + Namespace: "neogo", + }, + ) + prometheus.MustRegister(ctr) + rpcCounter[rpcCalls[i]] = ctr + } } diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index a8291afcf..e14c8fb42 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -153,18 +153,17 @@ func (s *Server) methodHandler(w http.ResponseWriter, req *request.In, reqParams resultsErr error ) + incCounter(req.Method) + Methods: switch req.Method { case "getapplicationlog": - getapplicationlogCalled.Inc() results, resultsErr = s.getApplicationLog(reqParams) case "getbestblockhash": - getbestblockhashCalled.Inc() results = "0x" + s.chain.CurrentBlockHash().StringLE() case "getblock": - getbestblockCalled.Inc() var hash util.Uint256 param, ok := reqParams.Value(0) @@ -208,11 +207,9 @@ Methods: } case "getblockcount": - getblockcountCalled.Inc() results = s.chain.BlockHeight() + 1 case "getblockhash": - getblockHashCalled.Inc() param, ok := reqParams.ValueWithType(0, request.NumberT) if !ok { resultsErr = response.ErrInvalidParams @@ -227,34 +224,26 @@ Methods: results = s.chain.GetHeaderHash(num) case "getblockheader": - getblockheaderCalled.Inc() results, resultsErr = s.getBlockHeader(reqParams) case "getblocksysfee": - getblocksysfeeCalled.Inc() results, resultsErr = s.getBlockSysFee(reqParams) case "getclaimable": - getclaimableCalled.Inc() results, resultsErr = s.getClaimable(reqParams) case "getconnectioncount": - getconnectioncountCalled.Inc() results = s.coreServer.PeerCount() case "getnep5balances": - getnep5balancesCalled.Inc() results, resultsErr = s.getNEP5Balances(reqParams) case "getnep5transfers": - getnep5transfersCalled.Inc() results, resultsErr = s.getNEP5Transfers(reqParams) case "getvalidators": - getvalidatorsCalled.Inc() results, resultsErr = s.getValidators() case "getversion": - getversionCalled.Inc() results = result.Version{ Port: s.coreServer.Port, Nonce: s.coreServer.ID(), @@ -262,7 +251,6 @@ Methods: } case "getpeers": - getpeersCalled.Inc() peers := result.NewGetPeers() peers.AddUnconnected(s.coreServer.UnconnectedPeers()) peers.AddConnected(s.coreServer.ConnectedPeers()) @@ -270,7 +258,6 @@ Methods: results = peers case "getrawmempool": - getrawmempoolCalled.Inc() mp := s.chain.GetMemPool() hashList := make([]util.Uint256, 0) for _, item := range mp.GetVerifiedTransactions() { @@ -279,11 +266,9 @@ Methods: results = hashList case "getstorage": - getstorageCalled.Inc() results, resultsErr = s.getStorage(reqParams) case "validateaddress": - validateaddressCalled.Inc() param, ok := reqParams.Value(0) if !ok { resultsErr = response.ErrInvalidParams @@ -292,7 +277,6 @@ Methods: results = validateAddress(param.Value) case "getassetstate": - getassetstateCalled.Inc() param, ok := reqParams.ValueWithType(0, request.StringT) if !ok { resultsErr = response.ErrInvalidParams @@ -313,31 +297,24 @@ Methods: } case "getaccountstate": - getaccountstateCalled.Inc() results, resultsErr = s.getAccountState(reqParams, false) case "getcontractstate": - getcontractstateCalled.Inc() results, resultsErr = s.getContractState(reqParams) case "getrawtransaction": - getrawtransactionCalled.Inc() results, resultsErr = s.getrawtransaction(reqParams) case "gettransactionheight": - gettransactionheightCalled.Inc() results, resultsErr = s.getTransactionHeight(reqParams) case "gettxout": - gettxoutCalled.Inc() results, resultsErr = s.getTxOut(reqParams) case "getunclaimed": - getunclaimedCalled.Inc() results, resultsErr = s.getUnclaimed(reqParams) case "getunspents": - getunspentsCalled.Inc() results, resultsErr = s.getAccountState(reqParams, true) case "invoke": @@ -350,11 +327,9 @@ Methods: results, resultsErr = s.invokescript(reqParams) case "submitblock": - submitblockCalled.Inc() results, resultsErr = s.submitBlock(reqParams) case "sendrawtransaction": - sendrawtransactionCalled.Inc() results, resultsErr = s.sendrawtransaction(reqParams) default: