Add debug stats handler
This commit is contained in:
		| @@ -2,9 +2,6 @@ package server | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"github.com/micro/go-micro/server/debug" | 	"github.com/micro/go-micro/server/debug" | ||||||
| 	proto "github.com/micro/go-micro/server/debug/proto" |  | ||||||
|  |  | ||||||
| 	"golang.org/x/net/context" |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // We use this to wrap any debug handlers so we preserve the signature Debug.{Method} | // We use this to wrap any debug handlers so we preserve the signature Debug.{Method} | ||||||
| @@ -12,10 +9,6 @@ type Debug struct { | |||||||
| 	debug.DebugHandler | 	debug.DebugHandler | ||||||
| } | } | ||||||
|  |  | ||||||
| func (d *Debug) Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error { |  | ||||||
| 	return d.DebugHandler.Health(ctx, req, rsp) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func registerDebugHandler(s Server) { | func registerDebugHandler(s Server) { | ||||||
| 	s.Handle(s.NewHandler(&Debug{s.Options().DebugHandler}, InternalHandler(true))) | 	s.Handle(s.NewHandler(&Debug{s.Options().DebugHandler}, InternalHandler(true))) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,6 +1,9 @@ | |||||||
| package debug | package debug | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"runtime" | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
| 	proto "github.com/micro/go-micro/server/debug/proto" | 	proto "github.com/micro/go-micro/server/debug/proto" | ||||||
|  |  | ||||||
| 	"golang.org/x/net/context" | 	"golang.org/x/net/context" | ||||||
| @@ -12,16 +15,37 @@ import ( | |||||||
| // and /varz | // and /varz | ||||||
| type DebugHandler interface { | type DebugHandler interface { | ||||||
| 	Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error | 	Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error | ||||||
|  | 	Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.StatsResponse) error | ||||||
| } | } | ||||||
|  |  | ||||||
| // Our own internal handler | // Our own internal handler | ||||||
| type debug struct{} | type debug struct { | ||||||
|  | 	started int64 | ||||||
|  | } | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| 	DefaultDebugHandler DebugHandler = new(debug) | 	DefaultDebugHandler DebugHandler = newDebug() | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | func newDebug() *debug { | ||||||
|  | 	return &debug{ | ||||||
|  | 		started: time.Now().Unix(), | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| func (d *debug) Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error { | func (d *debug) Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error { | ||||||
| 	rsp.Status = "ok" | 	rsp.Status = "ok" | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (d *debug) Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.StatsResponse) error { | ||||||
|  | 	var mstat runtime.MemStats | ||||||
|  | 	runtime.ReadMemStats(&mstat) | ||||||
|  |  | ||||||
|  | 	rsp.Started = uint64(d.started) | ||||||
|  | 	rsp.Uptime = uint64(time.Now().Unix() - d.started) | ||||||
|  | 	rsp.Memory = mstat.Alloc | ||||||
|  | 	rsp.Gc = mstat.PauseTotalNs | ||||||
|  | 	rsp.Threads = uint64(runtime.NumGoroutine()) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|   | |||||||
| @@ -1,16 +1,18 @@ | |||||||
| // Code generated by protoc-gen-go. | // Code generated by protoc-gen-go. | ||||||
| // source: go-micro/server/debug/proto/debug.proto | // source: github.com/micro/go-micro/server/debug/proto/debug.proto | ||||||
| // DO NOT EDIT! | // DO NOT EDIT! | ||||||
|  |  | ||||||
| /* | /* | ||||||
| Package debug is a generated protocol buffer package. | Package debug is a generated protocol buffer package. | ||||||
|  |  | ||||||
| It is generated from these files: | It is generated from these files: | ||||||
| 	go-micro/server/debug/proto/debug.proto | 	github.com/micro/go-micro/server/debug/proto/debug.proto | ||||||
|  |  | ||||||
| It has these top-level messages: | It has these top-level messages: | ||||||
| 	HealthRequest | 	HealthRequest | ||||||
| 	HealthResponse | 	HealthResponse | ||||||
|  | 	StatsRequest | ||||||
|  | 	StatsResponse | ||||||
| */ | */ | ||||||
| package debug | package debug | ||||||
|  |  | ||||||
| @@ -23,6 +25,12 @@ var _ = proto.Marshal | |||||||
| var _ = fmt.Errorf | var _ = fmt.Errorf | ||||||
| var _ = math.Inf | 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.ProtoPackageIsVersion2 // please upgrade the proto package | ||||||
|  |  | ||||||
| type HealthRequest struct { | type HealthRequest struct { | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -32,6 +40,7 @@ func (*HealthRequest) ProtoMessage()               {} | |||||||
| func (*HealthRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } | func (*HealthRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } | ||||||
|  |  | ||||||
| type HealthResponse struct { | type HealthResponse struct { | ||||||
|  | 	// default: ok | ||||||
| 	Status string `protobuf:"bytes,1,opt,name=status" json:"status,omitempty"` | 	Status string `protobuf:"bytes,1,opt,name=status" json:"status,omitempty"` | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -40,18 +49,52 @@ func (m *HealthResponse) String() string            { return proto.CompactTextSt | |||||||
| func (*HealthResponse) ProtoMessage()               {} | func (*HealthResponse) ProtoMessage()               {} | ||||||
| func (*HealthResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } | func (*HealthResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } | ||||||
|  |  | ||||||
|  | type StatsRequest struct { | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (m *StatsRequest) Reset()                    { *m = StatsRequest{} } | ||||||
|  | func (m *StatsRequest) String() string            { return proto.CompactTextString(m) } | ||||||
|  | func (*StatsRequest) ProtoMessage()               {} | ||||||
|  | func (*StatsRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } | ||||||
|  |  | ||||||
|  | type StatsResponse struct { | ||||||
|  | 	// unix timestamp | ||||||
|  | 	Started uint64 `protobuf:"varint,1,opt,name=started" json:"started,omitempty"` | ||||||
|  | 	// in seconds | ||||||
|  | 	Uptime uint64 `protobuf:"varint,2,opt,name=uptime" json:"uptime,omitempty"` | ||||||
|  | 	// in bytes | ||||||
|  | 	Memory uint64 `protobuf:"varint,3,opt,name=memory" json:"memory,omitempty"` | ||||||
|  | 	// num threads | ||||||
|  | 	Threads uint64 `protobuf:"varint,4,opt,name=threads" json:"threads,omitempty"` | ||||||
|  | 	// in nanoseconds | ||||||
|  | 	Gc uint64 `protobuf:"varint,5,opt,name=gc" json:"gc,omitempty"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (m *StatsResponse) Reset()                    { *m = StatsResponse{} } | ||||||
|  | func (m *StatsResponse) String() string            { return proto.CompactTextString(m) } | ||||||
|  | func (*StatsResponse) ProtoMessage()               {} | ||||||
|  | func (*StatsResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } | ||||||
|  |  | ||||||
| func init() { | func init() { | ||||||
| 	proto.RegisterType((*HealthRequest)(nil), "HealthRequest") | 	proto.RegisterType((*HealthRequest)(nil), "HealthRequest") | ||||||
| 	proto.RegisterType((*HealthResponse)(nil), "HealthResponse") | 	proto.RegisterType((*HealthResponse)(nil), "HealthResponse") | ||||||
|  | 	proto.RegisterType((*StatsRequest)(nil), "StatsRequest") | ||||||
|  | 	proto.RegisterType((*StatsResponse)(nil), "StatsResponse") | ||||||
| } | } | ||||||
|  |  | ||||||
| var fileDescriptor0 = []byte{ | var fileDescriptor0 = []byte{ | ||||||
| 	// 107 bytes of a gzipped FileDescriptorProto | 	// 201 bytes of a gzipped FileDescriptorProto | ||||||
| 	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x4f, 0xcf, 0xd7, 0xcd, | 	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x34, 0x8f, 0xbd, 0x6e, 0x83, 0x30, | ||||||
| 	0xcd, 0x4c, 0x2e, 0xca, 0xd7, 0x2f, 0x4e, 0x2d, 0x2a, 0x4b, 0x2d, 0xd2, 0x4f, 0x49, 0x4d, 0x2a, | 	0x14, 0x85, 0x05, 0xa5, 0x54, 0xbd, 0x2a, 0x54, 0x62, 0xa8, 0x3c, 0x56, 0x4c, 0x2c, 0xc5, 0x43, | ||||||
| 	0x4d, 0xd7, 0x2f, 0x28, 0xca, 0x2f, 0xc9, 0x87, 0xb0, 0xf5, 0xc0, 0x6c, 0x25, 0x7e, 0x2e, 0x5e, | 	0x97, 0x3e, 0x42, 0x67, 0xf2, 0x04, 0xfc, 0x5c, 0x19, 0xa4, 0x38, 0x26, 0xbe, 0xd7, 0x91, 0x32, | ||||||
| 	0x8f, 0xd4, 0xc4, 0x9c, 0x92, 0x8c, 0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2, 0x12, 0x25, 0x05, 0x2e, | 	0xe7, 0xc5, 0x03, 0xb6, 0xd9, 0xce, 0xf7, 0xd9, 0xe7, 0x48, 0x17, 0xfe, 0xd4, 0xc2, 0xb3, 0x1b, | ||||||
| 	0x3e, 0x98, 0x40, 0x71, 0x41, 0x7e, 0x5e, 0x71, 0xaa, 0x10, 0x1f, 0x17, 0x5b, 0x71, 0x49, 0x62, | 	0xda, 0xd1, 0x68, 0xa9, 0x97, 0xd1, 0x1a, 0xa9, 0xcc, 0x4f, 0x08, 0x84, 0xf6, 0x86, 0x56, 0x4e, | ||||||
| 	0x49, 0x69, 0xb1, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x12, 0x1b, 0x58, 0xa7, 0x31, 0x20, 0x00, | 	0x38, 0x38, 0x25, 0x57, 0x6b, 0xd8, 0x84, 0xdc, 0xfa, 0x5c, 0x7f, 0x42, 0xf1, 0x8f, 0xfd, 0x99, | ||||||
| 	0x00, 0xff, 0xff, 0x1c, 0xef, 0x98, 0xac, 0x64, 0x00, 0x00, 0x00, | 	0xe7, 0x0e, 0xaf, 0x0e, 0x89, 0xeb, 0x06, 0xca, 0x43, 0xd0, 0x6a, 0x2e, 0x84, 0xd5, 0x17, 0xe4, | ||||||
|  | 	0xc4, 0x3d, 0x3b, 0x12, 0xc9, 0x77, 0xd2, 0xbc, 0x77, 0x91, 0xea, 0x12, 0x3e, 0x4e, 0x5b, 0xa2, | ||||||
|  | 	0xa3, 0xf9, 0x48, 0xa0, 0x88, 0x22, 0x36, 0x05, 0xbc, 0x6d, 0x7f, 0x2d, 0xe3, 0xe4, 0xab, 0x59, | ||||||
|  | 	0x77, 0xe0, 0xbe, 0xe9, 0x56, 0x5e, 0x34, 0x8a, 0xd4, 0x3f, 0x44, 0xda, 0xbd, 0x46, 0x6d, 0xec, | ||||||
|  | 	0x5d, 0xbc, 0x04, 0x1f, 0x68, 0x5f, 0xe2, 0xd9, 0x62, 0x3f, 0x91, 0xc8, 0xc2, 0x52, 0xc4, 0xaa, | ||||||
|  | 	0x84, 0x54, 0x8d, 0xe2, 0xd5, 0xcb, 0x2d, 0x0d, 0xb9, 0xbf, 0xeb, 0xf7, 0x19, 0x00, 0x00, 0xff, | ||||||
|  | 	0xff, 0xc6, 0x75, 0x51, 0x35, 0x13, 0x01, 0x00, 0x00, | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,11 +6,29 @@ syntax = "proto3"; | |||||||
| // | // | ||||||
| // service Debug { | // service Debug { | ||||||
| // 	rpc Health(HealthRequest) returns (HealthResponse) {} | // 	rpc Health(HealthRequest) returns (HealthResponse) {} | ||||||
|  | // 	rpc Stats(StatsRequest) returns (StatsResponse) {} | ||||||
| // } | // } | ||||||
|  |  | ||||||
| message HealthRequest { | message HealthRequest { | ||||||
| } | } | ||||||
|  |  | ||||||
| message HealthResponse { | message HealthResponse { | ||||||
|  | 	// default: ok | ||||||
| 	string status = 1; | 	string status = 1; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | message StatsRequest { | ||||||
|  | } | ||||||
|  |  | ||||||
|  | message StatsResponse { | ||||||
|  | 	// unix timestamp | ||||||
|  | 	uint64 started = 1; | ||||||
|  | 	// in seconds | ||||||
|  | 	uint64 uptime = 2; | ||||||
|  | 	// in bytes | ||||||
|  | 	uint64 memory = 3; | ||||||
|  | 	// num threads | ||||||
|  | 	uint64 threads = 4; | ||||||
|  | 	// total gc in nanoseconds | ||||||
|  | 	uint64 gc = 5; | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user