Add monitor/debug packages

This commit is contained in:
Asim Aslam
2019-08-06 17:53:14 +01:00
parent bb01b3ed78
commit 3adce58eb2
21 changed files with 1030 additions and 206 deletions

View File

@@ -1,9 +0,0 @@
package server
import (
"github.com/micro/go-micro/server/debug"
)
func registerDebugHandler(s Server) {
s.Handle(s.NewHandler(&debug.Debug{s.Options().DebugHandler}, InternalHandler(true)))
}

View File

@@ -1,55 +0,0 @@
package debug
import (
"context"
"runtime"
"time"
proto "github.com/micro/go-micro/server/debug/proto"
)
// The debug handler represents an internal server handler
// used to determine health, status and env info about
// a service node. It's akin to Google's /statusz, /healthz,
// and /varz
type Handler interface {
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
type debug struct {
started int64
}
// We use this to wrap any debug handlers so we preserve the signature Debug.{Method}
type Debug struct {
Handler
}
var (
DefaultHandler Handler = newDebug()
)
func newDebug() *debug {
return &debug{
started: time.Now().Unix(),
}
}
func (d *debug) Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error {
rsp.Status = "ok"
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
}

View File

@@ -1,100 +0,0 @@
// Code generated by protoc-gen-go.
// source: github.com/micro/go-micro/server/debug/proto/debug.proto
// DO NOT EDIT!
/*
Package debug is a generated protocol buffer package.
It is generated from these files:
github.com/micro/go-micro/server/debug/proto/debug.proto
It has these top-level messages:
HealthRequest
HealthResponse
StatsRequest
StatsResponse
*/
package debug
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type HealthRequest struct {
}
func (m *HealthRequest) Reset() { *m = HealthRequest{} }
func (m *HealthRequest) String() string { return proto.CompactTextString(m) }
func (*HealthRequest) ProtoMessage() {}
func (*HealthRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type HealthResponse struct {
// default: ok
Status string `protobuf:"bytes,1,opt,name=status" json:"status,omitempty"`
}
func (m *HealthResponse) Reset() { *m = HealthResponse{} }
func (m *HealthResponse) String() string { return proto.CompactTextString(m) }
func (*HealthResponse) ProtoMessage() {}
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() {
proto.RegisterType((*HealthRequest)(nil), "HealthRequest")
proto.RegisterType((*HealthResponse)(nil), "HealthResponse")
proto.RegisterType((*StatsRequest)(nil), "StatsRequest")
proto.RegisterType((*StatsResponse)(nil), "StatsResponse")
}
var fileDescriptor0 = []byte{
// 201 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x34, 0x8f, 0xbd, 0x6e, 0x83, 0x30,
0x14, 0x85, 0x05, 0xa5, 0x54, 0xbd, 0x2a, 0x54, 0x62, 0xa8, 0x3c, 0x56, 0x4c, 0x2c, 0xc5, 0x43,
0x97, 0x3e, 0x42, 0x67, 0xf2, 0x04, 0xfc, 0x5c, 0x19, 0xa4, 0x38, 0x26, 0xbe, 0xd7, 0x91, 0x32,
0xe7, 0xc5, 0x03, 0xb6, 0xd9, 0xce, 0xf7, 0xd9, 0xe7, 0x48, 0x17, 0xfe, 0xd4, 0xc2, 0xb3, 0x1b,
0xda, 0xd1, 0x68, 0xa9, 0x97, 0xd1, 0x1a, 0xa9, 0xcc, 0x4f, 0x08, 0x84, 0xf6, 0x86, 0x56, 0x4e,
0x38, 0x38, 0x25, 0x57, 0x6b, 0xd8, 0x84, 0xdc, 0xfa, 0x5c, 0x7f, 0x42, 0xf1, 0x8f, 0xfd, 0x99,
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,
}

View File

@@ -1,34 +0,0 @@
syntax = "proto3";
// This is commented out due to import cycles.
// But its what we expect the RPC service to
// return.
//
// service Debug {
// rpc Health(HealthRequest) returns (HealthResponse) {}
// rpc Stats(StatsRequest) returns (StatsResponse) {}
// }
message HealthRequest {
}
message HealthResponse {
// default: ok
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;
}

View File

@@ -1,10 +0,0 @@
package grpc
import (
"github.com/micro/go-micro/server"
"github.com/micro/go-micro/server/debug"
)
func registerDebugHandler(s server.Server) {
s.Handle(s.NewHandler(&debug.Debug{s.Options().DebugHandler}, server.InternalHandler(true)))
}

View File

@@ -700,7 +700,6 @@ func (g *grpcServer) Deregister() error {
}
func (g *grpcServer) Start() error {
registerDebugHandler(g)
config := g.opts
// micro: config.Transport.Listen(config.Address)

View File

@@ -8,7 +8,6 @@ import (
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/server"
"github.com/micro/go-micro/server/debug"
"github.com/micro/go-micro/transport"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding"
@@ -89,10 +88,6 @@ func newOptions(opt ...server.Option) server.Options {
opts.Transport = transport.DefaultTransport
}
if opts.DebugHandler == nil {
opts.DebugHandler = debug.DefaultHandler
}
if len(opts.Address) == 0 {
opts.Address = server.DefaultAddress
}

View File

@@ -8,7 +8,6 @@ import (
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/server/debug"
"github.com/micro/go-micro/transport"
)
@@ -36,9 +35,6 @@ type Options struct {
// The router for requests
Router Router
// Debug Handler which can be set by a user
DebugHandler debug.Handler
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
@@ -66,10 +62,6 @@ func newOptions(opt ...Option) Options {
opts.Transport = transport.DefaultTransport
}
if opts.DebugHandler == nil {
opts.DebugHandler = debug.DefaultHandler
}
if opts.RegisterCheck == nil {
opts.RegisterCheck = DefaultRegisterCheck
}
@@ -156,13 +148,6 @@ func Transport(t transport.Transport) Option {
}
}
// DebugHandler for this server
func DebugHandler(d debug.Handler) Option {
return func(o *Options) {
o.DebugHandler = d
}
}
// Metadata associated with the server
func Metadata(md map[string]string) Option {
return func(o *Options) {

View File

@@ -485,7 +485,6 @@ func (s *rpcServer) Deregister() error {
}
func (s *rpcServer) Start() error {
registerDebugHandler(s)
config := s.Options()
// start listening on the transport