2021-03-08 11:18:43 +00:00
|
|
|
package rpcstats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-05-22 10:16:06 +00:00
|
|
|
"sync"
|
2021-03-08 11:18:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PublicAPI represents a set of APIs from the namespace.
|
|
|
|
type PublicAPI struct {
|
|
|
|
s *Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPI creates an instance of the API.
|
|
|
|
func NewAPI(s *Service) *PublicAPI {
|
|
|
|
return &PublicAPI{s: s}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets RPC usage stats
|
|
|
|
func (api *PublicAPI) Reset(context context.Context) {
|
|
|
|
resetStats()
|
|
|
|
}
|
|
|
|
|
|
|
|
type RPCStats struct {
|
|
|
|
Total uint `json:"total"`
|
|
|
|
CounterPerMethod map[string]uint `json:"methods"`
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
// GetStats returns RPC usage stats
|
2021-03-08 11:18:43 +00:00
|
|
|
func (api *PublicAPI) GetStats(context context.Context) (RPCStats, error) {
|
2024-05-22 10:16:06 +00:00
|
|
|
total, perMethod, perMethodPerTag := getStats()
|
2024-05-20 11:21:21 +00:00
|
|
|
|
|
|
|
counterPerMethod := make(map[string]uint)
|
|
|
|
perMethod.Range(func(key, value interface{}) bool {
|
|
|
|
counterPerMethod[key.(string)] = value.(uint)
|
|
|
|
return true
|
|
|
|
})
|
2024-05-22 10:16:06 +00:00
|
|
|
perMethodPerTag.Range(func(key, value interface{}) bool {
|
|
|
|
tag := key.(string)
|
|
|
|
methods := value.(*sync.Map)
|
|
|
|
methods.Range(func(key, value interface{}) bool {
|
|
|
|
method := key.(string)
|
|
|
|
count := value.(uint)
|
|
|
|
counterPerMethod[method+"_"+tag] = count
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
})
|
2024-05-20 11:21:21 +00:00
|
|
|
|
2021-03-08 11:18:43 +00:00
|
|
|
return RPCStats{
|
|
|
|
Total: total,
|
2024-05-20 11:21:21 +00:00
|
|
|
CounterPerMethod: counterPerMethod,
|
2021-03-08 11:18:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|