micro/debug/service/service.go

87 lines
1.6 KiB
Go
Raw Normal View History

package service
import (
"context"
"fmt"
"time"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/debug/log"
2019-12-04 12:27:30 +00:00
pb "github.com/micro/go-micro/debug/service/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),
}
}
// Logs queries the service logs and returns a channel to read the logs from
2019-12-05 00:01:17 +00:00
func (d *Debug) Log(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() {
req.Since = options.Since.Unix()
}
if options.Count > 0 {
req.Count = int64(options.Count)
}
req.Stream = options.Stream
// get the log stream
2019-12-04 23:58:29 +00:00
stream, err := d.dbg.Log(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
}
2019-12-04 23:58:29 +00:00
func (d *Debug) streamLogs(logChan chan log.Record, stream pb.Debug_LogService) {
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{
Timestamp: time.Unix(resp.Timestamp, 0),
Value: resp.Value,
Metadata: metadata,
}
logChan <- record
}
close(logChan)
}