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