2016-01-06 19:24:54 +00:00
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2016-05-28 22:30:47 +01:00
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
2016-01-06 19:24:54 +00:00
|
|
|
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
|
2019-07-28 19:43:50 +01:00
|
|
|
type Handler interface {
|
2016-01-06 19:24:54 +00:00
|
|
|
Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error
|
2016-05-28 22:30:47 +01:00
|
|
|
Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.StatsResponse) error
|
2016-01-06 19:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Our own internal handler
|
2016-05-28 22:30:47 +01:00
|
|
|
type debug struct {
|
|
|
|
started int64
|
|
|
|
}
|
2016-01-06 19:24:54 +00:00
|
|
|
|
2019-07-28 19:43:50 +01:00
|
|
|
// We use this to wrap any debug handlers so we preserve the signature Debug.{Method}
|
|
|
|
type Debug struct {
|
|
|
|
Handler
|
|
|
|
}
|
|
|
|
|
2016-01-06 19:24:54 +00:00
|
|
|
var (
|
2019-07-28 19:43:50 +01:00
|
|
|
DefaultHandler Handler = newDebug()
|
2016-01-06 19:24:54 +00:00
|
|
|
)
|
|
|
|
|
2016-05-28 22:30:47 +01:00
|
|
|
func newDebug() *debug {
|
|
|
|
return &debug{
|
|
|
|
started: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 19:24:54 +00:00
|
|
|
func (d *debug) Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error {
|
|
|
|
rsp.Status = "ok"
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-28 22:30:47 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|