rename files

This commit is contained in:
Asim Aslam 2019-12-17 17:08:38 +00:00
parent 50d5c6402b
commit 46fd205eda
3 changed files with 129 additions and 129 deletions

88
debug/service/client.go Normal file
View File

@ -0,0 +1,88 @@
// Package service provides the service log
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"
)
// Debug provides debug service client
type debugClient struct {
Client pb.DebugService
}
// NewClient provides a debug client
func NewClient(name string) *debugClient {
// create default client
cli := client.DefaultClient
return &debugClient{
Client: pb.NewDebugService(name, cli),
}
}
// 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 {
return nil, fmt.Errorf("failed getting log stream: %s", err)
}
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),
Value: resp.Value,
Metadata: metadata,
}
select {
case <-lg.stop:
return
case lg.stream <- record:
}
}
}

View File

@ -1,64 +0,0 @@
package service
import (
"time"
"github.com/micro/go-micro/debug"
"github.com/micro/go-micro/debug/log"
)
type serviceLog struct {
Client *debugClient
}
// Read reads log entries from the logger
func (s *serviceLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
var options log.ReadOptions
for _, o := range opts {
o(&options)
}
stream, err := s.Client.Log(options.Since, options.Count, false)
if err != nil {
return nil, err
}
defer stream.Stop()
// stream the records until nothing is left
var records []log.Record
for record := range stream.Chan() {
records = append(records, record)
}
return records, nil
}
// There is no write support
func (s *serviceLog) Write(r log.Record) error {
return nil
}
// Stream log records
func (s *serviceLog) Stream() (log.Stream, error) {
return s.Client.Log(time.Time{}, 0, true)
}
// 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: NewClient(name),
}
}

View File

@ -1,88 +1,64 @@
// Package service provides the service log
package service package service
import ( import (
"context"
"fmt"
"time" "time"
"github.com/micro/go-micro/client" "github.com/micro/go-micro/debug"
"github.com/micro/go-micro/debug/log" "github.com/micro/go-micro/debug/log"
pb "github.com/micro/go-micro/debug/service/proto"
) )
// Debug provides debug service client type serviceLog struct {
type debugClient struct { Client *debugClient
Client pb.DebugService
} }
// NewClient provides a debug client // Read reads log entries from the logger
func NewClient(name string) *debugClient { func (s *serviceLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
// create default client var options log.ReadOptions
cli := client.DefaultClient for _, o := range opts {
o(&options)
return &debugClient{
Client: pb.NewDebugService(name, cli),
}
}
// 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 { stream, err := s.Client.Log(options.Since, options.Count, false)
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 { if err != nil {
return nil, fmt.Errorf("failed getting log stream: %s", err) return nil, err
}
defer stream.Stop()
// stream the records until nothing is left
var records []log.Record
for record := range stream.Chan() {
records = append(records, record)
} }
lg := &logStream{ return records, nil
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) { // There is no write support
defer stream.Close() func (s *serviceLog) Write(r log.Record) error {
defer lg.Stop() return nil
}
for { // Stream log records
resp, err := stream.Recv() func (s *serviceLog) Stream() (log.Stream, error) {
if err != nil { return s.Client.Log(time.Time{}, 0, true)
break }
}
metadata := make(map[string]string) // NewLog returns a new log interface
for k, v := range resp.Metadata { func NewLog(opts ...log.Option) log.Log {
metadata[k] = v var options log.Options
} for _, o := range opts {
o(&options)
}
record := log.Record{ name := options.Name
Timestamp: time.Unix(resp.Timestamp, 0),
Value: resp.Value,
Metadata: metadata,
}
select { // set the default name
case <-lg.stop: if len(name) == 0 {
return name = debug.DefaultName
case lg.stream <- record: }
}
return &serviceLog{
Client: NewClient(name),
} }
} }