Decruft the debug logger interface
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Pacjage handler implements service debug handler
|
||||
// Package handler implements service debug handler embedded in go-micro services
|
||||
package handler
|
||||
|
||||
import (
|
||||
@@ -66,26 +66,27 @@ func (d *Debug) Log(ctx context.Context, stream server.Stream) error {
|
||||
}
|
||||
|
||||
if req.Stream {
|
||||
stop := make(chan bool)
|
||||
defer close(stop)
|
||||
|
||||
// TODO: we need to figure out how to close ithe log stream
|
||||
// TODO: we need to figure out how to close the log stream
|
||||
// It seems like when a client disconnects,
|
||||
// the connection stays open until some timeout expires
|
||||
// or something like that; that means the map of streams
|
||||
// might end up leaking memory if not cleaned up properly
|
||||
records := d.log.Stream(stop)
|
||||
records, stop := d.log.Stream()
|
||||
defer close(stop)
|
||||
|
||||
for record := range records {
|
||||
if err := d.sendRecord(record, stream); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// done streaming, return
|
||||
return nil
|
||||
}
|
||||
|
||||
// get the log records
|
||||
records := d.log.Read(options...)
|
||||
|
||||
// send all the logs downstream
|
||||
for _, record := range records {
|
||||
if err := d.sendRecord(record, stream); err != nil {
|
||||
@@ -102,15 +103,9 @@ func (d *Debug) sendRecord(record log.Record, stream server.Stream) error {
|
||||
metadata[k] = v
|
||||
}
|
||||
|
||||
pbRecord := &proto.Record{
|
||||
return stream.Send(&proto.Record{
|
||||
Timestamp: record.Timestamp.Unix(),
|
||||
Value: record.Value.(string),
|
||||
Metadata: metadata,
|
||||
}
|
||||
|
||||
if err := stream.Send(pbRecord); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
81
debug/service/log.go
Normal file
81
debug/service/log.go
Normal file
@@ -0,0 +1,81 @@
|
||||
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/service/proto"
|
||||
)
|
||||
|
||||
type serviceLog struct {
|
||||
Client *debugClient
|
||||
}
|
||||
|
||||
// Read reads log entries from the logger
|
||||
func (s *serviceLog) Read(opts ...log.ReadOption) []log.Record {
|
||||
// TODO: parse opts
|
||||
stream, err := s.Client.Log(opts...)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
// stream the records until nothing is left
|
||||
var records []log.Record
|
||||
for _, record := range stream {
|
||||
records = append(records, record)
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
// There is no write support
|
||||
func (s *serviceLog) Write(r log.Record) {
|
||||
return
|
||||
}
|
||||
|
||||
// Stream log records
|
||||
func (s *serviceLog) Stream(ch chan bool) (<-chan log.Record, chan bool) {
|
||||
stop := make(chan bool)
|
||||
stream, err := s.Client.Log(log.Stream(true))
|
||||
if err != nil {
|
||||
// return a closed stream
|
||||
stream = make(chan log.Record)
|
||||
close(stream)
|
||||
return stream, stop
|
||||
}
|
||||
|
||||
// stream the records until nothing is left
|
||||
go func() {
|
||||
var records []log.Record
|
||||
for _, record := range stream {
|
||||
select {
|
||||
case stream <- record:
|
||||
case <-stop:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// return the stream
|
||||
return stream, stop
|
||||
}
|
||||
|
||||
// NewLog returns a new log interface
|
||||
func NewLog(opts ...log.Option) log.Log {
|
||||
var options log.Options
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
name := options.Name
|
||||
|
||||
// set the default name
|
||||
if len(name) == 0 {
|
||||
name = debug.DefaultName
|
||||
}
|
||||
|
||||
return serviceLog{
|
||||
Client: newDebugClient(name),
|
||||
}
|
||||
}
|
@@ -1,3 +1,4 @@
|
||||
// Package service provides the service log
|
||||
package service
|
||||
|
||||
import (
|
||||
@@ -12,23 +13,23 @@ import (
|
||||
)
|
||||
|
||||
// Debug provides debug service client
|
||||
type Debug struct {
|
||||
dbg pb.DebugService
|
||||
type debugClient struct {
|
||||
Client pb.DebugService
|
||||
}
|
||||
|
||||
// NewDebug provides Debug service implementation
|
||||
func NewDebug(name string) *Debug {
|
||||
func newDebugClient(name string) *debug {
|
||||
// create default client
|
||||
cli := client.DefaultClient
|
||||
|
||||
return &Debug{
|
||||
dbg: pb.NewDebugService(name, cli),
|
||||
return &debugClient{
|
||||
Client: pb.NewDebugService(name, cli),
|
||||
}
|
||||
}
|
||||
|
||||
// Logs queries the service logs and returns a channel to read the logs from
|
||||
func (d *Debug) Log(opts ...log.ReadOption) (<-chan log.Record, error) {
|
||||
options := log.ReadOptions{}
|
||||
func (d *debugClient) Log(opts ...log.ReadOption) (<-chan log.Record, error) {
|
||||
var options log.ReadOptions
|
||||
// initialize the read options
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
@@ -46,20 +47,21 @@ func (d *Debug) Log(opts ...log.ReadOption) (<-chan log.Record, error) {
|
||||
req.Stream = options.Stream
|
||||
|
||||
// get the log stream
|
||||
stream, err := d.dbg.Log(context.Background(), req)
|
||||
stream, err := d.Client.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
|
||||
}
|
||||
|
||||
func (d *Debug) streamLogs(logChan chan log.Record, stream pb.Debug_LogService) {
|
||||
func (d *debugClient) streamLogs(logChan chan log.Record, stream pb.Debug_LogService) {
|
||||
defer stream.Close()
|
||||
|
||||
for {
|
||||
|
Reference in New Issue
Block a user