From 5fa22f2192900612648116bba801836799bd2a86 Mon Sep 17 00:00:00 2001 From: guogangping <512979011@qq.com> Date: Tue, 2 Nov 2021 13:33:05 +0800 Subject: [PATCH] feature: event query support tags filter --- .../core/monitor/event/pb/event_query.pb.go | 106 ++++++++++-------- .../event/pb/event_query.validator.pb.go | 1 + .../core/monitor/event/event_query.proto | 1 + .../event/query/event.query.service.go | 9 ++ .../event/query/event.query.service_test.go | 32 ++++++ 5 files changed, 105 insertions(+), 44 deletions(-) diff --git a/api/proto-go/core/monitor/event/pb/event_query.pb.go b/api/proto-go/core/monitor/event/pb/event_query.pb.go index 3a73c9b1fe1..0f54575f052 100644 --- a/api/proto-go/core/monitor/event/pb/event_query.pb.go +++ b/api/proto-go/core/monitor/event/pb/event_query.pb.go @@ -28,15 +28,16 @@ type GetEventsRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - EventId string `protobuf:"bytes,1,opt,name=eventId,proto3" json:"eventId,omitempty"` - TraceId string `protobuf:"bytes,2,opt,name=traceId,proto3" json:"traceId,omitempty"` - RelationId string `protobuf:"bytes,3,opt,name=relationId,proto3" json:"relationId,omitempty"` - RelationType string `protobuf:"bytes,4,opt,name=relationType,proto3" json:"relationType,omitempty"` - Start int64 `protobuf:"varint,5,opt,name=start,proto3" json:"start,omitempty"` - End int64 `protobuf:"varint,6,opt,name=end,proto3" json:"end,omitempty"` - PageNo int64 `protobuf:"varint,7,opt,name=pageNo,proto3" json:"pageNo,omitempty"` - PageSize int64 `protobuf:"varint,8,opt,name=pageSize,proto3" json:"pageSize,omitempty"` - Debug bool `protobuf:"varint,9,opt,name=debug,proto3" json:"debug,omitempty"` + EventId string `protobuf:"bytes,1,opt,name=eventId,proto3" json:"eventId,omitempty"` + TraceId string `protobuf:"bytes,2,opt,name=traceId,proto3" json:"traceId,omitempty"` + RelationId string `protobuf:"bytes,3,opt,name=relationId,proto3" json:"relationId,omitempty"` + RelationType string `protobuf:"bytes,4,opt,name=relationType,proto3" json:"relationType,omitempty"` + Start int64 `protobuf:"varint,5,opt,name=start,proto3" json:"start,omitempty"` + End int64 `protobuf:"varint,6,opt,name=end,proto3" json:"end,omitempty"` + PageNo int64 `protobuf:"varint,7,opt,name=pageNo,proto3" json:"pageNo,omitempty"` + PageSize int64 `protobuf:"varint,8,opt,name=pageSize,proto3" json:"pageSize,omitempty"` + Debug bool `protobuf:"varint,9,opt,name=debug,proto3" json:"debug,omitempty"` + Tags map[string]string `protobuf:"bytes,10,rep,name=tags,proto3" json:"tags,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` } func (x *GetEventsRequest) Reset() { @@ -134,6 +135,13 @@ func (x *GetEventsRequest) GetDebug() bool { return false } +func (x *GetEventsRequest) GetTags() map[string]string { + if x != nil { + return x.Tags + } + return nil +} + type GetEventsResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -237,7 +245,7 @@ var file_event_query_proto_rawDesc = []byte{ 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x15, 0x6f, 0x61, 0x70, 0x2f, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2f, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x22, 0xfc, 0x01, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, + 0x6f, 0x22, 0xfe, 0x02, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x74, 0x72, 0x61, 0x63, 0x65, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, @@ -253,29 +261,37 @@ var file_event_query_proto_rawDesc = []byte{ 0x0a, 0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x64, 0x65, 0x62, 0x75, 0x67, 0x18, 0x09, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x64, 0x65, 0x62, 0x75, 0x67, - 0x22, 0x51, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x3c, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x0b, 0x32, 0x28, 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, + 0x12, 0x47, 0x0a, 0x04, 0x74, 0x61, 0x67, 0x73, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x33, + 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x6d, 0x6f, 0x6e, 0x69, 0x74, + 0x6f, 0x72, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, + 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x54, 0x61, 0x67, 0x73, 0x45, 0x6e, + 0x74, 0x72, 0x79, 0x52, 0x04, 0x74, 0x61, 0x67, 0x73, 0x1a, 0x37, 0x0a, 0x09, 0x54, 0x61, 0x67, + 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, + 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, + 0x38, 0x01, 0x22, 0x51, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x3c, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x28, 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, 0x63, 0x6f, 0x72, + 0x65, 0x2e, 0x6d, 0x6f, 0x6e, 0x69, 0x74, 0x6f, 0x72, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, + 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, + 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3e, 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, + 0x74, 0x73, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x2b, 0x0a, 0x05, 0x69, 0x74, 0x65, 0x6d, + 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, 0x6f, + 0x61, 0x70, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x05, + 0x69, 0x74, 0x65, 0x6d, 0x73, 0x32, 0x8c, 0x01, 0x0a, 0x11, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x51, + 0x75, 0x65, 0x72, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x77, 0x0a, 0x09, 0x47, + 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x29, 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, + 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x6d, 0x6f, 0x6e, 0x69, 0x74, 0x6f, 0x72, 0x2e, 0x65, 0x76, 0x65, + 0x6e, 0x74, 0x2e, 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x2a, 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x6d, 0x6f, 0x6e, 0x69, 0x74, 0x6f, 0x72, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x47, 0x65, - 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, 0x04, 0x64, - 0x61, 0x74, 0x61, 0x22, 0x3e, 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, - 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x2b, 0x0a, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x18, - 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, 0x6f, 0x61, 0x70, - 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x69, 0x74, - 0x65, 0x6d, 0x73, 0x32, 0x8c, 0x01, 0x0a, 0x11, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x51, 0x75, 0x65, - 0x72, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x77, 0x0a, 0x09, 0x47, 0x65, 0x74, - 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x29, 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, 0x63, 0x6f, - 0x72, 0x65, 0x2e, 0x6d, 0x6f, 0x6e, 0x69, 0x74, 0x6f, 0x72, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, - 0x2e, 0x47, 0x65, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x1a, 0x2a, 0x2e, 0x65, 0x72, 0x64, 0x61, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x6d, 0x6f, - 0x6e, 0x69, 0x74, 0x6f, 0x72, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x47, 0x65, 0x74, 0x45, - 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x13, 0x82, - 0xd3, 0xe4, 0x93, 0x02, 0x0d, 0x12, 0x0b, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x65, 0x76, 0x65, 0x6e, - 0x74, 0x73, 0x42, 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, - 0x2f, 0x65, 0x72, 0x64, 0x61, 0x2d, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x2f, 0x65, 0x72, - 0x64, 0x61, 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2d, 0x67, 0x6f, 0x2f, 0x63, 0x6f, 0x72, 0x65, - 0x2f, 0x6d, 0x6f, 0x6e, 0x69, 0x74, 0x6f, 0x72, 0x2f, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2f, 0x70, - 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, + 0x13, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0d, 0x12, 0x0b, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x65, 0x76, + 0x65, 0x6e, 0x74, 0x73, 0x42, 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x65, 0x72, 0x64, 0x61, 0x2d, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x2f, + 0x65, 0x72, 0x64, 0x61, 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2d, 0x67, 0x6f, 0x2f, 0x63, 0x6f, + 0x72, 0x65, 0x2f, 0x6d, 0x6f, 0x6e, 0x69, 0x74, 0x6f, 0x72, 0x2f, 0x65, 0x76, 0x65, 0x6e, 0x74, + 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -290,23 +306,25 @@ func file_event_query_proto_rawDescGZIP() []byte { return file_event_query_proto_rawDescData } -var file_event_query_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_event_query_proto_msgTypes = make([]protoimpl.MessageInfo, 4) var file_event_query_proto_goTypes = []interface{}{ (*GetEventsRequest)(nil), // 0: erda.core.monitor.event.GetEventsRequest (*GetEventsResponse)(nil), // 1: erda.core.monitor.event.GetEventsResponse (*GetEventsResult)(nil), // 2: erda.core.monitor.event.GetEventsResult - (*pb.Event)(nil), // 3: erda.oap.event.Event + nil, // 3: erda.core.monitor.event.GetEventsRequest.TagsEntry + (*pb.Event)(nil), // 4: erda.oap.event.Event } var file_event_query_proto_depIdxs = []int32{ - 2, // 0: erda.core.monitor.event.GetEventsResponse.data:type_name -> erda.core.monitor.event.GetEventsResult - 3, // 1: erda.core.monitor.event.GetEventsResult.items:type_name -> erda.oap.event.Event - 0, // 2: erda.core.monitor.event.EventQueryService.GetEvents:input_type -> erda.core.monitor.event.GetEventsRequest - 1, // 3: erda.core.monitor.event.EventQueryService.GetEvents:output_type -> erda.core.monitor.event.GetEventsResponse - 3, // [3:4] is the sub-list for method output_type - 2, // [2:3] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name - 2, // [2:2] is the sub-list for extension extendee - 0, // [0:2] is the sub-list for field type_name + 3, // 0: erda.core.monitor.event.GetEventsRequest.tags:type_name -> erda.core.monitor.event.GetEventsRequest.TagsEntry + 2, // 1: erda.core.monitor.event.GetEventsResponse.data:type_name -> erda.core.monitor.event.GetEventsResult + 4, // 2: erda.core.monitor.event.GetEventsResult.items:type_name -> erda.oap.event.Event + 0, // 3: erda.core.monitor.event.EventQueryService.GetEvents:input_type -> erda.core.monitor.event.GetEventsRequest + 1, // 4: erda.core.monitor.event.EventQueryService.GetEvents:output_type -> erda.core.monitor.event.GetEventsResponse + 4, // [4:5] is the sub-list for method output_type + 3, // [3:4] is the sub-list for method input_type + 3, // [3:3] is the sub-list for extension type_name + 3, // [3:3] is the sub-list for extension extendee + 0, // [0:3] is the sub-list for field type_name } func init() { file_event_query_proto_init() } @@ -358,7 +376,7 @@ func file_event_query_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_event_query_proto_rawDesc, NumEnums: 0, - NumMessages: 3, + NumMessages: 4, NumExtensions: 0, NumServices: 1, }, diff --git a/api/proto-go/core/monitor/event/pb/event_query.validator.pb.go b/api/proto-go/core/monitor/event/pb/event_query.validator.pb.go index 7b52e4e36f8..40e7234bb42 100644 --- a/api/proto-go/core/monitor/event/pb/event_query.validator.pb.go +++ b/api/proto-go/core/monitor/event/pb/event_query.validator.pb.go @@ -19,6 +19,7 @@ var _ = fmt.Errorf var _ = math.Inf func (this *GetEventsRequest) Validate() error { + // Validation of proto3 map<> fields is unsupported. return nil } func (this *GetEventsResponse) Validate() error { diff --git a/api/proto/core/monitor/event/event_query.proto b/api/proto/core/monitor/event/event_query.proto index 792a8abbcc0..0f72d96b7b7 100644 --- a/api/proto/core/monitor/event/event_query.proto +++ b/api/proto/core/monitor/event/event_query.proto @@ -22,6 +22,7 @@ message GetEventsRequest { int64 pageNo = 7; int64 pageSize = 8; bool debug = 9; + map tags = 10; } message GetEventsResponse { diff --git a/modules/core/monitor/event/query/event.query.service.go b/modules/core/monitor/event/query/event.query.service.go index a60b78a53ed..90f8775902e 100644 --- a/modules/core/monitor/event/query/event.query.service.go +++ b/modules/core/monitor/event/query/event.query.service.go @@ -59,6 +59,15 @@ func (s *eventQueryService) GetEvents(ctx context.Context, req *pb.GetEventsRequ Value: req.RelationId, }) } + if len(req.Tags) > 0 { + for key, val := range req.Tags { + sel.Filters = append(sel.Filters, &storage.Filter{ + Key: "tags." + key, + Op: storage.EQ, + Value: val, + }) + } + } list, err := s.storageReader.QueryPaged(ctx, sel, int(req.PageNo), int(req.PageSize)) if err != nil { return nil, err diff --git a/modules/core/monitor/event/query/event.query.service_test.go b/modules/core/monitor/event/query/event.query.service_test.go index cecf81a42f0..78ba5145d63 100644 --- a/modules/core/monitor/event/query/event.query.service_test.go +++ b/modules/core/monitor/event/query/event.query.service_test.go @@ -95,6 +95,38 @@ func Test_eventQueryService_GetEvents_WithValidParams_Should_Return_NonEmptyList {EventID: "event-id-1"}, }, nil) + querySvc := &eventQueryService{ + storageReader: storage, + } + result, err := querySvc.GetEvents(context.Background(), &pb.GetEventsRequest{ + Start: 1, + End: 2, + TraceId: "trace-id", + RelationId: "res-id", + RelationType: "res-type", + Tags: map[string]string{ + "key-1": "val-1", + }, + }) + if err != nil { + t.Errorf("should not throw error") + } + if result == nil || len(result.Data.Items) != 1 { + t.Errorf("assert result failed") + } + +} + +func Test_eventQueryService_GetEvents_WithNilTags_Should_Not_Throw(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + storage := NewMockStorage(ctrl) + storage.EXPECT(). + QueryPaged(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). + Return([]*event.Event{ + {EventID: "event-id-1"}, + }, nil) + querySvc := &eventQueryService{ storageReader: storage, }