status-go/services/rpcstats/stats.go

72 lines
1.5 KiB
Go
Raw Normal View History

2021-03-08 11:18:43 +00:00
package rpcstats
import (
"sync"
"github.com/ethereum/go-ethereum/log"
2021-03-08 11:18:43 +00:00
)
type RPCUsageStats struct {
total uint
counterPerMethod sync.Map
counterPerMethodPerTag sync.Map
2021-03-08 11:18:43 +00:00
}
var stats *RPCUsageStats
func getInstance() *RPCUsageStats {
if stats == nil {
stats = &RPCUsageStats{}
2021-03-08 11:18:43 +00:00
}
return stats
}
func getStats() (uint, sync.Map) {
2021-03-08 11:18:43 +00:00
stats := getInstance()
return stats.total, stats.counterPerMethod
}
// func getStatsWithTag(tag string) (sync.Map, bool) {
// stats := getInstance()
// value, ok := stats.counterPerMethodPerTag.Load(tag)
// return value.(sync.Map), ok
// }
2021-03-08 11:18:43 +00:00
func resetStats() {
stats := getInstance()
stats.total = 0
stats.counterPerMethod = sync.Map{}
stats.counterPerMethodPerTag = sync.Map{}
2021-03-08 11:18:43 +00:00
}
// func resetStatsWithTag(tag string) {
// stats := getInstance()
// stats.counterPerMethodPerTag.Delete(tag)
// }
2021-03-08 11:18:43 +00:00
func CountCall(method string) {
log.Info("CountCall", "method", method)
2021-03-08 11:18:43 +00:00
stats := getInstance()
2021-03-08 11:18:43 +00:00
stats.total++
value, _ := stats.counterPerMethod.LoadOrStore(method, uint(0))
stats.counterPerMethod.Store(method, value.(uint)+1)
}
func CountCallWithTag(method string, tag string) {
if tag == "" {
CountCall(method)
return
}
stats := getInstance()
value, _ := stats.counterPerMethodPerTag.LoadOrStore(tag, sync.Map{})
methodMap := value.(sync.Map)
value, _ = methodMap.LoadOrStore(method, uint(0))
methodMap.Store(method, value.(uint)+1)
log.Info("CountCallWithTag", "method", method, "tag", tag, "count", value.(uint)+1)
CountCall(method)
2021-03-08 11:18:43 +00:00
}