2022-07-21 13:21:44 +00:00
|
|
|
package rpcsrv
|
2019-10-29 17:51:17 +00:00
|
|
|
|
2020-03-12 17:36:36 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-09 10:26:45 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-03-12 17:36:36 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
2019-10-29 17:51:17 +00:00
|
|
|
|
|
|
|
// Metrics used in monitoring service.
|
2022-11-09 10:26:45 +00:00
|
|
|
var (
|
|
|
|
rpcCounter = map[string]prometheus.Counter{}
|
|
|
|
rpcTimes = map[string]prometheus.Histogram{}
|
|
|
|
)
|
2019-10-29 17:51:17 +00:00
|
|
|
|
2022-11-09 10:26:45 +00:00
|
|
|
func addReqTimeMetric(name string, t time.Duration) {
|
|
|
|
hist, ok := rpcTimes[name]
|
|
|
|
if ok {
|
|
|
|
hist.Observe(t.Seconds())
|
|
|
|
}
|
2020-03-12 17:36:36 +00:00
|
|
|
ctr, ok := rpcCounter[name]
|
|
|
|
if ok {
|
|
|
|
ctr.Inc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 20:18:00 +00:00
|
|
|
func regCounter(call string) {
|
|
|
|
ctr := prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
2022-11-09 10:26:45 +00:00
|
|
|
Help: fmt.Sprintf("Number of calls to %s rpc endpoint (obsolete, to be removed)", call),
|
2022-03-21 20:18:00 +00:00
|
|
|
Name: fmt.Sprintf("%s_called", call),
|
|
|
|
Namespace: "neogo",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
prometheus.MustRegister(ctr)
|
|
|
|
rpcCounter[call] = ctr
|
2022-11-09 10:26:45 +00:00
|
|
|
rpcTimes[call] = prometheus.NewHistogram(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Help: "RPC " + call + " call handling time",
|
|
|
|
Name: "rpc_" + strings.ToLower(call) + "_time",
|
|
|
|
Namespace: "neogo",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
prometheus.MustRegister(rpcTimes[call])
|
2022-03-21 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 17:51:17 +00:00
|
|
|
func init() {
|
2020-03-25 12:19:12 +00:00
|
|
|
for call := range rpcHandlers {
|
2022-03-21 20:18:00 +00:00
|
|
|
regCounter(call)
|
|
|
|
}
|
|
|
|
for call := range rpcWsHandlers {
|
|
|
|
regCounter(call)
|
2020-03-12 17:36:36 +00:00
|
|
|
}
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|