1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/app/stats/command/command.go

128 lines
2.8 KiB
Go
Raw Normal View History

2019-02-01 14:08:21 -05:00
// +build !confonly
2018-03-31 04:30:12 -04:00
package command
//go:generate go run v2ray.com/core/common/errors/errorgen
2018-03-31 04:30:12 -04:00
import (
"context"
2019-07-08 02:12:17 -04:00
"runtime"
"time"
2018-03-31 04:30:12 -04:00
grpc "google.golang.org/grpc"
2018-07-10 17:40:58 -04:00
2018-03-31 04:30:12 -04:00
"v2ray.com/core"
2018-07-10 17:40:58 -04:00
"v2ray.com/core/app/stats"
2018-03-31 04:30:12 -04:00
"v2ray.com/core/common"
2018-07-10 17:40:58 -04:00
"v2ray.com/core/common/strmatcher"
feature_stats "v2ray.com/core/features/stats"
2018-03-31 04:30:12 -04:00
)
2018-07-10 17:40:58 -04:00
// statsServer is an implementation of StatsService.
2018-03-31 04:30:12 -04:00
type statsServer struct {
2019-07-08 02:12:17 -04:00
stats feature_stats.Manager
startTime time.Time
2018-03-31 04:30:12 -04:00
}
func NewStatsServer(manager feature_stats.Manager) StatsServiceServer {
2019-07-08 02:12:17 -04:00
return &statsServer{
stats: manager,
startTime: time.Now(),
}
2018-07-10 17:40:58 -04:00
}
2018-03-31 04:30:12 -04:00
func (s *statsServer) GetStats(ctx context.Context, request *GetStatsRequest) (*GetStatsResponse, error) {
c := s.stats.GetCounter(request.Name)
if c == nil {
return nil, newError(request.Name, " not found.")
}
var value int64
if request.Reset_ {
value = c.Set(0)
} else {
value = c.Value()
}
return &GetStatsResponse{
Stat: &Stat{
Name: request.Name,
Value: value,
},
}, nil
}
2018-07-10 17:40:58 -04:00
func (s *statsServer) QueryStats(ctx context.Context, request *QueryStatsRequest) (*QueryStatsResponse, error) {
matcher, err := strmatcher.Substr.New(request.Pattern)
if err != nil {
return nil, err
}
response := &QueryStatsResponse{}
manager, ok := s.stats.(*stats.Manager)
if !ok {
return nil, newError("QueryStats only works its own stats.Manager.")
}
manager.VisitCounters(func(name string, c feature_stats.Counter) bool {
2018-07-10 17:40:58 -04:00
if matcher.Match(name) {
var value int64
if request.Reset_ {
value = c.Set(0)
} else {
value = c.Value()
}
response.Stat = append(response.Stat, &Stat{
Name: name,
Value: value,
})
}
return true
})
return response, nil
}
2019-07-08 02:12:17 -04:00
func (s *statsServer) GetSysStats(ctx context.Context, request *SysStatsRequest) (*SysStatsResponse, error) {
var rtm runtime.MemStats
runtime.ReadMemStats(&rtm)
uptime := time.Since(s.startTime)
response := &SysStatsResponse{
Uptime: uint32(uptime.Seconds()),
NumGoroutine: uint32(runtime.NumGoroutine()),
Alloc: rtm.Alloc,
TotalAlloc: rtm.TotalAlloc,
Sys: rtm.Sys,
Mallocs: rtm.Mallocs,
Frees: rtm.Frees,
LiveObjects: rtm.Mallocs - rtm.Frees,
NumGC: rtm.NumGC,
PauseTotalNs: rtm.PauseTotalNs,
}
return response, nil
}
func (s *statsServer) mustEmbedUnimplementedStatsServiceServer() {}
2018-03-31 04:30:12 -04:00
type service struct {
2018-10-22 05:26:22 -04:00
statsManager feature_stats.Manager
2018-03-31 04:30:12 -04:00
}
func (s *service) Register(server *grpc.Server) {
2018-10-22 05:26:22 -04:00
RegisterStatsServiceServer(server, NewStatsServer(s.statsManager))
2018-03-31 04:30:12 -04:00
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
2018-10-22 05:26:22 -04:00
s := new(service)
core.RequireFeatures(ctx, func(sm feature_stats.Manager) {
s.statsManager = sm
})
return s, nil
2018-03-31 04:30:12 -04:00
}))
}