1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 08:15:55 -05:00
v2fly/app/stats/command/command.go

137 lines
3.1 KiB
Go
Raw Normal View History

2018-03-31 04:30:12 -04:00
package command
2021-02-16 15:31:50 -05:00
//go:generate go run github.com/v2fly/v2ray-core/v4/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
2021-02-16 15:31:50 -05:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/app/stats"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/strmatcher"
feature_stats "github.com/v2fly/v2ray-core/v4/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) {
mgroup := &strmatcher.MatcherGroup{}
if request.Pattern != "" {
request.Patterns = append(request.Patterns, request.Pattern)
}
t := strmatcher.Substr
if request.Regexp {
t = strmatcher.Regex
}
for _, p := range request.Patterns {
m, err := t.New(p)
if err != nil {
return nil, err
}
mgroup.Add(m)
2018-07-10 17:40:58 -04:00
}
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 {
if mgroup.Size() == 0 || len(mgroup.Match(name)) > 0 {
2018-07-10 17:40:58 -04:00
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
}))
}