2021-03-08 11:18:43 +00:00
|
|
|
package rpcstats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RPCUsageStats struct {
|
2024-05-20 11:21:21 +00:00
|
|
|
total uint
|
2024-05-22 10:16:06 +00:00
|
|
|
counterPerMethod *sync.Map
|
|
|
|
counterPerMethodPerTag *sync.Map
|
2021-03-08 11:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var stats *RPCUsageStats
|
|
|
|
|
|
|
|
func getInstance() *RPCUsageStats {
|
|
|
|
if stats == nil {
|
2024-05-20 11:21:21 +00:00
|
|
|
stats = &RPCUsageStats{}
|
2024-05-22 10:16:06 +00:00
|
|
|
stats.counterPerMethod = &sync.Map{}
|
|
|
|
stats.counterPerMethodPerTag = &sync.Map{}
|
2021-03-08 11:18:43 +00:00
|
|
|
}
|
|
|
|
return stats
|
|
|
|
}
|
|
|
|
|
2024-05-22 10:16:06 +00:00
|
|
|
func getStats() (uint, *sync.Map, *sync.Map) {
|
2021-03-08 11:18:43 +00:00
|
|
|
stats := getInstance()
|
2024-05-22 10:16:06 +00:00
|
|
|
return stats.total, stats.counterPerMethod, stats.counterPerMethodPerTag
|
2021-03-08 11:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func resetStats() {
|
|
|
|
stats := getInstance()
|
|
|
|
stats.total = 0
|
2024-05-22 10:16:06 +00:00
|
|
|
stats.counterPerMethod = &sync.Map{}
|
|
|
|
stats.counterPerMethodPerTag = &sync.Map{}
|
2021-03-08 11:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CountCall(method string) {
|
2024-05-20 11:21:21 +00:00
|
|
|
stats := getInstance()
|
2021-03-08 11:18:43 +00:00
|
|
|
stats.total++
|
2024-05-20 11:21:21 +00:00
|
|
|
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()
|
2024-05-22 10:16:06 +00:00
|
|
|
value, _ := stats.counterPerMethodPerTag.LoadOrStore(tag, &sync.Map{})
|
|
|
|
methodMap := value.(*sync.Map)
|
2024-05-20 11:21:21 +00:00
|
|
|
value, _ = methodMap.LoadOrStore(method, uint(0))
|
|
|
|
methodMap.Store(method, value.(uint)+1)
|
2024-05-23 07:16:42 +00:00
|
|
|
stats.total++
|
2021-03-08 11:18:43 +00:00
|
|
|
}
|