rpc: reuse handlers map for prometheus metrics

This commit is contained in:
Evgenii Stratonikov 2020-03-25 15:19:12 +03:00
parent 57f37bc7a0
commit 43495a49f4

View file

@ -7,42 +7,7 @@ import (
) )
// Metrics used in monitoring service. // Metrics used in monitoring service.
var ( var rpcCounter = map[string]prometheus.Counter{}
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",
"invoke",
"invokefunction",
"invokescript",
"sendrawtransaction",
"submitblock",
"validateaddress",
}
rpcCounter = map[string]prometheus.Counter{}
)
func incCounter(name string) { func incCounter(name string) {
ctr, ok := rpcCounter[name] ctr, ok := rpcCounter[name]
@ -52,15 +17,15 @@ func incCounter(name string) {
} }
func init() { func init() {
for i := range rpcCalls { for call := range rpcHandlers {
ctr := prometheus.NewCounter( ctr := prometheus.NewCounter(
prometheus.CounterOpts{ prometheus.CounterOpts{
Help: fmt.Sprintf("Number of calls to %s rpc endpoint", rpcCalls[i]), Help: fmt.Sprintf("Number of calls to %s rpc endpoint", call),
Name: fmt.Sprintf("%s_called", rpcCalls[i]), Name: fmt.Sprintf("%s_called", call),
Namespace: "neogo", Namespace: "neogo",
}, },
) )
prometheus.MustRegister(ctr) prometheus.MustRegister(ctr)
rpcCounter[rpcCalls[i]] = ctr rpcCounter[call] = ctr
} }
} }