2019-12-17 17:08:38 +00:00
|
|
|
// Package service provides the service log
|
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/debug/log"
|
|
|
|
pb "github.com/micro/go-micro/v2/debug/service/proto"
|
2019-12-17 17:08:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Debug provides debug service client
|
|
|
|
type debugClient struct {
|
|
|
|
Client pb.DebugService
|
|
|
|
}
|
|
|
|
|
2020-01-30 23:24:46 +00:00
|
|
|
func (d *debugClient) Trace() ([]*pb.Span, error) {
|
|
|
|
rsp, err := d.Client.Trace(context.Background(), &pb.TraceRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rsp.Spans, nil
|
|
|
|
}
|
|
|
|
|
2019-12-17 17:08:38 +00:00
|
|
|
// Logs queries the services logs and returns a channel to read the logs from
|
|
|
|
func (d *debugClient) Log(since time.Time, count int, stream bool) (log.Stream, error) {
|
|
|
|
req := &pb.LogRequest{}
|
|
|
|
if !since.IsZero() {
|
|
|
|
req.Since = since.Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
req.Count = int64(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set whether to stream
|
|
|
|
req.Stream = stream
|
|
|
|
|
|
|
|
// get the log stream
|
|
|
|
serverStream, err := d.Client.Log(context.Background(), req)
|
|
|
|
if err != nil {
|
2019-12-19 18:25:22 +00:00
|
|
|
return nil, err
|
2019-12-17 17:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lg := &logStream{
|
|
|
|
stream: make(chan log.Record),
|
|
|
|
stop: make(chan bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
// go stream logs
|
|
|
|
go d.streamLogs(lg, serverStream)
|
|
|
|
|
|
|
|
return lg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *debugClient) streamLogs(lg *logStream, stream pb.Debug_LogService) {
|
|
|
|
defer stream.Close()
|
|
|
|
defer lg.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
for k, v := range resp.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
record := log.Record{
|
|
|
|
Timestamp: time.Unix(resp.Timestamp, 0),
|
2019-12-18 16:02:11 +00:00
|
|
|
Message: resp.Message,
|
2019-12-17 17:08:38 +00:00
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-lg.stop:
|
|
|
|
return
|
|
|
|
case lg.stream <- record:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 18:24:00 +00:00
|
|
|
|
|
|
|
// NewClient provides a debug client
|
|
|
|
func NewClient(name string) *debugClient {
|
|
|
|
// create default client
|
|
|
|
cli := client.DefaultClient
|
|
|
|
|
|
|
|
return &debugClient{
|
|
|
|
Client: pb.NewDebugService(name, cli),
|
|
|
|
}
|
|
|
|
}
|