From 9c84e29cb81a7917955b1cf8e68cdcef5c2b7e1a Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Wed, 3 Jan 2024 20:53:38 +0800 Subject: [PATCH] add observer --- observer/example/grpc/main.go | 52 +++ observer/example/http/main.go | 86 +++++ observer/proto/observer.pb.go | 506 +++++++++++++++++++++++++++++ observer/proto/observer.proto | 43 +++ observer/proto/observer_grpc.pb.go | 105 ++++++ 5 files changed, 792 insertions(+) create mode 100644 observer/example/grpc/main.go create mode 100644 observer/example/http/main.go create mode 100644 observer/proto/observer.pb.go create mode 100644 observer/proto/observer.proto create mode 100644 observer/proto/observer_grpc.pb.go diff --git a/observer/example/grpc/main.go b/observer/example/grpc/main.go new file mode 100644 index 0000000..2bc1ec2 --- /dev/null +++ b/observer/example/grpc/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "context" + "flag" + "fmt" + "log" + "net" + + "github.com/go-gost/plugin/observer/proto" + "google.golang.org/grpc" +) + +var ( + port = flag.Int("port", 8000, "The server port") +) + +type server struct { + proto.UnimplementedObserverServer +} + +func (s *server) Observe(ctx context.Context, in *proto.ObserveRequest) (*proto.ObserveReply, error) { + reply := &proto.ObserveReply{ + Ok: true, + } + + for _, event := range in.Events { + switch event.Type { + case "service": + log.Printf("observe %s: kind=%s, service=%s, status=%+v", event.Type, event.Kind, event.Service, event.Status) + case "stats": + log.Printf("observe %s: kind=%s, service=%s, client=%s, stats=%+v", event.Type, event.Kind, event.Service, event.Client, event.Stats) + default: + log.Printf("observe %+v", event) + } + } + return reply, nil +} + +func main() { + flag.Parse() + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + s := grpc.NewServer() + proto.RegisterObserverServer(s, &server{}) + log.Printf("server listening at %v", lis.Addr()) + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/observer/example/http/main.go b/observer/example/http/main.go new file mode 100644 index 0000000..48bc4b0 --- /dev/null +++ b/observer/example/http/main.go @@ -0,0 +1,86 @@ +package main + +import ( + "bytes" + "encoding/json" + "flag" + "fmt" + "io" + "log" + "net" + "net/http" +) + +var ( + port = flag.Int("port", 8000, "The server port") +) + +type observeRequest struct { + Events []event `json:"events"` +} + +type event struct { + Kind string `json:"kind"` + Service string `json:"service"` + Client string `json:"client,omitempty"` + Type string `json:"type"` + Stats *statsEvent `json:"stats,omitempty"` + Status *statusEvent `json:"status,omitempty"` +} + +type statsEvent struct { + TotalConns uint64 `json:"totalConns"` + CurrentConns int64 `json:"currentConns"` + InputBytes uint64 `json:"inputBytes"` + OutputBytes uint64 `json:"outputBytes"` +} + +type statusEvent struct { + State string `json:"state"` + Msg string `json:"msg"` +} + +type observeResponse struct { + OK bool `json:"ok"` +} + +func main() { + flag.Parse() + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + log.Printf("server listening at %v", lis.Addr()) + + http.HandleFunc("/observer", func(w http.ResponseWriter, r *http.Request) { + rb := observeRequest{} + data, _ := io.ReadAll(r.Body) + log.Println(string(data)) + if err := json.NewDecoder(bytes.NewReader(data)).Decode(&rb); err != nil { + log.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + resp := observeResponse{ + OK: false, + } + + for _, event := range rb.Events { + switch event.Type { + case "service": + log.Printf("observe %s: kind=%s, service=%s, status=%+v", event.Type, event.Kind, event.Service, event.Status) + case "stats": + log.Printf("observe %s: kind=%s, service=%s, client=%s, stats=%+v", event.Type, event.Kind, event.Service, event.Client, event.Stats) + default: + log.Printf("observe %+v", event) + } + } + + json.NewEncoder(w).Encode(resp) + }) + + if err := http.Serve(lis, nil); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/observer/proto/observer.pb.go b/observer/proto/observer.pb.go new file mode 100644 index 0000000..1c0bab9 --- /dev/null +++ b/observer/proto/observer.pb.go @@ -0,0 +1,506 @@ +// protoc --go_out=. --go_opt=paths=source_relative \ +// --go-grpc_out=. --go-grpc_opt=paths=source_relative \ +// observer.proto + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v3.15.8 +// source: observer.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type ObserveRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Events []*Event `protobuf:"bytes,1,rep,name=events,proto3" json:"events,omitempty"` +} + +func (x *ObserveRequest) Reset() { + *x = ObserveRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_observer_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ObserveRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ObserveRequest) ProtoMessage() {} + +func (x *ObserveRequest) ProtoReflect() protoreflect.Message { + mi := &file_observer_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ObserveRequest.ProtoReflect.Descriptor instead. +func (*ObserveRequest) Descriptor() ([]byte, []int) { + return file_observer_proto_rawDescGZIP(), []int{0} +} + +func (x *ObserveRequest) GetEvents() []*Event { + if x != nil { + return x.Events + } + return nil +} + +type Event struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Kind string `protobuf:"bytes,1,opt,name=kind,proto3" json:"kind,omitempty"` + Service string `protobuf:"bytes,2,opt,name=service,proto3" json:"service,omitempty"` + Client string `protobuf:"bytes,3,opt,name=client,proto3" json:"client,omitempty"` + Type string `protobuf:"bytes,4,opt,name=type,proto3" json:"type,omitempty"` + Stats *Stats `protobuf:"bytes,5,opt,name=stats,proto3" json:"stats,omitempty"` + Status *ServiceStatus `protobuf:"bytes,6,opt,name=status,proto3" json:"status,omitempty"` +} + +func (x *Event) Reset() { + *x = Event{} + if protoimpl.UnsafeEnabled { + mi := &file_observer_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Event) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Event) ProtoMessage() {} + +func (x *Event) ProtoReflect() protoreflect.Message { + mi := &file_observer_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Event.ProtoReflect.Descriptor instead. +func (*Event) Descriptor() ([]byte, []int) { + return file_observer_proto_rawDescGZIP(), []int{1} +} + +func (x *Event) GetKind() string { + if x != nil { + return x.Kind + } + return "" +} + +func (x *Event) GetService() string { + if x != nil { + return x.Service + } + return "" +} + +func (x *Event) GetClient() string { + if x != nil { + return x.Client + } + return "" +} + +func (x *Event) GetType() string { + if x != nil { + return x.Type + } + return "" +} + +func (x *Event) GetStats() *Stats { + if x != nil { + return x.Stats + } + return nil +} + +func (x *Event) GetStatus() *ServiceStatus { + if x != nil { + return x.Status + } + return nil +} + +type ServiceStatus struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + State string `protobuf:"bytes,1,opt,name=state,proto3" json:"state,omitempty"` + Msg string `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"` +} + +func (x *ServiceStatus) Reset() { + *x = ServiceStatus{} + if protoimpl.UnsafeEnabled { + mi := &file_observer_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ServiceStatus) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ServiceStatus) ProtoMessage() {} + +func (x *ServiceStatus) ProtoReflect() protoreflect.Message { + mi := &file_observer_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ServiceStatus.ProtoReflect.Descriptor instead. +func (*ServiceStatus) Descriptor() ([]byte, []int) { + return file_observer_proto_rawDescGZIP(), []int{2} +} + +func (x *ServiceStatus) GetState() string { + if x != nil { + return x.State + } + return "" +} + +func (x *ServiceStatus) GetMsg() string { + if x != nil { + return x.Msg + } + return "" +} + +type Stats struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + TotalConns uint64 `protobuf:"varint,1,opt,name=totalConns,proto3" json:"totalConns,omitempty"` + CurrentConns uint64 `protobuf:"varint,2,opt,name=currentConns,proto3" json:"currentConns,omitempty"` + InputBytes uint64 `protobuf:"varint,3,opt,name=inputBytes,proto3" json:"inputBytes,omitempty"` + OutputBytes uint64 `protobuf:"varint,4,opt,name=outputBytes,proto3" json:"outputBytes,omitempty"` + TotalErrs uint64 `protobuf:"varint,5,opt,name=totalErrs,proto3" json:"totalErrs,omitempty"` +} + +func (x *Stats) Reset() { + *x = Stats{} + if protoimpl.UnsafeEnabled { + mi := &file_observer_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Stats) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Stats) ProtoMessage() {} + +func (x *Stats) ProtoReflect() protoreflect.Message { + mi := &file_observer_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Stats.ProtoReflect.Descriptor instead. +func (*Stats) Descriptor() ([]byte, []int) { + return file_observer_proto_rawDescGZIP(), []int{3} +} + +func (x *Stats) GetTotalConns() uint64 { + if x != nil { + return x.TotalConns + } + return 0 +} + +func (x *Stats) GetCurrentConns() uint64 { + if x != nil { + return x.CurrentConns + } + return 0 +} + +func (x *Stats) GetInputBytes() uint64 { + if x != nil { + return x.InputBytes + } + return 0 +} + +func (x *Stats) GetOutputBytes() uint64 { + if x != nil { + return x.OutputBytes + } + return 0 +} + +func (x *Stats) GetTotalErrs() uint64 { + if x != nil { + return x.TotalErrs + } + return 0 +} + +type ObserveReply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ok bool `protobuf:"varint,1,opt,name=ok,proto3" json:"ok,omitempty"` +} + +func (x *ObserveReply) Reset() { + *x = ObserveReply{} + if protoimpl.UnsafeEnabled { + mi := &file_observer_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ObserveReply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ObserveReply) ProtoMessage() {} + +func (x *ObserveReply) ProtoReflect() protoreflect.Message { + mi := &file_observer_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ObserveReply.ProtoReflect.Descriptor instead. +func (*ObserveReply) Descriptor() ([]byte, []int) { + return file_observer_proto_rawDescGZIP(), []int{4} +} + +func (x *ObserveReply) GetOk() bool { + if x != nil { + return x.Ok + } + return false +} + +var File_observer_proto protoreflect.FileDescriptor + +var file_observer_proto_rawDesc = []byte{ + 0x0a, 0x0e, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x12, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x36, 0x0a, 0x0e, 0x4f, 0x62, 0x73, 0x65, 0x72, + 0x76, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x24, 0x0a, 0x06, 0x65, 0x76, 0x65, + 0x6e, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x06, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x22, + 0xb3, 0x01, 0x0a, 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6b, 0x69, 0x6e, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6b, 0x69, 0x6e, 0x64, 0x12, 0x18, 0x0a, + 0x07, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, + 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x6c, 0x69, 0x65, 0x6e, + 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x12, + 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, + 0x79, 0x70, 0x65, 0x12, 0x22, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, 0x73, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x73, + 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x73, 0x12, 0x2c, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, + 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x06, 0x73, + 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x37, 0x0a, 0x0d, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x12, 0x10, 0x0a, 0x03, + 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x22, 0xab, + 0x01, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x74, 0x73, 0x12, 0x1e, 0x0a, 0x0a, 0x74, 0x6f, 0x74, 0x61, + 0x6c, 0x43, 0x6f, 0x6e, 0x6e, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0a, 0x74, 0x6f, + 0x74, 0x61, 0x6c, 0x43, 0x6f, 0x6e, 0x6e, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x63, 0x75, 0x72, 0x72, + 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0c, + 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x6e, 0x73, 0x12, 0x1e, 0x0a, 0x0a, + 0x69, 0x6e, 0x70, 0x75, 0x74, 0x42, 0x79, 0x74, 0x65, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, + 0x52, 0x0a, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x42, 0x79, 0x74, 0x65, 0x73, 0x12, 0x20, 0x0a, 0x0b, + 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x42, 0x79, 0x74, 0x65, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x04, 0x52, 0x0b, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x42, 0x79, 0x74, 0x65, 0x73, 0x12, 0x1c, + 0x0a, 0x09, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x45, 0x72, 0x72, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, + 0x04, 0x52, 0x09, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x45, 0x72, 0x72, 0x73, 0x22, 0x1e, 0x0a, 0x0c, + 0x4f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x0e, 0x0a, 0x02, + 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x32, 0x41, 0x0a, 0x08, + 0x4f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x12, 0x35, 0x0a, 0x07, 0x4f, 0x62, 0x73, 0x65, + 0x72, 0x76, 0x65, 0x12, 0x15, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4f, 0x62, 0x73, 0x65, + 0x72, 0x76, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x2e, 0x4f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x42, + 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, + 0x2d, 0x67, 0x6f, 0x73, 0x74, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2f, 0x6f, 0x62, 0x73, + 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, +} + +var ( + file_observer_proto_rawDescOnce sync.Once + file_observer_proto_rawDescData = file_observer_proto_rawDesc +) + +func file_observer_proto_rawDescGZIP() []byte { + file_observer_proto_rawDescOnce.Do(func() { + file_observer_proto_rawDescData = protoimpl.X.CompressGZIP(file_observer_proto_rawDescData) + }) + return file_observer_proto_rawDescData +} + +var file_observer_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_observer_proto_goTypes = []interface{}{ + (*ObserveRequest)(nil), // 0: proto.ObserveRequest + (*Event)(nil), // 1: proto.Event + (*ServiceStatus)(nil), // 2: proto.ServiceStatus + (*Stats)(nil), // 3: proto.Stats + (*ObserveReply)(nil), // 4: proto.ObserveReply +} +var file_observer_proto_depIdxs = []int32{ + 1, // 0: proto.ObserveRequest.events:type_name -> proto.Event + 3, // 1: proto.Event.stats:type_name -> proto.Stats + 2, // 2: proto.Event.status:type_name -> proto.ServiceStatus + 0, // 3: proto.Observer.Observe:input_type -> proto.ObserveRequest + 4, // 4: proto.Observer.Observe:output_type -> proto.ObserveReply + 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_observer_proto_init() } +func file_observer_proto_init() { + if File_observer_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_observer_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ObserveRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_observer_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Event); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_observer_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ServiceStatus); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_observer_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Stats); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_observer_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ObserveReply); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_observer_proto_rawDesc, + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_observer_proto_goTypes, + DependencyIndexes: file_observer_proto_depIdxs, + MessageInfos: file_observer_proto_msgTypes, + }.Build() + File_observer_proto = out.File + file_observer_proto_rawDesc = nil + file_observer_proto_goTypes = nil + file_observer_proto_depIdxs = nil +} diff --git a/observer/proto/observer.proto b/observer/proto/observer.proto new file mode 100644 index 0000000..6791752 --- /dev/null +++ b/observer/proto/observer.proto @@ -0,0 +1,43 @@ +// protoc --go_out=. --go_opt=paths=source_relative \ +// --go-grpc_out=. --go-grpc_opt=paths=source_relative \ +// observer.proto + +syntax = "proto3"; +package proto; +option go_package = "github.com/go-gost/plugin/observer/proto"; + +message ObserveRequest { + repeated Event events = 1; +} + +message Event { + string kind = 1; + string service = 2; + string client = 3; + string type = 4; + Stats stats = 5; + ServiceStatus status = 6; +} + +message ServiceStatus { + string state = 1; + string msg = 2; +} + +message Stats { + uint64 totalConns = 1; + uint64 currentConns = 2; + uint64 inputBytes = 3; + uint64 outputBytes = 4; + uint64 totalErrs = 5; +} + + +message ObserveReply { + bool ok = 1; +} + + +service Observer { + rpc Observe(ObserveRequest) returns (ObserveReply); +} diff --git a/observer/proto/observer_grpc.pb.go b/observer/proto/observer_grpc.pb.go new file mode 100644 index 0000000..a54b478 --- /dev/null +++ b/observer/proto/observer_grpc.pb.go @@ -0,0 +1,105 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.15.8 +// source: observer.proto + +package proto + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// ObserverClient is the client API for Observer service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type ObserverClient interface { + Observe(ctx context.Context, in *ObserveRequest, opts ...grpc.CallOption) (*ObserveReply, error) +} + +type observerClient struct { + cc grpc.ClientConnInterface +} + +func NewObserverClient(cc grpc.ClientConnInterface) ObserverClient { + return &observerClient{cc} +} + +func (c *observerClient) Observe(ctx context.Context, in *ObserveRequest, opts ...grpc.CallOption) (*ObserveReply, error) { + out := new(ObserveReply) + err := c.cc.Invoke(ctx, "/proto.Observer/Observe", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ObserverServer is the server API for Observer service. +// All implementations must embed UnimplementedObserverServer +// for forward compatibility +type ObserverServer interface { + Observe(context.Context, *ObserveRequest) (*ObserveReply, error) + mustEmbedUnimplementedObserverServer() +} + +// UnimplementedObserverServer must be embedded to have forward compatible implementations. +type UnimplementedObserverServer struct { +} + +func (UnimplementedObserverServer) Observe(context.Context, *ObserveRequest) (*ObserveReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method Observe not implemented") +} +func (UnimplementedObserverServer) mustEmbedUnimplementedObserverServer() {} + +// UnsafeObserverServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ObserverServer will +// result in compilation errors. +type UnsafeObserverServer interface { + mustEmbedUnimplementedObserverServer() +} + +func RegisterObserverServer(s grpc.ServiceRegistrar, srv ObserverServer) { + s.RegisterService(&Observer_ServiceDesc, srv) +} + +func _Observer_Observe_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ObserveRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ObserverServer).Observe(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/proto.Observer/Observe", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ObserverServer).Observe(ctx, req.(*ObserveRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// Observer_ServiceDesc is the grpc.ServiceDesc for Observer service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Observer_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "proto.Observer", + HandlerType: (*ObserverServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Observe", + Handler: _Observer_Observe_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "observer.proto", +}