From 4e965e4ce5323eba7dde720c4d1e62bffb6a307b Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Tue, 26 Nov 2019 15:39:55 +0000 Subject: [PATCH 01/15] First commit. Modified proto. Added empty handler method. --- debug/handler/debug.go | 4 + debug/proto/debug.pb.go | 157 ++++++++++++++++++++++++++++------ debug/proto/debug.pb.micro.go | 84 +++++++++++++++++- debug/proto/debug.proto | 30 +++++-- 4 files changed, 243 insertions(+), 32 deletions(-) diff --git a/debug/handler/debug.go b/debug/handler/debug.go index 973a6fc4..51a8aa9f 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -39,3 +39,7 @@ func (d *Debug) Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.S rsp.Threads = uint64(runtime.NumGoroutine()) return nil } + +func (d *Debug) Log(ctx context.Context, req *proto.LogRequest, rsp *proto.Debug_LogStream) error { + return nil +} diff --git a/debug/proto/debug.pb.go b/debug/proto/debug.pb.go index 316df6a8..d4ceaa11 100644 --- a/debug/proto/debug.pb.go +++ b/debug/proto/debug.pb.go @@ -1,5 +1,5 @@ // Code generated by protoc-gen-go. DO NOT EDIT. -// source: github.com/micro/go-micro/debug/proto/debug.proto +// source: debug.proto package debug @@ -30,7 +30,7 @@ func (m *HealthRequest) Reset() { *m = HealthRequest{} } func (m *HealthRequest) String() string { return proto.CompactTextString(m) } func (*HealthRequest) ProtoMessage() {} func (*HealthRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_7cb19b1a05a6e0a9, []int{0} + return fileDescriptor_8d9d361be58531fb, []int{0} } func (m *HealthRequest) XXX_Unmarshal(b []byte) error { @@ -63,7 +63,7 @@ func (m *HealthResponse) Reset() { *m = HealthResponse{} } func (m *HealthResponse) String() string { return proto.CompactTextString(m) } func (*HealthResponse) ProtoMessage() {} func (*HealthResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_7cb19b1a05a6e0a9, []int{1} + return fileDescriptor_8d9d361be58531fb, []int{1} } func (m *HealthResponse) XXX_Unmarshal(b []byte) error { @@ -101,7 +101,7 @@ func (m *StatsRequest) Reset() { *m = StatsRequest{} } func (m *StatsRequest) String() string { return proto.CompactTextString(m) } func (*StatsRequest) ProtoMessage() {} func (*StatsRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_7cb19b1a05a6e0a9, []int{2} + return fileDescriptor_8d9d361be58531fb, []int{2} } func (m *StatsRequest) XXX_Unmarshal(b []byte) error { @@ -142,7 +142,7 @@ func (m *StatsResponse) Reset() { *m = StatsResponse{} } func (m *StatsResponse) String() string { return proto.CompactTextString(m) } func (*StatsResponse) ProtoMessage() {} func (*StatsResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_7cb19b1a05a6e0a9, []int{3} + return fileDescriptor_8d9d361be58531fb, []int{3} } func (m *StatsResponse) XXX_Unmarshal(b []byte) error { @@ -198,32 +198,139 @@ func (m *StatsResponse) GetGc() uint64 { return 0 } +// LogRequest queries service for logs +type LogRequest struct { + // count is the count of events + Count uint64 `protobuf:"varint,1,opt,name=count,proto3" json:"count,omitempty"` + // relative time in seconds + // before the current time + // from which to show logs + Since uint64 `protobuf:"varint,2,opt,name=since,proto3" json:"since,omitempty"` + // stream logs continuously + Stream bool `protobuf:"varint,3,opt,name=stream,proto3" json:"stream,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *LogRequest) Reset() { *m = LogRequest{} } +func (m *LogRequest) String() string { return proto.CompactTextString(m) } +func (*LogRequest) ProtoMessage() {} +func (*LogRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_8d9d361be58531fb, []int{4} +} + +func (m *LogRequest) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_LogRequest.Unmarshal(m, b) +} +func (m *LogRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_LogRequest.Marshal(b, m, deterministic) +} +func (m *LogRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_LogRequest.Merge(m, src) +} +func (m *LogRequest) XXX_Size() int { + return xxx_messageInfo_LogRequest.Size(m) +} +func (m *LogRequest) XXX_DiscardUnknown() { + xxx_messageInfo_LogRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_LogRequest proto.InternalMessageInfo + +func (m *LogRequest) GetCount() uint64 { + if m != nil { + return m.Count + } + return 0 +} + +func (m *LogRequest) GetSince() uint64 { + if m != nil { + return m.Since + } + return 0 +} + +func (m *LogRequest) GetStream() bool { + if m != nil { + return m.Stream + } + return false +} + +// LogEvent is service log event +type LogEvent struct { + // event log record + // TODO: change this + Record string `protobuf:"bytes,1,opt,name=record,proto3" json:"record,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *LogEvent) Reset() { *m = LogEvent{} } +func (m *LogEvent) String() string { return proto.CompactTextString(m) } +func (*LogEvent) ProtoMessage() {} +func (*LogEvent) Descriptor() ([]byte, []int) { + return fileDescriptor_8d9d361be58531fb, []int{5} +} + +func (m *LogEvent) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_LogEvent.Unmarshal(m, b) +} +func (m *LogEvent) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_LogEvent.Marshal(b, m, deterministic) +} +func (m *LogEvent) XXX_Merge(src proto.Message) { + xxx_messageInfo_LogEvent.Merge(m, src) +} +func (m *LogEvent) XXX_Size() int { + return xxx_messageInfo_LogEvent.Size(m) +} +func (m *LogEvent) XXX_DiscardUnknown() { + xxx_messageInfo_LogEvent.DiscardUnknown(m) +} + +var xxx_messageInfo_LogEvent proto.InternalMessageInfo + +func (m *LogEvent) GetRecord() string { + if m != nil { + return m.Record + } + return "" +} + func init() { proto.RegisterType((*HealthRequest)(nil), "HealthRequest") proto.RegisterType((*HealthResponse)(nil), "HealthResponse") proto.RegisterType((*StatsRequest)(nil), "StatsRequest") proto.RegisterType((*StatsResponse)(nil), "StatsResponse") + proto.RegisterType((*LogRequest)(nil), "LogRequest") + proto.RegisterType((*LogEvent)(nil), "LogEvent") } -func init() { - proto.RegisterFile("github.com/micro/go-micro/debug/proto/debug.proto", fileDescriptor_7cb19b1a05a6e0a9) -} +func init() { proto.RegisterFile("debug.proto", fileDescriptor_8d9d361be58531fb) } -var fileDescriptor_7cb19b1a05a6e0a9 = []byte{ - // 237 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x90, 0x41, 0x4b, 0xc4, 0x30, - 0x14, 0x84, 0x77, 0xeb, 0xb6, 0xe2, 0xc3, 0x66, 0x21, 0x07, 0x09, 0x7b, 0x92, 0x9c, 0x0a, 0x62, - 0x8a, 0xfa, 0x17, 0x3c, 0x78, 0xae, 0x77, 0x21, 0x6d, 0x43, 0x5a, 0x30, 0xa6, 0x26, 0x2f, 0x07, - 0xcf, 0xfe, 0x71, 0x69, 0x92, 0x82, 0xbd, 0xcd, 0x4c, 0x98, 0x21, 0xdf, 0x83, 0x27, 0x3d, 0xe3, - 0x14, 0x7a, 0x31, 0x58, 0xd3, 0x9a, 0x79, 0x70, 0xb6, 0xd5, 0xf6, 0x31, 0x89, 0x51, 0xf5, 0x41, - 0xb7, 0x8b, 0xb3, 0x98, 0xb5, 0x88, 0x9a, 0x9f, 0xa1, 0x7e, 0x53, 0xf2, 0x13, 0xa7, 0x4e, 0x7d, - 0x07, 0xe5, 0x91, 0x37, 0x40, 0xb6, 0xc0, 0x2f, 0xf6, 0xcb, 0x2b, 0x7a, 0x07, 0x95, 0x47, 0x89, - 0xc1, 0xb3, 0xe3, 0xfd, 0xb1, 0xb9, 0xe9, 0xb2, 0xe3, 0x04, 0x6e, 0xdf, 0x51, 0xa2, 0xdf, 0x9a, - 0xbf, 0x47, 0xa8, 0x73, 0x90, 0x9b, 0x0c, 0xae, 0x3d, 0x4a, 0x87, 0x6a, 0x8c, 0xd5, 0x53, 0xb7, - 0xd9, 0x75, 0x33, 0x2c, 0x38, 0x1b, 0xc5, 0x8a, 0xf8, 0x90, 0xdd, 0x9a, 0x1b, 0x65, 0xac, 0xfb, - 0x61, 0x57, 0x29, 0x4f, 0x6e, 0x5d, 0xc2, 0xc9, 0x29, 0x39, 0x7a, 0x76, 0x4a, 0x4b, 0xd9, 0x52, - 0x02, 0x85, 0x1e, 0x58, 0x19, 0xc3, 0x42, 0x0f, 0xcf, 0x1f, 0x50, 0xbe, 0xae, 0x7c, 0xf4, 0x01, - 0xaa, 0x04, 0x42, 0x89, 0xd8, 0x21, 0x5e, 0xce, 0x62, 0x4f, 0xc8, 0x0f, 0xb4, 0x81, 0x32, 0x7e, - 0x9d, 0xd6, 0xe2, 0x3f, 0xd3, 0x85, 0x88, 0x1d, 0x11, 0x3f, 0xf4, 0x55, 0xbc, 0xdb, 0xcb, 0x5f, - 0x00, 0x00, 0x00, 0xff, 0xff, 0x20, 0xb8, 0xfe, 0x98, 0x6c, 0x01, 0x00, 0x00, +var fileDescriptor_8d9d361be58531fb = []byte{ + // 290 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x91, 0xdd, 0x4a, 0xc3, 0x30, + 0x14, 0xc7, 0xd7, 0x6d, 0x9d, 0xdb, 0x99, 0xeb, 0x20, 0x88, 0x84, 0x5d, 0x69, 0xae, 0x06, 0x42, + 0x10, 0x7d, 0x05, 0x05, 0x2f, 0x76, 0x21, 0xf5, 0x09, 0xb2, 0xf6, 0x90, 0x0d, 0x6c, 0x33, 0x93, + 0x53, 0xc1, 0x4b, 0xf1, 0xc5, 0x25, 0x5f, 0xe8, 0x2e, 0x7f, 0xff, 0xf6, 0x7c, 0xfd, 0x02, 0xcb, + 0x16, 0xf7, 0x83, 0x96, 0x27, 0x6b, 0xc8, 0x88, 0x35, 0xac, 0x5e, 0x50, 0xbd, 0xd3, 0xa1, 0xc6,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xf2, 0xef, 0xb8, 0xcd, 0x42, 0xe6, 0xbd, 0xc4, 0xe8, 0xbe, 0xd8, + 0xcf, 0xc2, 0x8b, 0x3e, 0xfe, 0x06, 0x00, 0x00, 0xff, 0xff, 0x70, 0x27, 0xbe, 0x8c, 0xe0, 0x01, + 0x00, 0x00, } diff --git a/debug/proto/debug.pb.micro.go b/debug/proto/debug.pb.micro.go index 3dcc256f..97cf7345 100644 --- a/debug/proto/debug.pb.micro.go +++ b/debug/proto/debug.pb.micro.go @@ -1,5 +1,5 @@ // Code generated by protoc-gen-micro. DO NOT EDIT. -// source: github.com/micro/go-micro/debug/proto/debug.proto +// source: debug.proto package debug @@ -36,6 +36,7 @@ var _ server.Option 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) + Log(ctx context.Context, in *LogRequest, opts ...client.CallOption) (Debug_LogService, error) } type debugService struct { @@ -76,17 +77,63 @@ func (c *debugService) Stats(ctx context.Context, in *StatsRequest, opts ...clie return out, nil } +func (c *debugService) Log(ctx context.Context, in *LogRequest, opts ...client.CallOption) (Debug_LogService, error) { + req := c.c.NewRequest(c.name, "Debug.Log", &LogRequest{}) + stream, err := c.c.Stream(ctx, req, opts...) + if err != nil { + return nil, err + } + if err := stream.Send(in); err != nil { + return nil, err + } + return &debugServiceLog{stream}, nil +} + +type Debug_LogService interface { + SendMsg(interface{}) error + RecvMsg(interface{}) error + Close() error + Recv() (*LogEvent, error) +} + +type debugServiceLog struct { + stream client.Stream +} + +func (x *debugServiceLog) Close() error { + return x.stream.Close() +} + +func (x *debugServiceLog) SendMsg(m interface{}) error { + return x.stream.Send(m) +} + +func (x *debugServiceLog) RecvMsg(m interface{}) error { + return x.stream.Recv(m) +} + +func (x *debugServiceLog) Recv() (*LogEvent, error) { + m := new(LogEvent) + err := x.stream.Recv(m) + if err != nil { + return nil, err + } + return m, nil +} + // Server API for Debug service type DebugHandler interface { Health(context.Context, *HealthRequest, *HealthResponse) error Stats(context.Context, *StatsRequest, *StatsResponse) error + Log(context.Context, *LogRequest, Debug_LogStream) error } func RegisterDebugHandler(s server.Server, hdlr DebugHandler, opts ...server.HandlerOption) error { type debug interface { Health(ctx context.Context, in *HealthRequest, out *HealthResponse) error Stats(ctx context.Context, in *StatsRequest, out *StatsResponse) error + Log(ctx context.Context, stream server.Stream) error } type Debug struct { debug @@ -106,3 +153,38 @@ func (h *debugHandler) Health(ctx context.Context, in *HealthRequest, out *Healt func (h *debugHandler) Stats(ctx context.Context, in *StatsRequest, out *StatsResponse) error { return h.DebugHandler.Stats(ctx, in, out) } + +func (h *debugHandler) Log(ctx context.Context, stream server.Stream) error { + m := new(LogRequest) + if err := stream.Recv(m); err != nil { + return err + } + return h.DebugHandler.Log(ctx, m, &debugLogStream{stream}) +} + +type Debug_LogStream interface { + SendMsg(interface{}) error + RecvMsg(interface{}) error + Close() error + Send(*LogEvent) error +} + +type debugLogStream struct { + stream server.Stream +} + +func (x *debugLogStream) Close() error { + return x.stream.Close() +} + +func (x *debugLogStream) SendMsg(m interface{}) error { + return x.stream.Send(m) +} + +func (x *debugLogStream) RecvMsg(m interface{}) error { + return x.stream.Recv(m) +} + +func (x *debugLogStream) Send(m *LogEvent) error { + return x.stream.Send(m) +} diff --git a/debug/proto/debug.proto b/debug/proto/debug.proto index b642cd41..648e48a8 100644 --- a/debug/proto/debug.proto +++ b/debug/proto/debug.proto @@ -1,20 +1,19 @@ syntax = "proto3"; service Debug { - rpc Health(HealthRequest) returns (HealthResponse) {} - rpc Stats(StatsRequest) returns (StatsResponse) {} + rpc Health(HealthRequest) returns (HealthResponse) {}; + rpc Stats(StatsRequest) returns (StatsResponse) {}; + rpc Log(LogRequest) returns (stream LogEvent) {}; } -message HealthRequest { -} +message HealthRequest {} message HealthResponse { // default: ok string status = 1; } -message StatsRequest { -} +message StatsRequest {} message StatsResponse { // unix timestamp @@ -28,3 +27,22 @@ message StatsResponse { // total gc in nanoseconds uint64 gc = 5; } + +// LogRequest queries service for logs +message LogRequest { + // count is the count of events + uint64 count = 1; + // relative time in seconds + // before the current time + // from which to show logs + uint64 since = 2; + // stream logs continuously + bool stream = 3; +} + +// LogEvent is service log event +message LogEvent { + // event log record + // TODO: change this + string record = 1; +} From 3f7f2afc7bc4bd48cd7aad105cba662ac8a915b1 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Tue, 26 Nov 2019 17:04:44 +0000 Subject: [PATCH 02/15] Dont be stupid -- stream does not require pointer. --- debug/handler/debug.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/debug/handler/debug.go b/debug/handler/debug.go index 51a8aa9f..d59d6b75 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -8,15 +8,15 @@ import ( proto "github.com/micro/go-micro/debug/proto" ) -type Debug struct { - proto.DebugHandler - started int64 -} - var ( + // DefaultHandler is default debug handler DefaultHandler = newDebug() ) +type Debug struct { + started int64 +} + func newDebug() *Debug { return &Debug{ started: time.Now().Unix(), @@ -40,6 +40,6 @@ func (d *Debug) Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.S return nil } -func (d *Debug) Log(ctx context.Context, req *proto.LogRequest, rsp *proto.Debug_LogStream) error { +func (d *Debug) Log(ctx context.Context, req *proto.LogRequest, rsp proto.Debug_LogStream) error { return nil } From ee9776e7b20af25310d0155c4945e8dd62da0797 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Wed, 27 Nov 2019 16:02:16 +0000 Subject: [PATCH 03/15] Added debug.Logger interface --- debug/buffer/buffer.go | 16 ++-- debug/log/default.go | 48 +++++++++++ debug/log/default_test.go | 26 ++++++ debug/log/logger.go | 171 ++++++++++++++++++++++++++++++++++++++ debug/log/options.go | 24 ++++++ 5 files changed, 278 insertions(+), 7 deletions(-) create mode 100644 debug/log/default.go create mode 100644 debug/log/default_test.go create mode 100644 debug/log/logger.go create mode 100644 debug/log/options.go diff --git a/debug/buffer/buffer.go b/debug/buffer/buffer.go index 8c55790c..50c511d4 100644 --- a/debug/buffer/buffer.go +++ b/debug/buffer/buffer.go @@ -17,6 +17,14 @@ type Entry struct { Timestamp time.Time } +// New returns a new buffer of the given size +func New(i int) *Buffer { + return &Buffer{ + size: i, + } +} + +// Put adds a new value to ring buffer func (b *Buffer) Put(v interface{}) { b.Lock() defer b.Unlock() @@ -83,13 +91,7 @@ func (b *Buffer) Since(t time.Time) []*Entry { return nil } +// Size returns the size of the ring buffer func (b *Buffer) Size() int { return b.size } - -// New returns a new buffer of the given size -func New(i int) *Buffer { - return &Buffer{ - size: i, - } -} diff --git a/debug/log/default.go b/debug/log/default.go new file mode 100644 index 00000000..9059c104 --- /dev/null +++ b/debug/log/default.go @@ -0,0 +1,48 @@ +package log + +import ( + "fmt" + golog "log" + + "github.com/micro/go-micro/debug/buffer" +) + +var ( + // DefaultSize of the logger buffer + DefaultSize = 1000 +) + +// defaultLogger is default micro logger +type defaultLogger struct { + *buffer.Buffer +} + +// NewLogger returns default Logger with +func NewLogger(opts ...Option) Logger { + // get default options + options := DefaultOptions() + + // apply requested options + for _, o := range opts { + o(&options) + } + + return &defaultLogger{ + Buffer: buffer.New(options.Size), + } +} + +// Write writes log into logger +func (l *defaultLogger) Write(v ...interface{}) { + l.log(fmt.Sprint(v...)) + golog.Print(v...) +} + +// Read reads logs from the logger +func (l *defaultLogger) Read(n int) []interface{} { + return l.Get(n) +} + +func (l *defaultLogger) log(entry string) { + l.Buffer.Put(entry) +} diff --git a/debug/log/default_test.go b/debug/log/default_test.go new file mode 100644 index 00000000..53ce9a92 --- /dev/null +++ b/debug/log/default_test.go @@ -0,0 +1,26 @@ +package log + +import ( + "reflect" + "testing" +) + +func TestLogger(t *testing.T) { + // make sure we have the right size of the logger ring buffer + if logger.(*defaultLogger).Size() != DefaultSize { + t.Errorf("expected buffer size: %d, got: %d", DefaultSize, logger.(*defaultLogger).Size()) + } + + // Log some cruft + Log("foobar") + Logf("foo %s", "bar") + + // Check if the logs are stored in the logger ring buffer + expectedEntries := []string{"foobar", "foo bar"} + entries := logger.Read(len(expectedEntries)) + for i, entry := range entries { + if !reflect.DeepEqual(entry, expectedEntries[i]) { + t.Errorf("expected %s, got %s", expectedEntries[i], entry) + } + } +} diff --git a/debug/log/logger.go b/debug/log/logger.go new file mode 100644 index 00000000..f46666f1 --- /dev/null +++ b/debug/log/logger.go @@ -0,0 +1,171 @@ +// Package log provides debug logging +package log + +import ( + "fmt" + "os" +) + +var ( + // logger is default Logger + logger Logger = NewLogger() + // default log level is info + level = LevelInfo + // prefix for all messages + prefix string +) + +// Logger is event logger +type Logger interface { + // Read reads specified number of log entries + Read(int) []interface{} + // Write writes logs to logger + Write(...interface{}) +} + +// level is a log level +type Level int + +const ( + LevelFatal Level = iota + LevelError + LevelInfo + LevelWarn + LevelDebug + LevelTrace +) + +func init() { + switch os.Getenv("MICRO_LOG_LEVEL") { + case "trace": + level = LevelTrace + case "debug": + level = LevelDebug + case "warn": + level = LevelWarn + case "info": + level = LevelInfo + case "error": + level = LevelError + case "fatal": + level = LevelFatal + } +} + +func Log(v ...interface{}) { + if len(prefix) > 0 { + logger.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...)) + return + } + logger.Write(fmt.Sprint(v...)) +} + +func Logf(format string, v ...interface{}) { + if len(prefix) > 0 { + format = prefix + " " + format + } + logger.Write(fmt.Sprintf(format, v...)) +} + +// WithLevel logs with the level specified +func WithLevel(l Level, v ...interface{}) { + if l > level { + return + } + Log(v...) +} + +// WithLevel logs with the level specified +func WithLevelf(l Level, format string, v ...interface{}) { + if l > level { + return + } + Logf(format, v...) +} + +// Trace provides trace level logging +func Trace(v ...interface{}) { + WithLevel(LevelTrace, v...) +} + +// Tracef provides trace level logging +func Tracef(format string, v ...interface{}) { + WithLevelf(LevelTrace, format, v...) +} + +// Debug provides debug level logging +func Debug(v ...interface{}) { + WithLevel(LevelDebug, v...) +} + +// Debugf provides debug level logging +func Debugf(format string, v ...interface{}) { + WithLevelf(LevelDebug, format, v...) +} + +// Warn provides warn level logging +func Warn(v ...interface{}) { + WithLevel(LevelWarn, v...) +} + +// Warnf provides warn level logging +func Warnf(format string, v ...interface{}) { + WithLevelf(LevelWarn, format, v...) +} + +// Info provides info level logging +func Info(v ...interface{}) { + WithLevel(LevelInfo, v...) +} + +// Infof provides info level logging +func Infof(format string, v ...interface{}) { + WithLevelf(LevelInfo, format, v...) +} + +// Error provides warn level logging +func Error(v ...interface{}) { + WithLevel(LevelError, v...) +} + +// Errorf provides warn level logging +func Errorf(format string, v ...interface{}) { + WithLevelf(LevelError, format, v...) +} + +// Fatal logs with Log and then exits with os.Exit(1) +func Fatal(v ...interface{}) { + WithLevel(LevelFatal, v...) + os.Exit(1) +} + +// Fatalf logs with Logf and then exits with os.Exit(1) +func Fatalf(format string, v ...interface{}) { + WithLevelf(LevelFatal, format, v...) + os.Exit(1) +} + +// GetLogger returns the local logger +func GetLogger() Logger { + return logger +} + +// SetLevel sets the log level +func SetLevel(l Level) { + level = l +} + +// GetLevel returns the current level +func GetLevel() Level { + return level +} + +// Set a prefix for the logger +func SetPrefix(p string) { + prefix = p +} + +// Set service name +func Name(name string) { + prefix = fmt.Sprintf("[%s]", name) +} diff --git a/debug/log/options.go b/debug/log/options.go new file mode 100644 index 00000000..cd2fafcd --- /dev/null +++ b/debug/log/options.go @@ -0,0 +1,24 @@ +package log + +// Option used by the logger +type Option func(*Options) + +// Options are logger options +type Options struct { + // Size is the size of ring buffer + Size int +} + +// Size sets the size of the ring buffer +func Size(s int) Option { + return func(o *Options) { + o.Size = s + } +} + +// DefaultOptions returns default options +func DefaultOptions() Options { + return Options{ + Size: DefaultSize, + } +} From 9e177be560a05e20dd7d041f3e685d76f8ab7b59 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Wed, 27 Nov 2019 16:12:39 +0000 Subject: [PATCH 04/15] Embed logger into debug.Handler --- debug/handler/debug.go | 3 +++ debug/log/default.go | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/debug/handler/debug.go b/debug/handler/debug.go index d59d6b75..c7095605 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -5,6 +5,8 @@ import ( "runtime" "time" + "github.com/micro/go-micro/debug/log" + proto "github.com/micro/go-micro/debug/proto" ) @@ -15,6 +17,7 @@ var ( type Debug struct { started int64 + log.Logger } func newDebug() *Debug { diff --git a/debug/log/default.go b/debug/log/default.go index 9059c104..641bf037 100644 --- a/debug/log/default.go +++ b/debug/log/default.go @@ -40,7 +40,12 @@ func (l *defaultLogger) Write(v ...interface{}) { // Read reads logs from the logger func (l *defaultLogger) Read(n int) []interface{} { - return l.Get(n) + entries := l.Get(n) + vals := make([]interface{}, 0, len(entries)) + for _, val := range entries { + vals = append(vals, val) + } + return vals } func (l *defaultLogger) log(entry string) { From 7deafbc5ce4e91651430b1355b560f5e5efa7947 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Wed, 27 Nov 2019 17:31:35 +0000 Subject: [PATCH 05/15] Added ReadOptions; Changed proto; Reimplemented Log(er) --- debug/handler/debug.go | 4 +- debug/log/default.go | 48 +++++++++----- debug/log/default_test.go | 22 +++--- debug/log/{logger.go => log.go} | 34 ++++++---- debug/log/options.go | 27 ++++++++ debug/proto/debug.pb.go | 114 +++++++++++++++++++------------- debug/proto/debug.pb.micro.go | 46 ++++++------- debug/proto/debug.proto | 15 +++-- 8 files changed, 195 insertions(+), 115 deletions(-) rename debug/log/{logger.go => log.go} (86%) diff --git a/debug/handler/debug.go b/debug/handler/debug.go index c7095605..8b8eedfd 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -17,7 +17,7 @@ var ( type Debug struct { started int64 - log.Logger + log.Log } func newDebug() *Debug { @@ -43,6 +43,6 @@ func (d *Debug) Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.S return nil } -func (d *Debug) Log(ctx context.Context, req *proto.LogRequest, rsp proto.Debug_LogStream) error { +func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, rsp proto.Debug_LogsStream) error { return nil } diff --git a/debug/log/default.go b/debug/log/default.go index 641bf037..b3840909 100644 --- a/debug/log/default.go +++ b/debug/log/default.go @@ -12,13 +12,13 @@ var ( DefaultSize = 1000 ) -// defaultLogger is default micro logger -type defaultLogger struct { +// defaultLog is default micro log +type defaultLog struct { *buffer.Buffer } -// NewLogger returns default Logger with -func NewLogger(opts ...Option) Logger { +// NewLog returns default Logger with +func NewLog(opts ...Option) Log { // get default options options := DefaultOptions() @@ -27,27 +27,43 @@ func NewLogger(opts ...Option) Logger { o(&options) } - return &defaultLogger{ + return &defaultLog{ Buffer: buffer.New(options.Size), } } // Write writes log into logger -func (l *defaultLogger) Write(v ...interface{}) { - l.log(fmt.Sprint(v...)) +func (l *defaultLog) Write(v ...interface{}) { + l.Buffer.Put(fmt.Sprint(v...)) golog.Print(v...) } // Read reads logs from the logger -func (l *defaultLogger) Read(n int) []interface{} { - entries := l.Get(n) - vals := make([]interface{}, 0, len(entries)) - for _, val := range entries { - vals = append(vals, val) +func (l *defaultLog) Read(opts ...ReadOption) []Record { + options := ReadOptions{} + // initialize the read options + for _, o := range opts { + o(&options) } - return vals -} -func (l *defaultLogger) log(entry string) { - l.Buffer.Put(entry) + var entries []*buffer.Entry + // if Since options ha sbeen specified we honor it + if !options.Since.IsZero() { + entries = l.Buffer.Since(options.Since) + } else { + // otherwie return last count entries + entries = l.Buffer.Get(options.Count) + } + + // TODO: if both Since and Count are set should we return + // last Count from the returned time scoped entries? + records := make([]Record, 0, len(entries)) + for _, entry := range entries { + record := Record{ + Timestamp: entry.Timestamp, + Value: entry.Value, + } + records = append(records, record) + } + return records } diff --git a/debug/log/default_test.go b/debug/log/default_test.go index 53ce9a92..0ef6e903 100644 --- a/debug/log/default_test.go +++ b/debug/log/default_test.go @@ -6,21 +6,27 @@ import ( ) func TestLogger(t *testing.T) { + // set size to some value + size := 100 + // override the global logger + logger = NewLog(Size(size)) // make sure we have the right size of the logger ring buffer - if logger.(*defaultLogger).Size() != DefaultSize { - t.Errorf("expected buffer size: %d, got: %d", DefaultSize, logger.(*defaultLogger).Size()) + if logger.(*defaultLog).Size() != size { + t.Errorf("expected buffer size: %d, got: %d", size, logger.(*defaultLog).Size()) } // Log some cruft - Log("foobar") - Logf("foo %s", "bar") + Info("foobar") + // increase the log level + level = LevelDebug + Debugf("foo %s", "bar") // Check if the logs are stored in the logger ring buffer - expectedEntries := []string{"foobar", "foo bar"} - entries := logger.Read(len(expectedEntries)) + expected := []string{"foobar", "foo bar"} + entries := logger.Read(Count(len(expected))) for i, entry := range entries { - if !reflect.DeepEqual(entry, expectedEntries[i]) { - t.Errorf("expected %s, got %s", expectedEntries[i], entry) + if !reflect.DeepEqual(entry.Value, expected[i]) { + t.Errorf("expected %s, got %s", expected[i], entry.Value) } } } diff --git a/debug/log/logger.go b/debug/log/log.go similarity index 86% rename from debug/log/logger.go rename to debug/log/log.go index f46666f1..1c9ae240 100644 --- a/debug/log/logger.go +++ b/debug/log/log.go @@ -4,25 +4,36 @@ package log import ( "fmt" "os" + "time" ) var ( // logger is default Logger - logger Logger = NewLogger() + logger Log = NewLog() // default log level is info level = LevelInfo // prefix for all messages prefix string ) -// Logger is event logger -type Logger interface { - // Read reads specified number of log entries - Read(int) []interface{} +// Log is event log +type Log interface { + // Read reads log entries from the logger + Read(...ReadOption) []Record // Write writes logs to logger Write(...interface{}) } +// Record is log record entry +type Record struct { + // Timestamp of logged event + Timestamp time.Time + // Value contains log entry + Value interface{} + // Metadata to enrich log record + Metadata map[string]string +} + // level is a log level type Level int @@ -52,7 +63,7 @@ func init() { } } -func Log(v ...interface{}) { +func log(v ...interface{}) { if len(prefix) > 0 { logger.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...)) return @@ -60,7 +71,7 @@ func Log(v ...interface{}) { logger.Write(fmt.Sprint(v...)) } -func Logf(format string, v ...interface{}) { +func logf(format string, v ...interface{}) { if len(prefix) > 0 { format = prefix + " " + format } @@ -72,7 +83,7 @@ func WithLevel(l Level, v ...interface{}) { if l > level { return } - Log(v...) + log(v...) } // WithLevel logs with the level specified @@ -80,7 +91,7 @@ func WithLevelf(l Level, format string, v ...interface{}) { if l > level { return } - Logf(format, v...) + logf(format, v...) } // Trace provides trace level logging @@ -145,11 +156,6 @@ func Fatalf(format string, v ...interface{}) { os.Exit(1) } -// GetLogger returns the local logger -func GetLogger() Logger { - return logger -} - // SetLevel sets the log level func SetLevel(l Level) { level = l diff --git a/debug/log/options.go b/debug/log/options.go index cd2fafcd..2320b71e 100644 --- a/debug/log/options.go +++ b/debug/log/options.go @@ -1,5 +1,7 @@ package log +import "time" + // Option used by the logger type Option func(*Options) @@ -22,3 +24,28 @@ func DefaultOptions() Options { Size: DefaultSize, } } + +// ReadOptions for querying the logs +type ReadOptions struct { + // Since what time in past to return the logs + Since time.Time + // Count specifies number of logs to return + Count int +} + +// ReadOption used for reading the logs +type ReadOption func(*ReadOptions) + +// Since sets the time since which to return the log records +func Since(s time.Time) ReadOption { + return func(o *ReadOptions) { + o.Since = s + } +} + +// Count sets the number of log records to return +func Count(c int) ReadOption { + return func(o *ReadOptions) { + o.Count = c + } +} diff --git a/debug/proto/debug.pb.go b/debug/proto/debug.pb.go index d4ceaa11..b8236261 100644 --- a/debug/proto/debug.pb.go +++ b/debug/proto/debug.pb.go @@ -259,78 +259,100 @@ func (m *LogRequest) GetStream() bool { return false } -// LogEvent is service log event -type LogEvent struct { - // event log record - // TODO: change this - Record string `protobuf:"bytes,1,opt,name=record,proto3" json:"record,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` +// Log is service log record +type Log struct { + // timestamp of log event + Timestamp int64 `protobuf:"varint,1,opt,name=timestamp,proto3" json:"timestamp,omitempty"` + // log value + Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` + // metadata + Metadata map[string]string `protobuf:"bytes,3,rep,name=metadata,proto3" json:"metadata,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } -func (m *LogEvent) Reset() { *m = LogEvent{} } -func (m *LogEvent) String() string { return proto.CompactTextString(m) } -func (*LogEvent) ProtoMessage() {} -func (*LogEvent) Descriptor() ([]byte, []int) { +func (m *Log) Reset() { *m = Log{} } +func (m *Log) String() string { return proto.CompactTextString(m) } +func (*Log) ProtoMessage() {} +func (*Log) Descriptor() ([]byte, []int) { return fileDescriptor_8d9d361be58531fb, []int{5} } -func (m *LogEvent) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_LogEvent.Unmarshal(m, b) +func (m *Log) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Log.Unmarshal(m, b) } -func (m *LogEvent) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_LogEvent.Marshal(b, m, deterministic) +func (m *Log) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Log.Marshal(b, m, deterministic) } -func (m *LogEvent) XXX_Merge(src proto.Message) { - xxx_messageInfo_LogEvent.Merge(m, src) +func (m *Log) XXX_Merge(src proto.Message) { + xxx_messageInfo_Log.Merge(m, src) } -func (m *LogEvent) XXX_Size() int { - return xxx_messageInfo_LogEvent.Size(m) +func (m *Log) XXX_Size() int { + return xxx_messageInfo_Log.Size(m) } -func (m *LogEvent) XXX_DiscardUnknown() { - xxx_messageInfo_LogEvent.DiscardUnknown(m) +func (m *Log) XXX_DiscardUnknown() { + xxx_messageInfo_Log.DiscardUnknown(m) } -var xxx_messageInfo_LogEvent proto.InternalMessageInfo +var xxx_messageInfo_Log proto.InternalMessageInfo -func (m *LogEvent) GetRecord() string { +func (m *Log) GetTimestamp() int64 { if m != nil { - return m.Record + return m.Timestamp + } + return 0 +} + +func (m *Log) GetValue() string { + if m != nil { + return m.Value } return "" } +func (m *Log) GetMetadata() map[string]string { + if m != nil { + return m.Metadata + } + return nil +} + func init() { proto.RegisterType((*HealthRequest)(nil), "HealthRequest") proto.RegisterType((*HealthResponse)(nil), "HealthResponse") proto.RegisterType((*StatsRequest)(nil), "StatsRequest") proto.RegisterType((*StatsResponse)(nil), "StatsResponse") proto.RegisterType((*LogRequest)(nil), "LogRequest") - proto.RegisterType((*LogEvent)(nil), "LogEvent") + proto.RegisterType((*Log)(nil), "Log") + proto.RegisterMapType((map[string]string)(nil), "Log.MetadataEntry") } func init() { proto.RegisterFile("debug.proto", fileDescriptor_8d9d361be58531fb) } var fileDescriptor_8d9d361be58531fb = []byte{ - // 290 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x91, 0xdd, 0x4a, 0xc3, 0x30, - 0x14, 0xc7, 0xd7, 0x6d, 0x9d, 0xdb, 0x99, 0xeb, 0x20, 0x88, 0x84, 0x5d, 0x69, 0xae, 0x06, 0x42, - 0x10, 0x7d, 0x05, 0x05, 0x2f, 0x76, 0x21, 0xf5, 0x09, 0xb2, 0xf6, 0x90, 0x0d, 0x6c, 0x33, 0x93, - 0x53, 0xc1, 0x4b, 0xf1, 0xc5, 0x25, 0x5f, 0xe8, 0x2e, 0x7f, 0xff, 0xf6, 0x7c, 0xfd, 0x02, 0xcb, - 0x16, 0xf7, 0x83, 0x96, 0x27, 0x6b, 0xc8, 0x88, 0x35, 0xac, 0x5e, 0x50, 0xbd, 0xd3, 0xa1, 0xc6, - 0x8f, 0x01, 0x1d, 0x89, 0x2d, 0x54, 0x39, 0x70, 0x27, 0xd3, 0x3b, 0x64, 0xd7, 0x30, 0x73, 0xa4, - 0x68, 0x70, 0xbc, 0xb8, 0x29, 0xb6, 0x8b, 0x3a, 0x91, 0xa8, 0xe0, 0xf2, 0x8d, 0x14, 0xb9, 0x5c, - 0xf9, 0x53, 0xc0, 0x2a, 0x05, 0xa9, 0x92, 0xc3, 0x85, 0x23, 0x65, 0x09, 0xdb, 0x50, 0x3a, 0xad, - 0x33, 0xfa, 0x9e, 0xc3, 0x89, 0x8e, 0x1d, 0xf2, 0x71, 0xf8, 0x90, 0xc8, 0xe7, 0x1d, 0x76, 0xc6, - 0x7e, 0xf1, 0x49, 0xcc, 0x23, 0xf9, 0x4e, 0x74, 0xb0, 0xa8, 0x5a, 0xc7, 0xa7, 0xb1, 0x53, 0x42, - 0x56, 0xc1, 0x58, 0x37, 0xbc, 0x0c, 0xe1, 0x58, 0x37, 0xe2, 0x15, 0x60, 0x67, 0x74, 0xda, 0x89, - 0x5d, 0x41, 0xd9, 0x98, 0xa1, 0xa7, 0x34, 0x3f, 0x82, 0x4f, 0xdd, 0xb1, 0x6f, 0xf2, 0xf0, 0x08, - 0xf1, 0x4e, 0x8b, 0xaa, 0x0b, 0xb3, 0xe7, 0x75, 0x22, 0x21, 0x60, 0xbe, 0x33, 0xfa, 0xf9, 0x13, - 0x7b, 0xf2, 0xff, 0x58, 0x6c, 0x8c, 0x6d, 0xb3, 0x8b, 0x48, 0x0f, 0xdf, 0x05, 0x94, 0x4f, 0x5e, - 0x2b, 0xbb, 0x83, 0x59, 0xf4, 0xc7, 0x2a, 0x79, 0x66, 0x76, 0xb3, 0x96, 0xe7, 0x62, 0xc5, 0x88, - 0x6d, 0xa1, 0x0c, 0xc6, 0xd8, 0x4a, 0xfe, 0x57, 0xb9, 0xa9, 0xe4, 0x99, 0x48, 0x31, 0x62, 0xb7, - 0x30, 0xd9, 0x19, 0xcd, 0x96, 0xf2, 0xef, 0xb8, 0xcd, 0x42, 0xe6, 0xbd, 0xc4, 0xe8, 0xbe, 0xd8, - 0xcf, 0xc2, 0x8b, 0x3e, 0xfe, 0x06, 0x00, 0x00, 0xff, 0xff, 0x70, 0x27, 0xbe, 0x8c, 0xe0, 0x01, - 0x00, 0x00, + // 359 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 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0xde, 0x89, 0xfc, 0x84, 0xa0, 0x90, + 0x7d, 0x83, 0x36, 0xdf, 0x80, 0x56, 0xd5, 0xa1, 0x29, 0xc6, 0x70, 0x03, 0xe6, 0x3b, 0x3b, 0xc1, + 0xeb, 0x21, 0x7b, 0x91, 0x5b, 0xa2, 0x6f, 0x0e, 0x78, 0x99, 0xac, 0xc8, 0x6f, 0x58, 0xea, 0x3b, + 0x2a, 0xe4, 0x75, 0x3b, 0xf8, 0x79, 0xf9, 0x59, 0xd0, 0x9e, 0xcf, 0xfc, 0xd8, 0x1b, 0xcf, 0x65, + 0x6e, 0x80, 0x30, 0x58, 0xd4, 0x02, 0x79, 0xc9, 0x91, 0xc7, 0xde, 0xc6, 0x4b, 0x57, 0x5b, 0xc2, + 0x32, 0x59, 0xb1, 0x3b, 0x2b, 0xde, 0x34, 0xd8, 0x9d, 0xf2, 0x8f, 0x77, 0x92, 0x2b, 0x08, 0x27, + 0x47, 0xe4, 0x07, 0x78, 0x8f, 0xe2, 0x64, 0x37, 0xaf, 0x1f, 0xbf, 0x0e, 0xba, 0x74, 0x2f, 0x9c, + 0xed, 0x0b, 0x04, 0xd7, 0xba, 0x5a, 0xf2, 0x17, 0xe6, 0xa6, 0x43, 0x12, 0xb1, 0x49, 0xbb, 0xc9, + 0x9a, 0x4d, 0xcb, 0xa5, 0x33, 0x92, 0x42, 0x30, 0xb4, 0x46, 0x42, 0xf6, 0xb9, 0xce, 0x24, 0x62, + 0x93, 0x32, 0xe9, 0x8c, 0xfc, 0x01, 0x3f, 0x93, 0x95, 0x22, 0x2b, 0x76, 0xde, 0x70, 0xe2, 0x6b, + 0xa0, 0xb3, 0x7f, 0xce, 0x6e, 0x3e, 0xfc, 0x51, 0xff, 0xdf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xbe, + 0xa2, 0x1f, 0x47, 0x60, 0x02, 0x00, 0x00, } diff --git a/debug/proto/debug.pb.micro.go b/debug/proto/debug.pb.micro.go index 97cf7345..6f89d5eb 100644 --- a/debug/proto/debug.pb.micro.go +++ b/debug/proto/debug.pb.micro.go @@ -36,7 +36,7 @@ var _ server.Option 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) - Log(ctx context.Context, in *LogRequest, opts ...client.CallOption) (Debug_LogService, error) + Logs(ctx context.Context, in *LogRequest, opts ...client.CallOption) (Debug_LogsService, error) } type debugService struct { @@ -77,8 +77,8 @@ func (c *debugService) Stats(ctx context.Context, in *StatsRequest, opts ...clie return out, nil } -func (c *debugService) Log(ctx context.Context, in *LogRequest, opts ...client.CallOption) (Debug_LogService, error) { - req := c.c.NewRequest(c.name, "Debug.Log", &LogRequest{}) +func (c *debugService) Logs(ctx context.Context, in *LogRequest, opts ...client.CallOption) (Debug_LogsService, error) { + req := c.c.NewRequest(c.name, "Debug.Logs", &LogRequest{}) stream, err := c.c.Stream(ctx, req, opts...) if err != nil { return nil, err @@ -86,34 +86,34 @@ func (c *debugService) Log(ctx context.Context, in *LogRequest, opts ...client.C if err := stream.Send(in); err != nil { return nil, err } - return &debugServiceLog{stream}, nil + return &debugServiceLogs{stream}, nil } -type Debug_LogService interface { +type Debug_LogsService interface { SendMsg(interface{}) error RecvMsg(interface{}) error Close() error - Recv() (*LogEvent, error) + Recv() (*Log, error) } -type debugServiceLog struct { +type debugServiceLogs struct { stream client.Stream } -func (x *debugServiceLog) Close() error { +func (x *debugServiceLogs) Close() error { return x.stream.Close() } -func (x *debugServiceLog) SendMsg(m interface{}) error { +func (x *debugServiceLogs) SendMsg(m interface{}) error { return x.stream.Send(m) } -func (x *debugServiceLog) RecvMsg(m interface{}) error { +func (x *debugServiceLogs) RecvMsg(m interface{}) error { return x.stream.Recv(m) } -func (x *debugServiceLog) Recv() (*LogEvent, error) { - m := new(LogEvent) +func (x *debugServiceLogs) Recv() (*Log, error) { + m := new(Log) err := x.stream.Recv(m) if err != nil { return nil, err @@ -126,14 +126,14 @@ func (x *debugServiceLog) Recv() (*LogEvent, error) { type DebugHandler interface { Health(context.Context, *HealthRequest, *HealthResponse) error Stats(context.Context, *StatsRequest, *StatsResponse) error - Log(context.Context, *LogRequest, Debug_LogStream) error + Logs(context.Context, *LogRequest, Debug_LogsStream) error } func RegisterDebugHandler(s server.Server, hdlr DebugHandler, opts ...server.HandlerOption) error { type debug interface { Health(ctx context.Context, in *HealthRequest, out *HealthResponse) error Stats(ctx context.Context, in *StatsRequest, out *StatsResponse) error - Log(ctx context.Context, stream server.Stream) error + Logs(ctx context.Context, stream server.Stream) error } type Debug struct { debug @@ -154,37 +154,37 @@ func (h *debugHandler) Stats(ctx context.Context, in *StatsRequest, out *StatsRe return h.DebugHandler.Stats(ctx, in, out) } -func (h *debugHandler) Log(ctx context.Context, stream server.Stream) error { +func (h *debugHandler) Logs(ctx context.Context, stream server.Stream) error { m := new(LogRequest) if err := stream.Recv(m); err != nil { return err } - return h.DebugHandler.Log(ctx, m, &debugLogStream{stream}) + return h.DebugHandler.Logs(ctx, m, &debugLogsStream{stream}) } -type Debug_LogStream interface { +type Debug_LogsStream interface { SendMsg(interface{}) error RecvMsg(interface{}) error Close() error - Send(*LogEvent) error + Send(*Log) error } -type debugLogStream struct { +type debugLogsStream struct { stream server.Stream } -func (x *debugLogStream) Close() error { +func (x *debugLogsStream) Close() error { return x.stream.Close() } -func (x *debugLogStream) SendMsg(m interface{}) error { +func (x *debugLogsStream) SendMsg(m interface{}) error { return x.stream.Send(m) } -func (x *debugLogStream) RecvMsg(m interface{}) error { +func (x *debugLogsStream) RecvMsg(m interface{}) error { return x.stream.Recv(m) } -func (x *debugLogStream) Send(m *LogEvent) error { +func (x *debugLogsStream) Send(m *Log) error { return x.stream.Send(m) } diff --git a/debug/proto/debug.proto b/debug/proto/debug.proto index 648e48a8..25030153 100644 --- a/debug/proto/debug.proto +++ b/debug/proto/debug.proto @@ -3,7 +3,7 @@ syntax = "proto3"; service Debug { rpc Health(HealthRequest) returns (HealthResponse) {}; rpc Stats(StatsRequest) returns (StatsResponse) {}; - rpc Log(LogRequest) returns (stream LogEvent) {}; + rpc Logs(LogRequest) returns (stream Log) {}; } message HealthRequest {} @@ -40,9 +40,12 @@ message LogRequest { bool stream = 3; } -// LogEvent is service log event -message LogEvent { - // event log record - // TODO: change this - string record = 1; +// Log is service log record +message Log { + // timestamp of log event + int64 timestamp = 1; + // log value + string value = 2; + // metadata + map metadata = 3; } From b01357058a8e232c9d36a9e95746963054da3790 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Wed, 27 Nov 2019 18:38:26 +0000 Subject: [PATCH 06/15] Regenerate proto cruft --- debug/handler/debug.go | 31 ++++++++++++++++++++++- debug/proto/debug.pb.go | 56 ++++++++++++++++++++--------------------- debug/proto/debug.proto | 4 +-- 3 files changed, 60 insertions(+), 31 deletions(-) diff --git a/debug/handler/debug.go b/debug/handler/debug.go index 8b8eedfd..e532de79 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -43,6 +43,35 @@ func (d *Debug) Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.S return nil } -func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, rsp proto.Debug_LogsStream) error { +func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, stream proto.Debug_LogsStream) error { + var records []log.Record + since := time.Unix(0, req.Since) + if !since.IsZero() { + records = d.Log.Read(log.Since(since)) + } else { + records = d.Log.Read(log.Count(int(req.Count))) + } + + defer stream.Close() + + // TODO: figure out the stream later on + // stream the logs + for _, record := range records { + metadata := make(map[string]string) + for k, v := range record.Metadata { + metadata[k] = v + } + + recLog := &proto.Log{ + Timestamp: record.Timestamp.UnixNano(), + Value: record.Value.(string), + Metadata: metadata, + } + + if err := stream.Send(recLog); err != nil { + return err + } + } + return nil } diff --git a/debug/proto/debug.pb.go b/debug/proto/debug.pb.go index b8236261..423e98cc 100644 --- a/debug/proto/debug.pb.go +++ b/debug/proto/debug.pb.go @@ -201,11 +201,11 @@ func (m *StatsResponse) GetGc() uint64 { // LogRequest queries service for logs type LogRequest struct { // count is the count of events - Count uint64 `protobuf:"varint,1,opt,name=count,proto3" json:"count,omitempty"` + Count int64 `protobuf:"varint,1,opt,name=count,proto3" json:"count,omitempty"` // relative time in seconds // before the current time // from which to show logs - Since uint64 `protobuf:"varint,2,opt,name=since,proto3" json:"since,omitempty"` + Since int64 `protobuf:"varint,2,opt,name=since,proto3" json:"since,omitempty"` // stream logs continuously Stream bool `protobuf:"varint,3,opt,name=stream,proto3" json:"stream,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` @@ -238,14 +238,14 @@ func (m *LogRequest) XXX_DiscardUnknown() { var xxx_messageInfo_LogRequest proto.InternalMessageInfo -func (m *LogRequest) GetCount() uint64 { +func (m *LogRequest) GetCount() int64 { if m != nil { return m.Count } return 0 } -func (m *LogRequest) GetSince() uint64 { +func (m *LogRequest) GetSince() int64 { if m != nil { return m.Since } @@ -331,28 +331,28 @@ func init() { func init() { proto.RegisterFile("debug.proto", fileDescriptor_8d9d361be58531fb) } var fileDescriptor_8d9d361be58531fb = []byte{ - // 359 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0xcd, 0x6a, 0xe3, 0x30, - 0x14, 0x85, 0xe3, 0xbf, 0x4c, 0x72, 0x33, 0x76, 0x06, 0x31, 0x0c, 0xc6, 0xcc, 0x40, 0xd0, 0xca, - 0x30, 0x20, 0x86, 0xcc, 0xa6, 0xb4, 0xdb, 0x16, 0xba, 0x70, 0xa1, 0xb8, 0x4f, 0xa0, 0xd8, 0xc2, - 0x09, 0x8d, 0x2d, 0xd7, 0xba, 0x2e, 0x64, 0xd1, 0x55, 0xdf, 0xa5, 0xcf, 0x59, 0x64, 0xc9, 0x4d, - 0x0d, 0xdd, 0xf9, 0x3b, 0xf2, 0x3d, 0x47, 0xbe, 0xc7, 0xb0, 0x2a,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 0xb1, 0x3b, 0x2b, 0xde, 0x34, 0xd8, 0x9d, 0xf2, 0x8f, 0x77, 0x92, 0x2b, 0x08, 0x27, - 0x47, 0xe4, 0x07, 0x78, 0x8f, 0xe2, 0x64, 0x37, 0xaf, 0x1f, 0xbf, 0x0e, 0xba, 0x74, 0x2f, 0x9c, - 0xed, 0x0b, 0x04, 0xd7, 0xba, 0x5a, 0xf2, 0x17, 0xe6, 0xa6, 0x43, 0x12, 0xb1, 0x49, 0xbb, 0xc9, - 0x9a, 0x4d, 0xcb, 0xa5, 0x33, 0x92, 0x42, 0x30, 0xb4, 0x46, 0x42, 0xf6, 0xb9, 0xce, 0x24, 0x62, - 0x93, 0x32, 0xe9, 0x8c, 0xfc, 0x01, 0x3f, 0x93, 0x95, 0x22, 0x2b, 0x76, 0xde, 0x70, 0xe2, 0x6b, - 0xa0, 0xb3, 0x7f, 0xce, 0x6e, 0x3e, 0xfc, 0x51, 0xff, 0xdf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xbe, - 0xa2, 0x1f, 0x47, 0x60, 0x02, 0x00, 0x00, + // 360 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0x5f, 0x6b, 0xdb, 0x30, + 0x14, 0xc5, 0xe3, 0x7f, 0x59, 0x72, 0x33, 0x3b, 0x43, 0x8c, 0x61, 0xcc, 0x06, 0x41, 0x4f, 0x86, + 0x81, 0x18, 0xd9, 0xcb, 0xd8, 0x5e, 0x57, 0xe8, 0x83, 0x0b, 0x45, 0xfd, 0x04, 0x8a, 0x2d, 0x9c, + 0xd0, 0xd8, 0x72, 0xad, 0xeb, 0x42, 0x1e, 0xfa, 0xd4, 0xef, 0xd2, 0xcf, 0x59, 0x64, 0x29, 0x4d, + 0x0d, 0x7d, 0xf3, 0xef, 0xc8, 0xf7, 0x1c, 0xf9, 0x1e, 0xc3, 0xaa, 0x92, 0xbb, 0xa1, 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15, 0xaa, 0x66, 0x37, 0x4e, 0xbc, 0x6a, 0xb1, 0x3f, 0xf1, 0xb7, 0x77, 0xb2, 0x7f, 0x10, + 0x4f, 0x8e, 0xc8, 0x17, 0x08, 0xee, 0xe5, 0xc9, 0x6d, 0xde, 0x3c, 0x7e, 0x1c, 0xf4, 0xd7, 0xff, + 0xe3, 0x6d, 0x9f, 0x20, 0xfa, 0x6f, 0xaa, 0x25, 0x3f, 0x61, 0x6e, 0x3b, 0x24, 0x09, 0x9b, 0xb4, + 0x9b, 0xad, 0xd9, 0xb4, 0x5c, 0x3a, 0x23, 0x39, 0x44, 0x63, 0x6b, 0x24, 0x66, 0xef, 0xeb, 0xcc, + 0x12, 0x36, 0x29, 0x93, 0xce, 0xc8, 0x0f, 0x08, 0x0b, 0x55, 0x6b, 0xb2, 0x62, 0x97, 0x0d, 0x67, + 0xa1, 0x01, 0x3a, 0xfb, 0xe5, 0xed, 0xe6, 0xe3, 0x1f, 0xf5, 0xfb, 0x35, 0x00, 0x00, 0xff, 0xff, + 0x95, 0x24, 0xad, 0xbc, 0x60, 0x02, 0x00, 0x00, } diff --git a/debug/proto/debug.proto b/debug/proto/debug.proto index 25030153..830423b3 100644 --- a/debug/proto/debug.proto +++ b/debug/proto/debug.proto @@ -31,11 +31,11 @@ message StatsResponse { // LogRequest queries service for logs message LogRequest { // count is the count of events - uint64 count = 1; + int64 count = 1; // relative time in seconds // before the current time // from which to show logs - uint64 since = 2; + int64 since = 2; // stream logs continuously bool stream = 3; } From 60e0e81523ed564be9206c117ddd4bd36d4f3323 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Wed, 27 Nov 2019 20:32:54 +0000 Subject: [PATCH 07/15] Added debug/service to grab the logs from a service --- debug/debug.go | 7 ++++ debug/service/service.go | 83 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 debug/debug.go create mode 100644 debug/service/service.go diff --git a/debug/debug.go b/debug/debug.go new file mode 100644 index 00000000..a34d7f28 --- /dev/null +++ b/debug/debug.go @@ -0,0 +1,7 @@ +// Package debug provides micro debug packages +package debug + +var ( + // DefaultName is the name of debug service + DefaultName = "go.micro.debug" +) diff --git a/debug/service/service.go b/debug/service/service.go new file mode 100644 index 00000000..5089058e --- /dev/null +++ b/debug/service/service.go @@ -0,0 +1,83 @@ +package service + +import ( + "context" + "fmt" + "time" + + "github.com/micro/go-micro/client" + + "github.com/micro/go-micro/debug/log" + pb "github.com/micro/go-micro/debug/proto" +) + +// Debug provides debug service client +type Debug struct { + dbg pb.DebugService +} + +// NewDebug provides Debug service implementation +func NewDebug(name string) *Debug { + // create default client + cli := client.DefaultClient + + return &Debug{ + dbg: pb.NewDebugService(name, cli), + } +} + +func (d *Debug) Logs(opts ...log.ReadOption) (<-chan log.Record, error) { + options := log.ReadOptions{} + // initialize the read options + for _, o := range opts { + o(&options) + } + + req := &pb.LogRequest{} + if !options.Since.IsZero() { + req.Since = options.Since.UnixNano() + } + + if options.Count > 0 { + req.Count = int64(options.Count) + } + + // get the log stream + stream, err := d.dbg.Logs(context.Background(), req) + if err != nil { + return nil, fmt.Errorf("failed getting log stream: %s", err) + } + + // log channel for streaming logs + logChan := make(chan log.Record) + // go stream logs + go d.streamLogs(logChan, stream) + + return logChan, nil +} + +func (d *Debug) streamLogs(logChan chan log.Record, stream pb.Debug_LogsService) { + defer stream.Close() + + for { + resp, err := stream.Recv() + if err != nil { + break + } + + metadata := make(map[string]string) + for k, v := range resp.Metadata { + metadata[k] = v + } + + record := log.Record{ + Timestamp: time.Unix(0, resp.Timestamp), + Value: resp.Value, + Metadata: metadata, + } + + logChan <- record + } + + close(logChan) +} From 265e8ade05ab184e9d5fb248bad2ecfd0b854a9b Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Thu, 28 Nov 2019 11:05:35 +0000 Subject: [PATCH 08/15] Embed DebugHandler proto --- debug/handler/debug.go | 2 +- debug/log/default.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/debug/handler/debug.go b/debug/handler/debug.go index e532de79..7dd11b69 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -17,6 +17,7 @@ var ( type Debug struct { started int64 + proto.DebugHandler log.Log } @@ -55,7 +56,6 @@ func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, stream proto.De defer stream.Close() // TODO: figure out the stream later on - // stream the logs for _, record := range records { metadata := make(map[string]string) for k, v := range record.Metadata { diff --git a/debug/log/default.go b/debug/log/default.go index b3840909..9e546cb4 100644 --- a/debug/log/default.go +++ b/debug/log/default.go @@ -32,13 +32,13 @@ func NewLog(opts ...Option) Log { } } -// Write writes log into logger +// Write writes logs into logger func (l *defaultLog) Write(v ...interface{}) { l.Buffer.Put(fmt.Sprint(v...)) golog.Print(v...) } -// Read reads logs from the logger +// Read reads logs and returns them func (l *defaultLog) Read(opts ...ReadOption) []Record { options := ReadOptions{} // initialize the read options @@ -55,8 +55,9 @@ func (l *defaultLog) Read(opts ...ReadOption) []Record { entries = l.Buffer.Get(options.Count) } - // TODO: if both Since and Count are set should we return + // TODO: if both Since and Count are set should we return? // last Count from the returned time scoped entries? + records := make([]Record, 0, len(entries)) for _, entry := range entries { record := Record{ From 9fab47ecdbe3b14a31f73f2bd0e85cf00be93f75 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Thu, 28 Nov 2019 11:36:38 +0000 Subject: [PATCH 09/15] Added DefaultLog variables --- debug/handler/debug.go | 7 ++++--- debug/log/log.go | 34 +++++++++++++++++----------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/debug/handler/debug.go b/debug/handler/debug.go index 7dd11b69..02f853f7 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -18,12 +18,13 @@ var ( type Debug struct { started int64 proto.DebugHandler - log.Log + log log.Log } func newDebug() *Debug { return &Debug{ started: time.Now().Unix(), + log: log.DefaultLog, } } @@ -48,9 +49,9 @@ func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, stream proto.De var records []log.Record since := time.Unix(0, req.Since) if !since.IsZero() { - records = d.Log.Read(log.Since(since)) + records = d.log.Read(log.Since(since)) } else { - records = d.Log.Read(log.Count(int(req.Count))) + records = d.log.Read(log.Count(int(req.Count))) } defer stream.Close() diff --git a/debug/log/log.go b/debug/log/log.go index 1c9ae240..f56fa5de 100644 --- a/debug/log/log.go +++ b/debug/log/log.go @@ -8,10 +8,10 @@ import ( ) var ( - // logger is default Logger - logger Log = NewLog() - // default log level is info - level = LevelInfo + // DefaultLog logger + DefaultLog = NewLog() + // DefaultLevel is default log level + DefaultLevel = LevelInfo // prefix for all messages prefix string ) @@ -49,38 +49,38 @@ const ( func init() { switch os.Getenv("MICRO_LOG_LEVEL") { case "trace": - level = LevelTrace + DefaultLevel = LevelTrace case "debug": - level = LevelDebug + DefaultLevel = LevelDebug case "warn": - level = LevelWarn + DefaultLevel = LevelWarn case "info": - level = LevelInfo + DefaultLevel = LevelInfo case "error": - level = LevelError + DefaultLevel = LevelError case "fatal": - level = LevelFatal + DefaultLevel = LevelFatal } } func log(v ...interface{}) { if len(prefix) > 0 { - logger.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...)) + DefaultLog.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...)) return } - logger.Write(fmt.Sprint(v...)) + DefaultLog.Write(fmt.Sprint(v...)) } func logf(format string, v ...interface{}) { if len(prefix) > 0 { format = prefix + " " + format } - logger.Write(fmt.Sprintf(format, v...)) + DefaultLog.Write(fmt.Sprintf(format, v...)) } // WithLevel logs with the level specified func WithLevel(l Level, v ...interface{}) { - if l > level { + if l > DefaultLevel { return } log(v...) @@ -88,7 +88,7 @@ func WithLevel(l Level, v ...interface{}) { // WithLevel logs with the level specified func WithLevelf(l Level, format string, v ...interface{}) { - if l > level { + if l > DefaultLevel { return } logf(format, v...) @@ -158,12 +158,12 @@ func Fatalf(format string, v ...interface{}) { // SetLevel sets the log level func SetLevel(l Level) { - level = l + DefaultLevel = l } // GetLevel returns the current level func GetLevel() Level { - return level + return DefaultLevel } // Set a prefix for the logger From 13d2a9ec7a4b620606333079d4aba06578c91f6e Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Thu, 28 Nov 2019 11:55:49 +0000 Subject: [PATCH 10/15] Register proto handler properly --- service.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/service.go b/service.go index 3058a3b2..e0c53a98 100644 --- a/service.go +++ b/service.go @@ -16,6 +16,8 @@ import ( "github.com/micro/go-micro/server" "github.com/micro/go-micro/util/log" "github.com/micro/go-micro/util/wrapper" + + pb "github.com/micro/go-micro/debug/proto" ) type service struct { @@ -141,12 +143,9 @@ func (s *service) Stop() error { func (s *service) Run() error { // register the debug handler - s.opts.Server.Handle( - s.opts.Server.NewHandler( - handler.DefaultHandler, - server.InternalHandler(true), - ), - ) + pb.RegisterDebugHandler(s.opts.Server, + handler.DefaultHandler, + server.InternalHandler(true)) // start the profiler // TODO: set as an option to the service, don't just use pprof From 612f872f76d2a5cf1785ce8a34174fd89f2f9b0a Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Thu, 28 Nov 2019 15:50:03 +0000 Subject: [PATCH 11/15] Server should not close the stream when done --- debug/handler/debug.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/debug/handler/debug.go b/debug/handler/debug.go index 02f853f7..144be48d 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -54,8 +54,6 @@ func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, stream proto.De records = d.log.Read(log.Count(int(req.Count))) } - defer stream.Close() - // TODO: figure out the stream later on for _, record := range records { metadata := make(map[string]string) From 7f1dea72f27a3c06be3240dc0cd13030f59e4f93 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Thu, 28 Nov 2019 18:08:48 +0000 Subject: [PATCH 12/15] Simplified Logs RPC. Cleaned up code. Added comments. --- debug/buffer/buffer.go | 8 +++++--- debug/handler/debug.go | 19 ++++++++++++++----- debug/log/default.go | 22 +++++++++++++++++----- debug/log/default_test.go | 10 +++++----- debug/log/options.go | 9 +++++++++ debug/service/service.go | 1 + 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/debug/buffer/buffer.go b/debug/buffer/buffer.go index 50c511d4..881e832a 100644 --- a/debug/buffer/buffer.go +++ b/debug/buffer/buffer.go @@ -6,12 +6,14 @@ import ( "time" ) +// Buffer is ring buffer type Buffer struct { size int sync.RWMutex vals []*Entry } +// Entry is ring buffer data entry type Entry struct { Value interface{} Timestamp time.Time @@ -43,14 +45,14 @@ func (b *Buffer) Put(v interface{}) { // Get returns the last n entries func (b *Buffer) Get(n int) []*Entry { + b.RLock() + defer b.RUnlock() + // reset any invalid values if n > b.size || n < 0 { n = b.size } - b.RLock() - defer b.RUnlock() - // create a delta delta := b.size - n diff --git a/debug/handler/debug.go b/debug/handler/debug.go index 144be48d..d5a9ea6c 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -1,3 +1,4 @@ +// Pacjage handler implements service debug handler package handler import ( @@ -46,15 +47,23 @@ func (d *Debug) Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.S } func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, stream proto.Debug_LogsStream) error { - var records []log.Record + var options []log.ReadOption + since := time.Unix(0, req.Since) if !since.IsZero() { - records = d.log.Read(log.Since(since)) - } else { - records = d.log.Read(log.Count(int(req.Count))) + options = append(options, log.Since(since)) } - // TODO: figure out the stream later on + count := int(req.Count) + if count > 0 { + options = append(options, log.Count(count)) + } + + // get the log records + records := d.log.Read(options...) + + // TODO: figure out the stream + for _, record := range records { metadata := make(map[string]string) for k, v := range record.Metadata { diff --git a/debug/log/default.go b/debug/log/default.go index 9e546cb4..35b11fe4 100644 --- a/debug/log/default.go +++ b/debug/log/default.go @@ -50,13 +50,24 @@ func (l *defaultLog) Read(opts ...ReadOption) []Record { // if Since options ha sbeen specified we honor it if !options.Since.IsZero() { entries = l.Buffer.Since(options.Since) - } else { - // otherwie return last count entries - entries = l.Buffer.Get(options.Count) } - // TODO: if both Since and Count are set should we return? - // last Count from the returned time scoped entries? + // only if we specified valid count constraint + // do we do some serious if-else kung-fu + // if since has been given we return *count* number of + // logs since the requested timestamp; + // otherwise we retourn last count number of logs + if options.Count > 0 { + switch len(entries) > 0 { + case true: + // if we request fewer logs than what since constraint gives us + if options.Count < len(entries) { + entries = entries[0:options.Count] + } + default: + entries = l.Buffer.Get(options.Count) + } + } records := make([]Record, 0, len(entries)) for _, entry := range entries { @@ -66,5 +77,6 @@ func (l *defaultLog) Read(opts ...ReadOption) []Record { } records = append(records, record) } + return records } diff --git a/debug/log/default_test.go b/debug/log/default_test.go index 0ef6e903..e0f08a7c 100644 --- a/debug/log/default_test.go +++ b/debug/log/default_test.go @@ -9,21 +9,21 @@ func TestLogger(t *testing.T) { // set size to some value size := 100 // override the global logger - logger = NewLog(Size(size)) + DefaultLog = NewLog(Size(size)) // make sure we have the right size of the logger ring buffer - if logger.(*defaultLog).Size() != size { - t.Errorf("expected buffer size: %d, got: %d", size, logger.(*defaultLog).Size()) + if DefaultLog.(*defaultLog).Size() != size { + t.Errorf("expected buffer size: %d, got: %d", size, DefaultLog.(*defaultLog).Size()) } // Log some cruft Info("foobar") // increase the log level - level = LevelDebug + DefaultLevel = LevelDebug Debugf("foo %s", "bar") // Check if the logs are stored in the logger ring buffer expected := []string{"foobar", "foo bar"} - entries := logger.Read(Count(len(expected))) + entries := DefaultLog.Read(Count(len(expected))) for i, entry := range entries { if !reflect.DeepEqual(entry.Value, expected[i]) { t.Errorf("expected %s, got %s", expected[i], entry.Value) diff --git a/debug/log/options.go b/debug/log/options.go index 2320b71e..03dece38 100644 --- a/debug/log/options.go +++ b/debug/log/options.go @@ -31,6 +31,8 @@ type ReadOptions struct { Since time.Time // Count specifies number of logs to return Count int + // Stream requests continuous log stream + Stream bool } // ReadOption used for reading the logs @@ -49,3 +51,10 @@ func Count(c int) ReadOption { o.Count = c } } + +// Stream requests continuous log stream +func Stream(s bool) ReadOption { + return func(o *ReadOptions) { + o.Stream = s + } +} diff --git a/debug/service/service.go b/debug/service/service.go index 5089058e..aa3fd2e7 100644 --- a/debug/service/service.go +++ b/debug/service/service.go @@ -26,6 +26,7 @@ func NewDebug(name string) *Debug { } } +// Logs queries the service logs and returns a channel to read the logs from func (d *Debug) Logs(opts ...log.ReadOption) (<-chan log.Record, error) { options := log.ReadOptions{} // initialize the read options From ecdadef633b995d4c01d663e46830feb2cba5d4d Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Sat, 30 Nov 2019 12:39:29 +0000 Subject: [PATCH 13/15] Added hack support for logs streaming cruft --- debug/buffer/buffer.go | 45 +++++++++++++++++++++++++++++--- debug/handler/debug.go | 55 +++++++++++++++++++++++++++++----------- debug/log/default.go | 32 +++++++++++++++++++---- debug/log/log.go | 2 ++ debug/service/service.go | 2 ++ 5 files changed, 112 insertions(+), 24 deletions(-) diff --git a/debug/buffer/buffer.go b/debug/buffer/buffer.go index 881e832a..007293aa 100644 --- a/debug/buffer/buffer.go +++ b/debug/buffer/buffer.go @@ -4,13 +4,22 @@ package buffer import ( "sync" "time" + + "github.com/google/uuid" ) +type stream struct { + id string + entries chan *Entry + stop chan bool +} + // Buffer is ring buffer type Buffer struct { size int sync.RWMutex - vals []*Entry + vals []*Entry + streams map[string]stream } // Entry is ring buffer data entry @@ -22,7 +31,8 @@ type Entry struct { // New returns a new buffer of the given size func New(i int) *Buffer { return &Buffer{ - size: i, + size: i, + streams: make(map[string]stream), } } @@ -32,15 +42,26 @@ func (b *Buffer) Put(v interface{}) { defer b.Unlock() // append to values - b.vals = append(b.vals, &Entry{ + entry := &Entry{ Value: v, Timestamp: time.Now(), - }) + } + b.vals = append(b.vals, entry) // trim if bigger than size required if len(b.vals) > b.size { b.vals = b.vals[1:] } + + // TODO: this is fucking ugly + for _, stream := range b.streams { + select { + case <-stream.stop: + delete(b.streams, stream.id) + close(stream.entries) + case stream.entries <- entry: + } + } } // Get returns the last n entries @@ -93,6 +114,22 @@ func (b *Buffer) Since(t time.Time) []*Entry { return nil } +// Stream logs from the buffer +func (b *Buffer) Stream(stop chan bool) <-chan *Entry { + b.Lock() + defer b.Unlock() + + entries := make(chan *Entry, 128) + id := uuid.New().String() + b.streams[id] = stream{ + id: id, + entries: entries, + stop: stop, + } + + return entries +} + // Size returns the size of the ring buffer func (b *Buffer) Size() int { return b.size diff --git a/debug/handler/debug.go b/debug/handler/debug.go index d5a9ea6c..f21dd644 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -59,27 +59,52 @@ func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, stream proto.De options = append(options, log.Count(count)) } + if req.Stream { + stop := make(chan bool) + defer close(stop) + + // TODO: figure out how to close log stream + // It seems when the client disconnects, + // the connection stays open until some timeout expires + // or something like that; that means the map of streams + // might end up bloating if not cleaned up properly + records := d.log.Stream(stop) + for record := range records { + if err := d.sendRecord(record, stream); err != nil { + return err + } + } + // done streaming, return + return nil + } + // get the log records records := d.log.Read(options...) - - // TODO: figure out the stream - + // send all the logs downstream for _, record := range records { - metadata := make(map[string]string) - for k, v := range record.Metadata { - metadata[k] = v - } - - recLog := &proto.Log{ - Timestamp: record.Timestamp.UnixNano(), - Value: record.Value.(string), - Metadata: metadata, - } - - if err := stream.Send(recLog); err != nil { + if err := d.sendRecord(record, stream); err != nil { return err } } return nil } + +func (d *Debug) sendRecord(record log.Record, stream proto.Debug_LogsStream) error { + metadata := make(map[string]string) + for k, v := range record.Metadata { + metadata[k] = v + } + + recLog := &proto.Log{ + Timestamp: record.Timestamp.UnixNano(), + Value: record.Value.(string), + Metadata: metadata, + } + + if err := stream.Send(recLog); err != nil { + return err + } + + return nil +} diff --git a/debug/log/default.go b/debug/log/default.go index 35b11fe4..d715e0c6 100644 --- a/debug/log/default.go +++ b/debug/log/default.go @@ -34,8 +34,8 @@ func NewLog(opts ...Option) Log { // Write writes logs into logger func (l *defaultLog) Write(v ...interface{}) { - l.Buffer.Put(fmt.Sprint(v...)) golog.Print(v...) + l.Buffer.Put(fmt.Sprint(v...)) } // Read reads logs and returns them @@ -53,10 +53,10 @@ func (l *defaultLog) Read(opts ...ReadOption) []Record { } // only if we specified valid count constraint - // do we do some serious if-else kung-fu - // if since has been given we return *count* number of - // logs since the requested timestamp; - // otherwise we retourn last count number of logs + // do we end up doing some serious if-else kung-fu + // if since constraint has been provided + // we return *count* number of logs since the given timestamp; + // otherwise we return last count number of logs if options.Count > 0 { switch len(entries) > 0 { case true: @@ -80,3 +80,25 @@ func (l *defaultLog) Read(opts ...ReadOption) []Record { return records } + +// Stream returns channel for reading log records +func (l *defaultLog) Stream(stop chan bool) <-chan Record { + // get stream channel from ring buffer + stream := l.Buffer.Stream(stop) + records := make(chan Record) + + fmt.Println("requested log stream") + + // stream the log records + go func() { + for entry := range stream { + records <- Record{ + Timestamp: entry.Timestamp, + Value: entry.Value, + Metadata: make(map[string]string), + } + } + }() + + return records +} diff --git a/debug/log/log.go b/debug/log/log.go index f56fa5de..c70ce00c 100644 --- a/debug/log/log.go +++ b/debug/log/log.go @@ -22,6 +22,8 @@ type Log interface { Read(...ReadOption) []Record // Write writes logs to logger Write(...interface{}) + // Stream logs + Stream(chan bool) <-chan Record } // Record is log record entry diff --git a/debug/service/service.go b/debug/service/service.go index aa3fd2e7..9fda358c 100644 --- a/debug/service/service.go +++ b/debug/service/service.go @@ -43,6 +43,8 @@ func (d *Debug) Logs(opts ...log.ReadOption) (<-chan log.Record, error) { req.Count = int64(options.Count) } + req.Stream = options.Stream + // get the log stream stream, err := d.dbg.Logs(context.Background(), req) if err != nil { From 4613a820ca6341ce151b15fa394eebf230c10318 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Sun, 1 Dec 2019 13:15:10 +0000 Subject: [PATCH 14/15] Small refactoring og logs * log.Write now accepts log.Record * we stream last 10 records first * regenerate proto because of the above --- debug/handler/debug.go | 14 ++--- debug/log/default.go | 22 +++++-- debug/log/log.go | 12 ++-- debug/proto/debug.pb.go | 104 +++++++++++++++++----------------- debug/proto/debug.pb.micro.go | 10 ++-- debug/proto/debug.proto | 22 +++---- debug/service/service.go | 2 +- 7 files changed, 98 insertions(+), 88 deletions(-) diff --git a/debug/handler/debug.go b/debug/handler/debug.go index f21dd644..0e077c7f 100644 --- a/debug/handler/debug.go +++ b/debug/handler/debug.go @@ -49,7 +49,7 @@ func (d *Debug) Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.S func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, stream proto.Debug_LogsStream) error { var options []log.ReadOption - since := time.Unix(0, req.Since) + since := time.Unix(req.Since, 0) if !since.IsZero() { options = append(options, log.Since(since)) } @@ -63,11 +63,11 @@ func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, stream proto.De stop := make(chan bool) defer close(stop) - // TODO: figure out how to close log stream - // It seems when the client disconnects, + // TODO: we need to figure out how to close ithe log stream + // It seems like when a client disconnects, // the connection stays open until some timeout expires // or something like that; that means the map of streams - // might end up bloating if not cleaned up properly + // might end up leaking memory if not cleaned up properly records := d.log.Stream(stop) for record := range records { if err := d.sendRecord(record, stream); err != nil { @@ -96,13 +96,13 @@ func (d *Debug) sendRecord(record log.Record, stream proto.Debug_LogsStream) err metadata[k] = v } - recLog := &proto.Log{ - Timestamp: record.Timestamp.UnixNano(), + pbRecord := &proto.Record{ + Timestamp: record.Timestamp.Unix(), Value: record.Value.(string), Metadata: metadata, } - if err := stream.Send(recLog); err != nil { + if err := stream.Send(pbRecord); err != nil { return err } diff --git a/debug/log/default.go b/debug/log/default.go index d715e0c6..6f7bddab 100644 --- a/debug/log/default.go +++ b/debug/log/default.go @@ -33,9 +33,9 @@ func NewLog(opts ...Option) Log { } // Write writes logs into logger -func (l *defaultLog) Write(v ...interface{}) { - golog.Print(v...) - l.Buffer.Put(fmt.Sprint(v...)) +func (l *defaultLog) Write(r Record) { + golog.Print(r.Value) + l.Buffer.Put(fmt.Sprint(r.Value)) } // Read reads logs and returns them @@ -85,12 +85,22 @@ func (l *defaultLog) Read(opts ...ReadOption) []Record { func (l *defaultLog) Stream(stop chan bool) <-chan Record { // get stream channel from ring buffer stream := l.Buffer.Stream(stop) - records := make(chan Record) - - fmt.Println("requested log stream") + // make a buffered channel + records := make(chan Record, 128) + // get last 10 records + last10 := l.Buffer.Get(10) // stream the log records go func() { + // first send last 10 records + for _, entry := range last10 { + records <- Record{ + Timestamp: entry.Timestamp, + Value: entry.Value, + Metadata: make(map[string]string), + } + } + // now stream continuously for entry := range stream { records <- Record{ Timestamp: entry.Timestamp, diff --git a/debug/log/log.go b/debug/log/log.go index c70ce00c..42a8f558 100644 --- a/debug/log/log.go +++ b/debug/log/log.go @@ -20,9 +20,9 @@ var ( type Log interface { // Read reads log entries from the logger Read(...ReadOption) []Record - // Write writes logs to logger - Write(...interface{}) - // Stream logs + // Write writes records to log + Write(Record) + // Stream log records Stream(chan bool) <-chan Record } @@ -67,17 +67,17 @@ func init() { func log(v ...interface{}) { if len(prefix) > 0 { - DefaultLog.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...)) + DefaultLog.Write(Record{Value: fmt.Sprint(append([]interface{}{prefix, " "}, v...)...)}) return } - DefaultLog.Write(fmt.Sprint(v...)) + DefaultLog.Write(Record{Value: fmt.Sprint(v...)}) } func logf(format string, v ...interface{}) { if len(prefix) > 0 { format = prefix + " " + format } - DefaultLog.Write(fmt.Sprintf(format, v...)) + DefaultLog.Write(Record{Value: fmt.Sprintf(format, v...)}) } // WithLevel logs with the level specified diff --git a/debug/proto/debug.pb.go b/debug/proto/debug.pb.go index 423e98cc..a94a55ce 100644 --- a/debug/proto/debug.pb.go +++ b/debug/proto/debug.pb.go @@ -198,15 +198,15 @@ func (m *StatsResponse) GetGc() uint64 { return 0 } -// LogRequest queries service for logs +// LogRequest requests service logs type LogRequest struct { - // count is the count of events + // count of records to request Count int64 `protobuf:"varint,1,opt,name=count,proto3" json:"count,omitempty"` // relative time in seconds // before the current time // from which to show logs Since int64 `protobuf:"varint,2,opt,name=since,proto3" json:"since,omitempty"` - // stream logs continuously + // stream records continuously Stream bool `protobuf:"varint,3,opt,name=stream,proto3" json:"stream,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -259,59 +259,59 @@ func (m *LogRequest) GetStream() bool { return false } -// Log is service log record -type Log struct { - // timestamp of log event +// Record is service log record +type Record struct { + // timestamp of log record Timestamp int64 `protobuf:"varint,1,opt,name=timestamp,proto3" json:"timestamp,omitempty"` - // log value + // record value Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` - // metadata + // record metadata Metadata map[string]string `protobuf:"bytes,3,rep,name=metadata,proto3" json:"metadata,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` } -func (m *Log) Reset() { *m = Log{} } -func (m *Log) String() string { return proto.CompactTextString(m) } -func (*Log) ProtoMessage() {} -func (*Log) Descriptor() ([]byte, []int) { +func (m *Record) Reset() { *m = Record{} } +func (m *Record) String() string { return proto.CompactTextString(m) } +func (*Record) ProtoMessage() {} +func (*Record) Descriptor() ([]byte, []int) { return fileDescriptor_8d9d361be58531fb, []int{5} } -func (m *Log) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_Log.Unmarshal(m, b) +func (m *Record) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Record.Unmarshal(m, b) } -func (m *Log) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_Log.Marshal(b, m, deterministic) +func (m *Record) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Record.Marshal(b, m, deterministic) } -func (m *Log) XXX_Merge(src proto.Message) { - xxx_messageInfo_Log.Merge(m, src) +func (m *Record) XXX_Merge(src proto.Message) { + xxx_messageInfo_Record.Merge(m, src) } -func (m *Log) XXX_Size() int { - return xxx_messageInfo_Log.Size(m) +func (m *Record) XXX_Size() int { + return xxx_messageInfo_Record.Size(m) } -func (m *Log) XXX_DiscardUnknown() { - xxx_messageInfo_Log.DiscardUnknown(m) +func (m *Record) XXX_DiscardUnknown() { + xxx_messageInfo_Record.DiscardUnknown(m) } -var xxx_messageInfo_Log proto.InternalMessageInfo +var xxx_messageInfo_Record proto.InternalMessageInfo -func (m *Log) GetTimestamp() int64 { +func (m *Record) GetTimestamp() int64 { if m != nil { return m.Timestamp } return 0 } -func (m *Log) GetValue() string { +func (m *Record) GetValue() string { if m != nil { return m.Value } return "" } -func (m *Log) GetMetadata() map[string]string { +func (m *Record) GetMetadata() map[string]string { if m != nil { return m.Metadata } @@ -324,35 +324,35 @@ func init() { proto.RegisterType((*StatsRequest)(nil), "StatsRequest") proto.RegisterType((*StatsResponse)(nil), "StatsResponse") proto.RegisterType((*LogRequest)(nil), "LogRequest") - proto.RegisterType((*Log)(nil), "Log") - proto.RegisterMapType((map[string]string)(nil), "Log.MetadataEntry") + proto.RegisterType((*Record)(nil), "Record") + proto.RegisterMapType((map[string]string)(nil), "Record.MetadataEntry") } func init() { proto.RegisterFile("debug.proto", fileDescriptor_8d9d361be58531fb) } var fileDescriptor_8d9d361be58531fb = []byte{ - // 360 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0x5f, 0x6b, 0xdb, 0x30, - 0x14, 0xc5, 0xe3, 0x7f, 0x59, 0x72, 0x33, 0x3b, 0x43, 0x8c, 0x61, 0xcc, 0x06, 0x41, 0x4f, 0x86, - 0x81, 0x18, 0xd9, 0xcb, 0xd8, 0x5e, 0x57, 0xe8, 0x83, 0x0b, 0x45, 0xfd, 0x04, 0x8a, 0x2d, 0x9c, - 0xd0, 0xd8, 0x72, 0xad, 0xeb, 0x42, 0x1e, 0xfa, 0xd4, 0xef, 0xd2, 0xcf, 0x59, 0x64, 0x29,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xdc, 0x02, 0x61, 0xb0, 0x68, 0x24, 0x8a, 0x4a, 0xa0, 0x48, 0x83, 0x4d, 0x90, 0xaf, 0xb6, - 0x84, 0x15, 0xaa, 0x66, 0x37, 0x4e, 0xbc, 0x6a, 0xb1, 0x3f, 0xf1, 0xb7, 0x77, 0xb2, 0x7f, 0x10, - 0x4f, 0x8e, 0xc8, 0x17, 0x08, 0xee, 0xe5, 0xc9, 0x6d, 0xde, 0x3c, 0x7e, 0x1c, 0xf4, 0xd7, 0xff, - 0xe3, 0x6d, 0x9f, 0x20, 0xfa, 0x6f, 0xaa, 0x25, 0x3f, 0x61, 0x6e, 0x3b, 0x24, 0x09, 0x9b, 0xb4, - 0x9b, 0xad, 0xd9, 0xb4, 0x5c, 0x3a, 0x23, 0x39, 0x44, 0x63, 0x6b, 0x24, 0x66, 0xef, 0xeb, 0xcc, - 0x12, 0x36, 0x29, 0x93, 0xce, 0xc8, 0x0f, 0x08, 0x0b, 0x55, 0x6b, 0xb2, 0x62, 0x97, 0x0d, 0x67, - 0xa1, 0x01, 0x3a, 0xfb, 0xe5, 0xed, 0xe6, 0xe3, 0x1f, 0xf5, 0xfb, 0x35, 0x00, 0x00, 0xff, 0xff, - 0x95, 0x24, 0xad, 0xbc, 0x60, 0x02, 0x00, 0x00, + // 364 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0x41, 0x6b, 0xdb, 0x30, + 0x1c, 0xc5, 0x63, 0x3b, 0x76, 0x92, 0x7f, 0x66, 0x67, 0x88, 0x6d, 0x18, 0xb3, 0x43, 0xd0, 0xc9, + 0x30, 0x10, 0x5b, 0x76, 0x19, 0xdb, 0x75, 0x85, 0x1e, 0x52, 0x28, 0xea, 0x27, 0x50, 0x6c, 0xe1, + 0x84, 0xc6, 0x96, 0x6b, 0xfd, 0x5d, 0xc8, 0xad, 0xd0, 0xaf, 0xd3, 0x0f, 0x59, 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05, 0xb7, 0x40, 0x7e, 0xc1, 0xbc, 0x96, 0x28, 0x4a, 0x81, 0x22, 0x0d, 0xd6, 0x41, + 0xbe, 0xdc, 0x7c, 0x65, 0xd6, 0x8e, 0xdd, 0x38, 0xfd, 0xaa, 0xc1, 0xee, 0xc4, 0xdf, 0x7e, 0xcb, + 0xfe, 0x41, 0x3c, 0x5a, 0x22, 0x9f, 0x21, 0xb8, 0x97, 0x27, 0x77, 0xff, 0xe6, 0xf3, 0xe3, 0xac, + 0xbf, 0xfe, 0x1f, 0x6f, 0xf3, 0xe4, 0x41, 0xf8, 0xdf, 0x34, 0x4c, 0x7e, 0x40, 0x64, 0xab, 0x24, + 0x09, 0x1b, 0x95, 0x9c, 0xad, 0xd8, 0xb8, 0x63, 0x3a, 0x21, 0x39, 0x84, 0x43, 0x79, 0x24, 0x66, + 0xef, 0x5b, 0xcd, 0x12, 0x36, 0xea, 0x94, 0x4e, 0xc8, 0x1a, 0xa6, 0x5b, 0x55, 0x69, 0xb2, 0x64, + 0x97, 0x8b, 0xce, 0x66, 0xee, 0x4c, 0x74, 0xf2, 0xd3, 0xdb, 0x45, 0xc3, 0xdb, 0xfa, 0xfd, 0x1a, + 0x00, 0x00, 0xff, 0xff, 0xea, 0x2d, 0x15, 0xdb, 0x6a, 0x02, 0x00, 0x00, } diff --git a/debug/proto/debug.pb.micro.go b/debug/proto/debug.pb.micro.go index 6f89d5eb..3381c2af 100644 --- a/debug/proto/debug.pb.micro.go +++ b/debug/proto/debug.pb.micro.go @@ -93,7 +93,7 @@ type Debug_LogsService interface { SendMsg(interface{}) error RecvMsg(interface{}) error Close() error - Recv() (*Log, error) + Recv() (*Record, error) } type debugServiceLogs struct { @@ -112,8 +112,8 @@ func (x *debugServiceLogs) RecvMsg(m interface{}) error { return x.stream.Recv(m) } -func (x *debugServiceLogs) Recv() (*Log, error) { - m := new(Log) +func (x *debugServiceLogs) Recv() (*Record, error) { + m := new(Record) err := x.stream.Recv(m) if err != nil { return nil, err @@ -166,7 +166,7 @@ type Debug_LogsStream interface { SendMsg(interface{}) error RecvMsg(interface{}) error Close() error - Send(*Log) error + Send(*Record) error } type debugLogsStream struct { @@ -185,6 +185,6 @@ func (x *debugLogsStream) RecvMsg(m interface{}) error { return x.stream.Recv(m) } -func (x *debugLogsStream) Send(m *Log) error { +func (x *debugLogsStream) Send(m *Record) error { return x.stream.Send(m) } diff --git a/debug/proto/debug.proto b/debug/proto/debug.proto index 830423b3..a666068a 100644 --- a/debug/proto/debug.proto +++ b/debug/proto/debug.proto @@ -1,9 +1,9 @@ syntax = "proto3"; service Debug { - rpc Health(HealthRequest) returns (HealthResponse) {}; - rpc Stats(StatsRequest) returns (StatsResponse) {}; - rpc Logs(LogRequest) returns (stream Log) {}; + rpc Health(HealthRequest) returns (HealthResponse) {}; + rpc Stats(StatsRequest) returns (StatsResponse) {}; + rpc Logs(LogRequest) returns (stream Record) {}; } message HealthRequest {} @@ -28,24 +28,24 @@ message StatsResponse { uint64 gc = 5; } -// LogRequest queries service for logs +// LogRequest requests service logs message LogRequest { - // count is the count of events + // count of records to request int64 count = 1; // relative time in seconds // before the current time // from which to show logs int64 since = 2; - // stream logs continuously + // stream records continuously bool stream = 3; } -// Log is service log record -message Log { - // timestamp of log event +// Record is service log record +message Record { + // timestamp of log record int64 timestamp = 1; - // log value + // record value string value = 2; - // metadata + // record metadata map metadata = 3; } diff --git a/debug/service/service.go b/debug/service/service.go index 9fda358c..2a0ea006 100644 --- a/debug/service/service.go +++ b/debug/service/service.go @@ -74,7 +74,7 @@ func (d *Debug) streamLogs(logChan chan log.Record, stream pb.Debug_LogsService) } record := log.Record{ - Timestamp: time.Unix(0, resp.Timestamp), + Timestamp: time.Unix(resp.Timestamp, 0), Value: resp.Value, Metadata: metadata, } From b2ecd93404c7c563c0935b2945b88b46d7cf5a81 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Sun, 1 Dec 2019 18:33:08 +0000 Subject: [PATCH 15/15] Time resolution change to be in line with debug/handler.Handler --- debug/service/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug/service/service.go b/debug/service/service.go index 2a0ea006..a30d2836 100644 --- a/debug/service/service.go +++ b/debug/service/service.go @@ -36,7 +36,7 @@ func (d *Debug) Logs(opts ...log.ReadOption) (<-chan log.Record, error) { req := &pb.LogRequest{} if !options.Since.IsZero() { - req.Since = options.Since.UnixNano() + req.Since = options.Since.Unix() } if options.Count > 0 {