mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-16 17:38:45 -05:00
90 lines
1.9 KiB
Go
90 lines
1.9 KiB
Go
package command
|
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg command -path App,Stats,Command
|
|
|
|
import (
|
|
"context"
|
|
|
|
grpc "google.golang.org/grpc"
|
|
|
|
"v2ray.com/core"
|
|
"v2ray.com/core/app/stats"
|
|
"v2ray.com/core/common"
|
|
"v2ray.com/core/common/strmatcher"
|
|
)
|
|
|
|
// statsServer is an implementation of StatsService.
|
|
type statsServer struct {
|
|
stats core.StatManager
|
|
}
|
|
|
|
func NewStatsServer(manager core.StatManager) StatsServiceServer {
|
|
return &statsServer{stats: manager}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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.Visit(func(name string, c core.StatCounter) bool {
|
|
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
|
|
}
|
|
|
|
type service struct {
|
|
v *core.Instance
|
|
}
|
|
|
|
func (s *service) Register(server *grpc.Server) {
|
|
RegisterStatsServiceServer(server, NewStatsServer(s.v.Stats()))
|
|
}
|
|
|
|
func init() {
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
|
s := core.MustFromContext(ctx)
|
|
return &service{v: s}, nil
|
|
}))
|
|
}
|