micro/debug/service/handler/debug.go

118 lines
2.5 KiB
Go
Raw Normal View History

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