2019-12-17 15:38:03 +00:00
|
|
|
// Package handler implements service debug handler embedded in go-micro services
|
2019-08-06 17:53:14 +01:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/debug/log"
|
|
|
|
proto "github.com/micro/go-micro/v2/debug/service/proto"
|
|
|
|
"github.com/micro/go-micro/v2/debug/stats"
|
|
|
|
"github.com/micro/go-micro/v2/debug/trace"
|
|
|
|
"github.com/micro/go-micro/v2/server"
|
2019-08-06 17:53:14 +01:00
|
|
|
)
|
|
|
|
|
2020-01-29 16:05:58 +00:00
|
|
|
// NewHandler returns an instance of the Debug Handler
|
2020-01-29 22:40:43 +00:00
|
|
|
func NewHandler() *Debug {
|
2020-01-29 16:05:58 +00:00
|
|
|
return &Debug{
|
|
|
|
log: log.DefaultLog,
|
|
|
|
stats: stats.DefaultStats,
|
2020-01-29 22:40:43 +00:00
|
|
|
trace: trace.DefaultTracer,
|
2020-01-29 16:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-06 17:53:14 +01:00
|
|
|
|
2019-11-26 17:04:44 +00:00
|
|
|
type Debug struct {
|
2019-12-18 18:36:42 +00:00
|
|
|
// must honour the debug handler
|
2019-11-28 11:05:35 +00:00
|
|
|
proto.DebugHandler
|
2019-12-18 18:36:42 +00:00
|
|
|
// the logger for retrieving logs
|
2019-11-28 11:36:38 +00:00
|
|
|
log log.Log
|
2019-12-18 18:36:42 +00:00
|
|
|
// the stats collector
|
|
|
|
stats stats.Stats
|
2020-01-24 21:29:29 +00:00
|
|
|
// the tracer
|
2020-01-29 15:45:11 +00:00
|
|
|
trace trace.Tracer
|
2019-11-26 17:04:44 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-18 18:36:42 +00:00
|
|
|
stats, err := d.stats.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(stats) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the response values
|
|
|
|
rsp.Timestamp = uint64(stats[0].Timestamp)
|
|
|
|
rsp.Started = uint64(stats[0].Started)
|
|
|
|
rsp.Uptime = uint64(stats[0].Uptime)
|
|
|
|
rsp.Memory = stats[0].Memory
|
|
|
|
rsp.Gc = stats[0].GC
|
|
|
|
rsp.Threads = stats[0].Threads
|
|
|
|
rsp.Requests = stats[0].Requests
|
|
|
|
rsp.Errors = stats[0].Errors
|
|
|
|
|
2019-08-06 17:53:14 +01:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-26 15:39:55 +00:00
|
|
|
|
2020-01-24 21:29:29 +00:00
|
|
|
func (d *Debug) Trace(ctx context.Context, req *proto.TraceRequest, rsp *proto.TraceResponse) error {
|
|
|
|
traces, err := d.trace.Read(trace.ReadTrace(req.Id))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, trace := range traces {
|
|
|
|
rsp.Spans = append(rsp.Spans, &proto.Span{
|
|
|
|
Trace: trace.Trace,
|
|
|
|
Id: trace.Id,
|
|
|
|
Parent: trace.Parent,
|
|
|
|
Name: trace.Name,
|
|
|
|
Started: uint64(trace.Started.UnixNano()),
|
|
|
|
Duration: uint64(trace.Duration.Nanoseconds()),
|
|
|
|
Metadata: trace.Metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-17 15:38:03 +00:00
|
|
|
// TODO: we need to figure out how to close the log stream
|
2019-12-01 13:15:10 +00:00
|
|
|
// 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-12-17 16:56:55 +00:00
|
|
|
lgStream, err := d.log.Stream()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lgStream.Stop()
|
2019-12-17 15:38:03 +00:00
|
|
|
|
2019-12-17 16:56:55 +00:00
|
|
|
for record := range lgStream.Chan() {
|
2019-12-18 18:36:42 +00:00
|
|
|
// copy metadata
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
for k, v := range record.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
// send record
|
|
|
|
if err := stream.Send(&proto.Record{
|
|
|
|
Timestamp: record.Timestamp.Unix(),
|
|
|
|
Message: record.Message.(string),
|
|
|
|
Metadata: metadata,
|
|
|
|
}); err != nil {
|
2019-11-30 12:39:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 15:38:03 +00:00
|
|
|
|
2019-11-30 12:39:29 +00:00
|
|
|
// done streaming, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-28 18:08:48 +00:00
|
|
|
// get the log records
|
2019-12-17 16:56:55 +00:00
|
|
|
records, err := d.log.Read(options...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-17 15:38:03 +00:00
|
|
|
|
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-12-18 18:36:42 +00:00
|
|
|
// copy metadata
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
for k, v := range record.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
// send record
|
|
|
|
if err := stream.Send(&proto.Record{
|
|
|
|
Timestamp: record.Timestamp.Unix(),
|
|
|
|
Message: record.Message.(string),
|
|
|
|
Metadata: metadata,
|
|
|
|
}); err != nil {
|
2019-11-30 12:39:29 +00:00
|
|
|
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
|
|
|
|
}
|