2019-11-27 20:32:54 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/debug/log"
|
|
|
|
pb "github.com/micro/go-micro/debug/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Debug provides debug service client
|
|
|
|
type Debug struct {
|
|
|
|
dbg pb.DebugService
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDebug provides Debug service implementation
|
|
|
|
func NewDebug(name string) *Debug {
|
|
|
|
// create default client
|
|
|
|
cli := client.DefaultClient
|
|
|
|
|
|
|
|
return &Debug{
|
|
|
|
dbg: pb.NewDebugService(name, cli),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 18:08:48 +00:00
|
|
|
// Logs queries the service logs and returns a channel to read the logs from
|
2019-11-27 20:32:54 +00:00
|
|
|
func (d *Debug) Logs(opts ...log.ReadOption) (<-chan log.Record, error) {
|
|
|
|
options := log.ReadOptions{}
|
|
|
|
// initialize the read options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &pb.LogRequest{}
|
|
|
|
if !options.Since.IsZero() {
|
2019-12-01 18:33:08 +00:00
|
|
|
req.Since = options.Since.Unix()
|
2019-11-27 20:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.Count > 0 {
|
|
|
|
req.Count = int64(options.Count)
|
|
|
|
}
|
|
|
|
|
2019-11-30 12:39:29 +00:00
|
|
|
req.Stream = options.Stream
|
|
|
|
|
2019-11-27 20:32:54 +00:00
|
|
|
// get the log stream
|
|
|
|
stream, err := d.dbg.Logs(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed getting log stream: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// log channel for streaming logs
|
|
|
|
logChan := make(chan log.Record)
|
|
|
|
// go stream logs
|
|
|
|
go d.streamLogs(logChan, stream)
|
|
|
|
|
|
|
|
return logChan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Debug) streamLogs(logChan chan log.Record, stream pb.Debug_LogsService) {
|
|
|
|
defer stream.Close()
|
|
|
|
|
|
|
|
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{
|
2019-12-01 13:15:10 +00:00
|
|
|
Timestamp: time.Unix(resp.Timestamp, 0),
|
2019-11-27 20:32:54 +00:00
|
|
|
Value: resp.Value,
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
logChan <- record
|
|
|
|
}
|
|
|
|
|
|
|
|
close(logChan)
|
|
|
|
}
|