1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-08-04 21:42:57 +02:00

feat: add test framework & refactor RPC server (#2579)

Co-authored-by: Rene Jochum <rene@jochum.dev>
This commit is contained in:
David Brouwer
2022-10-20 13:00:50 +02:00
committed by GitHub
parent c25dee7c8a
commit a3980c2308
54 changed files with 3703 additions and 2497 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ package debug
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
proto "google.golang.org/protobuf/proto"
math "math"
)
@ -21,12 +21,6 @@ var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
@ -46,6 +40,7 @@ type DebugService interface {
Health(ctx context.Context, in *HealthRequest, opts ...client.CallOption) (*HealthResponse, error)
Stats(ctx context.Context, in *StatsRequest, opts ...client.CallOption) (*StatsResponse, error)
Trace(ctx context.Context, in *TraceRequest, opts ...client.CallOption) (*TraceResponse, error)
MessageBus(ctx context.Context, opts ...client.CallOption) (Debug_MessageBusService, error)
}
type debugService struct {
@ -76,6 +71,7 @@ type Debug_LogService interface {
Context() context.Context
SendMsg(interface{}) error
RecvMsg(interface{}) error
CloseSend() error
Close() error
Recv() (*Record, error)
}
@ -84,6 +80,10 @@ type debugServiceLog struct {
stream client.Stream
}
func (x *debugServiceLog) CloseSend() error {
return x.stream.CloseSend()
}
func (x *debugServiceLog) Close() error {
return x.stream.Close()
}
@ -139,6 +139,62 @@ func (c *debugService) Trace(ctx context.Context, in *TraceRequest, opts ...clie
return out, nil
}
func (c *debugService) MessageBus(ctx context.Context, opts ...client.CallOption) (Debug_MessageBusService, error) {
req := c.c.NewRequest(c.name, "Debug.MessageBus", &BusMsg{})
stream, err := c.c.Stream(ctx, req, opts...)
if err != nil {
return nil, err
}
return &debugServiceMessageBus{stream}, nil
}
type Debug_MessageBusService interface {
Context() context.Context
SendMsg(interface{}) error
RecvMsg(interface{}) error
CloseSend() error
Close() error
Send(*BusMsg) error
Recv() (*BusMsg, error)
}
type debugServiceMessageBus struct {
stream client.Stream
}
func (x *debugServiceMessageBus) CloseSend() error {
return x.stream.CloseSend()
}
func (x *debugServiceMessageBus) Close() error {
return x.stream.Close()
}
func (x *debugServiceMessageBus) Context() context.Context {
return x.stream.Context()
}
func (x *debugServiceMessageBus) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *debugServiceMessageBus) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
func (x *debugServiceMessageBus) Send(m *BusMsg) error {
return x.stream.Send(m)
}
func (x *debugServiceMessageBus) Recv() (*BusMsg, error) {
m := new(BusMsg)
err := x.stream.Recv(m)
if err != nil {
return nil, err
}
return m, nil
}
// Server API for Debug service
type DebugHandler interface {
@ -146,6 +202,7 @@ type DebugHandler interface {
Health(context.Context, *HealthRequest, *HealthResponse) error
Stats(context.Context, *StatsRequest, *StatsResponse) error
Trace(context.Context, *TraceRequest, *TraceResponse) error
MessageBus(context.Context, Debug_MessageBusStream) error
}
func RegisterDebugHandler(s server.Server, hdlr DebugHandler, opts ...server.HandlerOption) error {
@ -154,6 +211,7 @@ func RegisterDebugHandler(s server.Server, hdlr DebugHandler, opts ...server.Han
Health(ctx context.Context, in *HealthRequest, out *HealthResponse) error
Stats(ctx context.Context, in *StatsRequest, out *StatsResponse) error
Trace(ctx context.Context, in *TraceRequest, out *TraceResponse) error
MessageBus(ctx context.Context, stream server.Stream) error
}
type Debug struct {
debug
@ -217,3 +275,48 @@ func (h *debugHandler) Stats(ctx context.Context, in *StatsRequest, out *StatsRe
func (h *debugHandler) Trace(ctx context.Context, in *TraceRequest, out *TraceResponse) error {
return h.DebugHandler.Trace(ctx, in, out)
}
func (h *debugHandler) MessageBus(ctx context.Context, stream server.Stream) error {
return h.DebugHandler.MessageBus(ctx, &debugMessageBusStream{stream})
}
type Debug_MessageBusStream interface {
Context() context.Context
SendMsg(interface{}) error
RecvMsg(interface{}) error
Close() error
Send(*BusMsg) error
Recv() (*BusMsg, error)
}
type debugMessageBusStream struct {
stream server.Stream
}
func (x *debugMessageBusStream) Close() error {
return x.stream.Close()
}
func (x *debugMessageBusStream) Context() context.Context {
return x.stream.Context()
}
func (x *debugMessageBusStream) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *debugMessageBusStream) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
func (x *debugMessageBusStream) Send(m *BusMsg) error {
return x.stream.Send(m)
}
func (x *debugMessageBusStream) Recv() (*BusMsg, error) {
m := new(BusMsg)
if err := x.stream.Recv(m); err != nil {
return nil, err
}
return m, nil
}

View File

@ -1,99 +1,107 @@
syntax = "proto3";
package debug;
option go_package = "./proto;debug";
// Compile this proto by running the following command in the debug directory:
// protoc --proto_path=. --micro_out=. --go_out=:. proto/debug.proto
service Debug {
rpc Log(LogRequest) returns (stream Record) {};
rpc Health(HealthRequest) returns (HealthResponse) {};
rpc Stats(StatsRequest) returns (StatsResponse) {};
rpc Trace(TraceRequest) returns (TraceResponse) {};
rpc Log(LogRequest) returns (stream Record) {};
rpc Health(HealthRequest) returns (HealthResponse) {};
rpc Stats(StatsRequest) returns (StatsResponse) {};
rpc Trace(TraceRequest) returns (TraceResponse) {};
rpc MessageBus(stream BusMsg) returns (stream BusMsg) {};
}
message BusMsg { string msg = 1; }
message HealthRequest {
// optional service name
string service = 1;
// optional service name
string service = 1;
}
message HealthResponse {
// default: ok
string status = 1;
// default: ok
string status = 1;
}
message StatsRequest {
// optional service name
string service = 1;
// optional service name
string service = 1;
}
message StatsResponse {
// timestamp of recording
uint64 timestamp = 1;
// unix timestamp
uint64 started = 2;
// in seconds
uint64 uptime = 3;
// in bytes
uint64 memory = 4;
// num threads
uint64 threads = 5;
// total gc in nanoseconds
uint64 gc = 6;
// total number of requests
uint64 requests = 7;
// total number of errors
uint64 errors = 8;
// timestamp of recording
uint64 timestamp = 1;
// unix timestamp
uint64 started = 2;
// in seconds
uint64 uptime = 3;
// in bytes
uint64 memory = 4;
// num threads
uint64 threads = 5;
// total gc in nanoseconds
uint64 gc = 6;
// total number of requests
uint64 requests = 7;
// total number of errors
uint64 errors = 8;
}
// LogRequest requests service logs
message LogRequest {
// service to request logs for
string service = 1;
// stream records continuously
bool stream = 2;
// count of records to request
int64 count = 3;
// relative time in seconds
// before the current time
// from which to show logs
int64 since = 4;
// service to request logs for
string service = 1;
// stream records continuously
bool stream = 2;
// count of records to request
int64 count = 3;
// relative time in seconds
// before the current time
// from which to show logs
int64 since = 4;
}
// Record is service log record
// Also used as default basic message type to test requests.
message Record {
// timestamp of log record
int64 timestamp = 1;
// record metadata
map<string,string> metadata = 2;
// message
string message = 3;
// timestamp of log record
int64 timestamp = 1;
// record metadata
map<string, string> metadata = 2;
// message
string message = 3;
}
message TraceRequest {
// trace id to retrieve
string id = 1;
}
message TraceResponse {
repeated Span spans = 1;
// trace id to retrieve
string id = 1;
}
message TraceResponse { repeated Span spans = 1; }
enum SpanType {
INBOUND = 0;
OUTBOUND = 1;
INBOUND = 0;
OUTBOUND = 1;
}
message Span {
// the trace id
string trace = 1;
// id of the span
string id = 2;
// parent span
string parent = 3;
// name of the resource
string name = 4;
// time of start in nanoseconds
uint64 started = 5;
// duration of the execution in nanoseconds
uint64 duration = 6;
// associated metadata
map<string,string> metadata = 7;
SpanType type = 8;
// the trace id
string trace = 1;
// id of the span
string id = 2;
// parent span
string parent = 3;
// name of the resource
string name = 4;
// time of start in nanoseconds
uint64 started = 5;
// duration of the execution in nanoseconds
uint64 duration = 6;
// associated metadata
map<string, string> metadata = 7;
SpanType type = 8;
}