Make the debug handler an actual thing that can be set by users

This commit is contained in:
Asim 2016-01-06 19:24:54 +00:00
parent 66107fd304
commit 76918fc703
9 changed files with 137 additions and 91 deletions

21
server/debug.go Normal file
View File

@ -0,0 +1,21 @@
package server
import (
"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}
type Debug struct {
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) {
s.Handle(s.NewHandler(&Debug{s.Options().DebugHandler}))
}

27
server/debug/debug.go Normal file
View File

@ -0,0 +1,27 @@
package debug
import (
proto "github.com/micro/go-micro/server/debug/proto"
"golang.org/x/net/context"
)
// 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 DebugHandler interface {
Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error
}
// Our own internal handler
type debug struct{}
var (
DefaultDebugHandler = new(debug)
)
func (d *debug) Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error {
rsp.Status = "ok"
return nil
}

View File

@ -0,0 +1,57 @@
// Code generated by protoc-gen-go.
// source: go-micro/server/debug/proto/debug.proto
// DO NOT EDIT!
/*
Package debug is a generated protocol buffer package.
It is generated from these files:
go-micro/server/debug/proto/debug.proto
It has these top-level messages:
HealthRequest
HealthResponse
*/
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
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 {
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} }
func init() {
proto.RegisterType((*HealthRequest)(nil), "HealthRequest")
proto.RegisterType((*HealthResponse)(nil), "HealthResponse")
}
var fileDescriptor0 = []byte{
// 107 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x4f, 0xcf, 0xd7, 0xcd,
0xcd, 0x4c, 0x2e, 0xca, 0xd7, 0x2f, 0x4e, 0x2d, 0x2a, 0x4b, 0x2d, 0xd2, 0x4f, 0x49, 0x4d, 0x2a,
0x4d, 0xd7, 0x2f, 0x28, 0xca, 0x2f, 0xc9, 0x87, 0xb0, 0xf5, 0xc0, 0x6c, 0x25, 0x7e, 0x2e, 0x5e,
0x8f, 0xd4, 0xc4, 0x9c, 0x92, 0x8c, 0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2, 0x12, 0x25, 0x05, 0x2e,
0x3e, 0x98, 0x40, 0x71, 0x41, 0x7e, 0x5e, 0x71, 0xaa, 0x10, 0x1f, 0x17, 0x5b, 0x71, 0x49, 0x62,
0x49, 0x69, 0xb1, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x12, 0x1b, 0x58, 0xa7, 0x31, 0x20, 0x00,
0x00, 0xff, 0xff, 0x1c, 0xef, 0x98, 0xac, 0x64, 0x00, 0x00, 0x00,
}

View File

@ -0,0 +1,16 @@
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) {}
// }
message HealthRequest {
}
message HealthResponse {
string status = 1;
}

View File

@ -1,25 +0,0 @@
package server
import (
"github.com/micro/go-micro/server/proto/health"
"golang.org/x/net/context"
)
type (
HealthChecker interface {
Health(ctx context.Context, req *health.Request, rsp *health.Response) error
}
Debug struct{}
)
var DefaultHealthChecker HealthChecker = new(Debug)
func (d *Debug) Health(ctx context.Context, req *health.Request, rsp *health.Response) error {
rsp.Status = "ok"
return nil
}
func registerHealthChecker(s Server) {
s.Handle(s.NewHandler(DefaultHealthChecker))
}

View File

@ -4,6 +4,7 @@ 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"
"golang.org/x/net/context"
@ -23,6 +24,9 @@ type Options struct {
HdlrWrappers []HandlerWrapper
SubWrappers []SubscriberWrapper
// Debug Handler which can be set by a user
DebugHandler debug.DebugHandler
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
@ -50,6 +54,10 @@ func newOptions(opt ...Option) Options {
opts.Transport = transport.DefaultTransport
}
if opts.DebugHandler == nil {
opts.DebugHandler = debug.DefaultDebugHandler
}
if len(opts.Address) == 0 {
opts.Address = DefaultAddress
}
@ -132,6 +140,13 @@ func Transport(t transport.Transport) Option {
}
}
// DebugHandler for this server
func DebugHandler(d debug.DebugHandler) 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

@ -1,57 +0,0 @@
// Code generated by protoc-gen-go.
// source: micro/go-micro/server/proto/health/health.proto
// DO NOT EDIT!
/*
Package health is a generated protocol buffer package.
It is generated from these files:
micro/go-micro/server/proto/health/health.proto
It has these top-level messages:
Request
Response
*/
package health
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
type Request struct {
}
func (m *Request) Reset() { *m = Request{} }
func (m *Request) String() string { return proto.CompactTextString(m) }
func (*Request) ProtoMessage() {}
func (*Request) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type Response struct {
Status string `protobuf:"bytes,1,opt,name=status" json:"status,omitempty"`
}
func (m *Response) Reset() { *m = Response{} }
func (m *Response) String() string { return proto.CompactTextString(m) }
func (*Response) ProtoMessage() {}
func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func init() {
proto.RegisterType((*Request)(nil), "Request")
proto.RegisterType((*Response)(nil), "Response")
}
var fileDescriptor0 = []byte{
// 104 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xd2, 0xcf, 0xcd, 0x4c, 0x2e,
0xca, 0xd7, 0x4f, 0xcf, 0xd7, 0x85, 0x30, 0x8a, 0x53, 0x8b, 0xca, 0x52, 0x8b, 0xf4, 0x0b, 0x8a,
0xf2, 0x4b, 0xf2, 0xf5, 0x33, 0x52, 0x13, 0x73, 0x4a, 0x32, 0xa0, 0x94, 0x1e, 0x58, 0x4c, 0x89,
0x93, 0x8b, 0x3d, 0x28, 0xb5, 0xb0, 0x34, 0xb5, 0xb8, 0x44, 0x49, 0x8a, 0x8b, 0x23, 0x28, 0xb5,
0xb8, 0x20, 0x3f, 0xaf, 0x38, 0x55, 0x88, 0x8f, 0x8b, 0xad, 0xb8, 0x24, 0xb1, 0xa4, 0xb4, 0x58,
0x82, 0x51, 0x81, 0x51, 0x83, 0x33, 0x89, 0x0d, 0xac, 0xda, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff,
0x2c, 0x7c, 0x5d, 0x9d, 0x60, 0x00, 0x00, 0x00,
}

View File

@ -1,8 +0,0 @@
syntax = "proto3";
message Request {
}
message Response {
string status = 1;
}

View File

@ -290,7 +290,7 @@ func (s *rpcServer) Deregister() error {
}
func (s *rpcServer) Start() error {
registerHealthChecker(s)
registerDebugHandler(s)
config := s.Options()
ts, err := config.Transport.Listen(config.Address)