2019-11-28 18:08:48 +00:00
|
|
|
// Pacjage handler implements service debug handler
|
2019-08-06 17:53:14 +01:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
2019-11-27 16:12:39 +00:00
|
|
|
"github.com/micro/go-micro/debug/log"
|
2019-12-04 12:27:30 +00:00
|
|
|
proto "github.com/micro/go-micro/debug/service/proto"
|
2019-12-02 14:55:35 +00:00
|
|
|
"github.com/micro/go-micro/server"
|
2019-08-06 17:53:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-26 17:04:44 +00:00
|
|
|
// DefaultHandler is default debug handler
|
2019-08-06 17:53:14 +01:00
|
|
|
DefaultHandler = newDebug()
|
|
|
|
)
|
|
|
|
|
2019-11-26 17:04:44 +00:00
|
|
|
type Debug struct {
|
|
|
|
started int64
|
2019-11-28 11:05:35 +00:00
|
|
|
proto.DebugHandler
|
2019-11-28 11:36:38 +00:00
|
|
|
log log.Log
|
2019-11-26 17:04:44 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 17:53:14 +01:00
|
|
|
func newDebug() *Debug {
|
|
|
|
return &Debug{
|
|
|
|
started: time.Now().Unix(),
|
2019-11-28 11:36:38 +00:00
|
|
|
log: log.DefaultLog,
|
2019-08-06 17:53:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-12-04 23:51:07 +00:00
|
|
|
rsp.Timestamp = uint64(time.Now().Unix())
|
2019-08-06 17:53:14 +01:00
|
|
|
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
|
|
|
|
}
|
2019-11-26 15:39:55 +00:00
|
|
|
|
2019-12-04 23:58:29 +00:00
|
|
|
func (d *Debug) Log(ctx context.Context, stream server.Stream) error {
|
2019-12-02 14:55:35 +00:00
|
|
|
req := new(proto.LogRequest)
|
|
|
|
if err := stream.Recv(req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-28 18:08:48 +00:00
|
|
|
var options []log.ReadOption
|
|
|
|
|
2019-12-01 13:15:10 +00:00
|
|
|
since := time.Unix(req.Since, 0)
|
2019-11-27 18:38:26 +00:00
|
|
|
if !since.IsZero() {
|
2019-11-28 18:08:48 +00:00
|
|
|
options = append(options, log.Since(since))
|
|
|
|
}
|
|
|
|
|
|
|
|
count := int(req.Count)
|
|
|
|
if count > 0 {
|
|
|
|
options = append(options, log.Count(count))
|
2019-11-27 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 12:39:29 +00:00
|
|
|
if req.Stream {
|
|
|
|
stop := make(chan bool)
|
|
|
|
defer close(stop)
|
|
|
|
|
2019-12-01 13:15:10 +00:00
|
|
|
// TODO: we need to figure out how to close ithe log stream
|
|
|
|
// It seems like when a client disconnects,
|
2019-11-30 12:39:29 +00:00
|
|
|
// the connection stays open until some timeout expires
|
|
|
|
// or something like that; that means the map of streams
|
2019-12-01 13:15:10 +00:00
|
|
|
// might end up leaking memory if not cleaned up properly
|
2019-11-30 12:39:29 +00:00
|
|
|
records := d.log.Stream(stop)
|
|
|
|
for record := range records {
|
|
|
|
if err := d.sendRecord(record, stream); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// done streaming, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-28 18:08:48 +00:00
|
|
|
// get the log records
|
|
|
|
records := d.log.Read(options...)
|
2019-11-30 12:39:29 +00:00
|
|
|
// send all the logs downstream
|
2019-11-27 18:38:26 +00:00
|
|
|
for _, record := range records {
|
2019-11-30 12:39:29 +00:00
|
|
|
if err := d.sendRecord(record, stream); err != nil {
|
|
|
|
return err
|
2019-11-27 18:38:26 +00:00
|
|
|
}
|
2019-11-30 12:39:29 +00:00
|
|
|
}
|
2019-11-27 18:38:26 +00:00
|
|
|
|
2019-11-30 12:39:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-27 18:38:26 +00:00
|
|
|
|
2019-12-02 14:55:35 +00:00
|
|
|
func (d *Debug) sendRecord(record log.Record, stream server.Stream) error {
|
2019-11-30 12:39:29 +00:00
|
|
|
metadata := make(map[string]string)
|
|
|
|
for k, v := range record.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:15:10 +00:00
|
|
|
pbRecord := &proto.Record{
|
|
|
|
Timestamp: record.Timestamp.Unix(),
|
2019-11-30 12:39:29 +00:00
|
|
|
Value: record.Value.(string),
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:15:10 +00:00
|
|
|
if err := stream.Send(pbRecord); err != nil {
|
2019-11-30 12:39:29 +00:00
|
|
|
return err
|
2019-11-27 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 15:39:55 +00:00
|
|
|
return nil
|
|
|
|
}
|