Skip to content

Commit b288b3c

Browse files
committed
grpc for QueryStats
1 parent 7efa7ee commit b288b3c

File tree

5 files changed

+314
-31
lines changed

5 files changed

+314
-31
lines changed

app/stats/command/command.go

+41-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ import (
66
"context"
77

88
grpc "google.golang.org/grpc"
9+
910
"v2ray.com/core"
11+
"v2ray.com/core/app/stats"
1012
"v2ray.com/core/common"
13+
"v2ray.com/core/common/strmatcher"
1114
)
1215

16+
// statsServer is an implementation of StatsService.
1317
type statsServer struct {
1418
stats core.StatManager
1519
}
1620

21+
func NewStatsServer(manager core.StatManager) StatsServiceServer {
22+
return &statsServer{stats: manager}
23+
}
24+
1725
func (s *statsServer) GetStats(ctx context.Context, request *GetStatsRequest) (*GetStatsResponse, error) {
1826
c := s.stats.GetCounter(request.Name)
1927
if c == nil {
@@ -33,14 +41,44 @@ func (s *statsServer) GetStats(ctx context.Context, request *GetStatsRequest) (*
3341
}, nil
3442
}
3543

44+
func (s *statsServer) QueryStats(ctx context.Context, request *QueryStatsRequest) (*QueryStatsResponse, error) {
45+
matcher, err := strmatcher.Substr.New(request.Pattern)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
response := &QueryStatsResponse{}
51+
52+
manager, ok := s.stats.(*stats.Manager)
53+
if !ok {
54+
return nil, newError("QueryStats only works its own stats.Manager.")
55+
}
56+
57+
manager.Visit(func(name string, c core.StatCounter) bool {
58+
if matcher.Match(name) {
59+
var value int64
60+
if request.Reset_ {
61+
value = c.Set(0)
62+
} else {
63+
value = c.Value()
64+
}
65+
response.Stat = append(response.Stat, &Stat{
66+
Name: name,
67+
Value: value,
68+
})
69+
}
70+
return true
71+
})
72+
73+
return response, nil
74+
}
75+
3676
type service struct {
3777
v *core.Instance
3878
}
3979

4080
func (s *service) Register(server *grpc.Server) {
41-
RegisterStatsServiceServer(server, &statsServer{
42-
stats: s.v.Stats(),
43-
})
81+
RegisterStatsServiceServer(server, NewStatsServer(s.v.Stats()))
4482
}
4583

4684
func init() {

app/stats/command/command.pb.go

+150-28
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import fmt "fmt"
55
import math "math"
66

77
import (
8-
"context"
9-
8+
context "golang.org/x/net/context"
109
grpc "google.golang.org/grpc"
1110
)
1211

@@ -35,7 +34,7 @@ func (m *GetStatsRequest) Reset() { *m = GetStatsRequest{} }
3534
func (m *GetStatsRequest) String() string { return proto.CompactTextString(m) }
3635
func (*GetStatsRequest) ProtoMessage() {}
3736
func (*GetStatsRequest) Descriptor() ([]byte, []int) {
38-
return fileDescriptor_command_a956deb495595f76, []int{0}
37+
return fileDescriptor_command_d2105e779ee1253c, []int{0}
3938
}
4039
func (m *GetStatsRequest) XXX_Unmarshal(b []byte) error {
4140
return xxx_messageInfo_GetStatsRequest.Unmarshal(m, b)
@@ -81,7 +80,7 @@ func (m *Stat) Reset() { *m = Stat{} }
8180
func (m *Stat) String() string { return proto.CompactTextString(m) }
8281
func (*Stat) ProtoMessage() {}
8382
func (*Stat) Descriptor() ([]byte, []int) {
84-
return fileDescriptor_command_a956deb495595f76, []int{1}
83+
return fileDescriptor_command_d2105e779ee1253c, []int{1}
8584
}
8685
func (m *Stat) XXX_Unmarshal(b []byte) error {
8786
return xxx_messageInfo_Stat.Unmarshal(m, b)
@@ -126,7 +125,7 @@ func (m *GetStatsResponse) Reset() { *m = GetStatsResponse{} }
126125
func (m *GetStatsResponse) String() string { return proto.CompactTextString(m) }
127126
func (*GetStatsResponse) ProtoMessage() {}
128127
func (*GetStatsResponse) Descriptor() ([]byte, []int) {
129-
return fileDescriptor_command_a956deb495595f76, []int{2}
128+
return fileDescriptor_command_d2105e779ee1253c, []int{2}
130129
}
131130
func (m *GetStatsResponse) XXX_Unmarshal(b []byte) error {
132131
return xxx_messageInfo_GetStatsResponse.Unmarshal(m, b)
@@ -153,6 +152,90 @@ func (m *GetStatsResponse) GetStat() *Stat {
153152
return nil
154153
}
155154

155+
type QueryStatsRequest struct {
156+
Pattern string `protobuf:"bytes,1,opt,name=pattern,proto3" json:"pattern,omitempty"`
157+
Reset_ bool `protobuf:"varint,2,opt,name=reset,proto3" json:"reset,omitempty"`
158+
XXX_NoUnkeyedLiteral struct{} `json:"-"`
159+
XXX_unrecognized []byte `json:"-"`
160+
XXX_sizecache int32 `json:"-"`
161+
}
162+
163+
func (m *QueryStatsRequest) Reset() { *m = QueryStatsRequest{} }
164+
func (m *QueryStatsRequest) String() string { return proto.CompactTextString(m) }
165+
func (*QueryStatsRequest) ProtoMessage() {}
166+
func (*QueryStatsRequest) Descriptor() ([]byte, []int) {
167+
return fileDescriptor_command_d2105e779ee1253c, []int{3}
168+
}
169+
func (m *QueryStatsRequest) XXX_Unmarshal(b []byte) error {
170+
return xxx_messageInfo_QueryStatsRequest.Unmarshal(m, b)
171+
}
172+
func (m *QueryStatsRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
173+
return xxx_messageInfo_QueryStatsRequest.Marshal(b, m, deterministic)
174+
}
175+
func (dst *QueryStatsRequest) XXX_Merge(src proto.Message) {
176+
xxx_messageInfo_QueryStatsRequest.Merge(dst, src)
177+
}
178+
func (m *QueryStatsRequest) XXX_Size() int {
179+
return xxx_messageInfo_QueryStatsRequest.Size(m)
180+
}
181+
func (m *QueryStatsRequest) XXX_DiscardUnknown() {
182+
xxx_messageInfo_QueryStatsRequest.DiscardUnknown(m)
183+
}
184+
185+
var xxx_messageInfo_QueryStatsRequest proto.InternalMessageInfo
186+
187+
func (m *QueryStatsRequest) GetPattern() string {
188+
if m != nil {
189+
return m.Pattern
190+
}
191+
return ""
192+
}
193+
194+
func (m *QueryStatsRequest) GetReset_() bool {
195+
if m != nil {
196+
return m.Reset_
197+
}
198+
return false
199+
}
200+
201+
type QueryStatsResponse struct {
202+
Stat []*Stat `protobuf:"bytes,1,rep,name=stat,proto3" json:"stat,omitempty"`
203+
XXX_NoUnkeyedLiteral struct{} `json:"-"`
204+
XXX_unrecognized []byte `json:"-"`
205+
XXX_sizecache int32 `json:"-"`
206+
}
207+
208+
func (m *QueryStatsResponse) Reset() { *m = QueryStatsResponse{} }
209+
func (m *QueryStatsResponse) String() string { return proto.CompactTextString(m) }
210+
func (*QueryStatsResponse) ProtoMessage() {}
211+
func (*QueryStatsResponse) Descriptor() ([]byte, []int) {
212+
return fileDescriptor_command_d2105e779ee1253c, []int{4}
213+
}
214+
func (m *QueryStatsResponse) XXX_Unmarshal(b []byte) error {
215+
return xxx_messageInfo_QueryStatsResponse.Unmarshal(m, b)
216+
}
217+
func (m *QueryStatsResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
218+
return xxx_messageInfo_QueryStatsResponse.Marshal(b, m, deterministic)
219+
}
220+
func (dst *QueryStatsResponse) XXX_Merge(src proto.Message) {
221+
xxx_messageInfo_QueryStatsResponse.Merge(dst, src)
222+
}
223+
func (m *QueryStatsResponse) XXX_Size() int {
224+
return xxx_messageInfo_QueryStatsResponse.Size(m)
225+
}
226+
func (m *QueryStatsResponse) XXX_DiscardUnknown() {
227+
xxx_messageInfo_QueryStatsResponse.DiscardUnknown(m)
228+
}
229+
230+
var xxx_messageInfo_QueryStatsResponse proto.InternalMessageInfo
231+
232+
func (m *QueryStatsResponse) GetStat() []*Stat {
233+
if m != nil {
234+
return m.Stat
235+
}
236+
return nil
237+
}
238+
156239
type Config struct {
157240
XXX_NoUnkeyedLiteral struct{} `json:"-"`
158241
XXX_unrecognized []byte `json:"-"`
@@ -163,7 +246,7 @@ func (m *Config) Reset() { *m = Config{} }
163246
func (m *Config) String() string { return proto.CompactTextString(m) }
164247
func (*Config) ProtoMessage() {}
165248
func (*Config) Descriptor() ([]byte, []int) {
166-
return fileDescriptor_command_a956deb495595f76, []int{3}
249+
return fileDescriptor_command_d2105e779ee1253c, []int{5}
167250
}
168251
func (m *Config) XXX_Unmarshal(b []byte) error {
169252
return xxx_messageInfo_Config.Unmarshal(m, b)
@@ -187,6 +270,8 @@ func init() {
187270
proto.RegisterType((*GetStatsRequest)(nil), "v2ray.core.app.stats.command.GetStatsRequest")
188271
proto.RegisterType((*Stat)(nil), "v2ray.core.app.stats.command.Stat")
189272
proto.RegisterType((*GetStatsResponse)(nil), "v2ray.core.app.stats.command.GetStatsResponse")
273+
proto.RegisterType((*QueryStatsRequest)(nil), "v2ray.core.app.stats.command.QueryStatsRequest")
274+
proto.RegisterType((*QueryStatsResponse)(nil), "v2ray.core.app.stats.command.QueryStatsResponse")
190275
proto.RegisterType((*Config)(nil), "v2ray.core.app.stats.command.Config")
191276
}
192277

@@ -203,6 +288,7 @@ const _ = grpc.SupportPackageIsVersion4
203288
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
204289
type StatsServiceClient interface {
205290
GetStats(ctx context.Context, in *GetStatsRequest, opts ...grpc.CallOption) (*GetStatsResponse, error)
291+
QueryStats(ctx context.Context, in *QueryStatsRequest, opts ...grpc.CallOption) (*QueryStatsResponse, error)
206292
}
207293

208294
type statsServiceClient struct {
@@ -222,9 +308,19 @@ func (c *statsServiceClient) GetStats(ctx context.Context, in *GetStatsRequest,
222308
return out, nil
223309
}
224310

311+
func (c *statsServiceClient) QueryStats(ctx context.Context, in *QueryStatsRequest, opts ...grpc.CallOption) (*QueryStatsResponse, error) {
312+
out := new(QueryStatsResponse)
313+
err := c.cc.Invoke(ctx, "/v2ray.core.app.stats.command.StatsService/QueryStats", in, out, opts...)
314+
if err != nil {
315+
return nil, err
316+
}
317+
return out, nil
318+
}
319+
225320
// StatsServiceServer is the server API for StatsService service.
226321
type StatsServiceServer interface {
227322
GetStats(context.Context, *GetStatsRequest) (*GetStatsResponse, error)
323+
QueryStats(context.Context, *QueryStatsRequest) (*QueryStatsResponse, error)
228324
}
229325

230326
func RegisterStatsServiceServer(s *grpc.Server, srv StatsServiceServer) {
@@ -249,6 +345,24 @@ func _StatsService_GetStats_Handler(srv interface{}, ctx context.Context, dec fu
249345
return interceptor(ctx, in, info, handler)
250346
}
251347

348+
func _StatsService_QueryStats_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
349+
in := new(QueryStatsRequest)
350+
if err := dec(in); err != nil {
351+
return nil, err
352+
}
353+
if interceptor == nil {
354+
return srv.(StatsServiceServer).QueryStats(ctx, in)
355+
}
356+
info := &grpc.UnaryServerInfo{
357+
Server: srv,
358+
FullMethod: "/v2ray.core.app.stats.command.StatsService/QueryStats",
359+
}
360+
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
361+
return srv.(StatsServiceServer).QueryStats(ctx, req.(*QueryStatsRequest))
362+
}
363+
return interceptor(ctx, in, info, handler)
364+
}
365+
252366
var _StatsService_serviceDesc = grpc.ServiceDesc{
253367
ServiceName: "v2ray.core.app.stats.command.StatsService",
254368
HandlerType: (*StatsServiceServer)(nil),
@@ -257,32 +371,40 @@ var _StatsService_serviceDesc = grpc.ServiceDesc{
257371
MethodName: "GetStats",
258372
Handler: _StatsService_GetStats_Handler,
259373
},
374+
{
375+
MethodName: "QueryStats",
376+
Handler: _StatsService_QueryStats_Handler,
377+
},
260378
},
261379
Streams: []grpc.StreamDesc{},
262380
Metadata: "v2ray.com/core/app/stats/command/command.proto",
263381
}
264382

265383
func init() {
266-
proto.RegisterFile("v2ray.com/core/app/stats/command/command.proto", fileDescriptor_command_a956deb495595f76)
267-
}
268-
269-
var fileDescriptor_command_a956deb495595f76 = []byte{
270-
// 267 bytes of a gzipped FileDescriptorProto
271-
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x91, 0x3f, 0x4b, 0x03, 0x31,
272-
0x14, 0xc0, 0xbd, 0x5a, 0xeb, 0xf9, 0x14, 0x94, 0xe0, 0x50, 0xa4, 0xc3, 0x91, 0xa9, 0x8b, 0xef,
273-
0xe4, 0x04, 0x17, 0x27, 0xbd, 0x41, 0x10, 0x07, 0x49, 0xc1, 0xc1, 0x2d, 0xc6, 0xa7, 0x14, 0x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,
285-
0x07, 0xb5, 0xfb, 0xec, 0x71, 0x77, 0x13, 0x7e, 0x8d, 0x66, 0x0f, 0x95, 0x90, 0x6b, 0xac, 0x23,
286-
0x79, 0x65, 0x6d, 0xda, 0xc8, 0x63, 0xdd, 0x3d, 0x3f, 0x4d, 0xd2, 0xa7, 0x9d, 0x7f, 0x07, 0x00,
287-
0x00, 0xff, 0xff, 0x10, 0x3a, 0x8a, 0xf3, 0xe6, 0x01, 0x00, 0x00,
384+
proto.RegisterFile("v2ray.com/core/app/stats/command/command.proto", fileDescriptor_command_d2105e779ee1253c)
385+
}
386+
387+
var fileDescriptor_command_d2105e779ee1253c = []byte{
388+
// 321 bytes of a gzipped FileDescriptorProto
389+
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x92, 0xb1, 0x4e, 0xc3, 0x30,
390+
0x10, 0x86, 0x49, 0x5b, 0xda, 0x72, 0x20, 0x01, 0x16, 0x43, 0x55, 0x75, 0x88, 0x3c, 0x75, 0xc1,
391+
0xa9, 0x82, 0xc4, 0xc2, 0x04, 0x19, 0x90, 0x50, 0x07, 0x70, 0x25, 0x06, 0x36, 0x13, 0x0e, 0x54,
392+
0x41, 0x62, 0xd7, 0x76, 0x22, 0xe5, 0x95, 0x78, 0x38, 0x9e, 0x01, 0xc5, 0x49, 0x54, 0xa0, 0x6a,
393+
0x54, 0xa6, 0xdc, 0xc5, 0xff, 0x77, 0xf7, 0xdf, 0xd9, 0xc0, 0xf2, 0x50, 0x8b, 0x82, 0xc5, 0x32,
394+
0x09, 0x62, 0xa9, 0x31, 0x10, 0x4a, 0x05, 0xc6, 0x0a, 0x6b, 0x82, 0x58, 0x26, 0x89, 0x48, 0x5f,
395+
0x9a, 0x2f, 0x53, 0x5a, 0x5a, 0x49, 0x26, 0x8d, 0x5e, 0x23, 0x13, 0x4a, 0x31, 0xa7, 0x65, 0xb5,
396+
0x86, 0x5e, 0xc1, 0xf1, 0x2d, 0x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0xa5, 0xdc, 0x16, 0x0d, 0x8b, 0xaa, 0xe3, 0xe7, 0xbe,
408+
0x7b, 0xbb, 0x17, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xcc, 0x9e, 0xb8, 0xeb, 0xed, 0x02, 0x00,
409+
0x00,
288410
}

app/stats/command/command.proto

+10
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ message GetStatsResponse {
2222
Stat stat = 1;
2323
}
2424

25+
message QueryStatsRequest {
26+
string pattern = 1;
27+
bool reset = 2;
28+
}
29+
30+
message QueryStatsResponse {
31+
repeated Stat stat = 1;
32+
}
33+
2534
service StatsService {
2635
rpc GetStats(GetStatsRequest) returns (GetStatsResponse) {}
36+
rpc QueryStats(QueryStatsRequest) returns (QueryStatsResponse) {}
2737
}
2838

2939
message Config {}

0 commit comments

Comments
 (0)