Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Add Log Interface #147

Merged
merged 2 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions docs/GRPC-Reporter-Option.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

`GRPCReporterOption` allows for functional options to adjust behaviour of a `gRPC` reporter to be created by `NewGRPCReporter`.

| Function | Describe |
| ---------- | --- |
| `reporter.WithLogger` | setup logger for gRPC reporter |
| `reporter.WithCheckInterval` | setup service and endpoint registry check interval |
| `reporter.WithMaxSendQueueSize` | setup send span queue buffer length |
| `reporter.WithInstanceProps` | setup service instance properties eg: org=SkyAPM |
| `reporter.WithTransportCredentials` | setup transport layer security |
| `reporter.WithAuthentication` | used Authentication for gRPC |
| `reporter.WithCDS` | setup CDS service |
| Function | Describe |
|-------------------------------------|----------------------------------------------------|
| `reporter.WithLog` | setup log for gRPC reporter |
| `reporter.WithCheckInterval` | setup service and endpoint registry check interval |
| `reporter.WithMaxSendQueueSize` | setup send span queue buffer length |
| `reporter.WithInstanceProps` | setup service instance properties eg: org=SkyAPM |
| `reporter.WithTransportCredentials` | setup transport layer security |
| `reporter.WithAuthentication` | used Authentication for gRPC |
| `reporter.WithCDS` | setup CDS service |
69 changes: 69 additions & 0 deletions logger/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Copyright 2022 SkyAPM org
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package logger

import "log"

type Log interface {
// Info logs to the INFO log.
Info(args ...interface{})
// Infof logs to the INFO log.
Infof(format string, args ...interface{})
// Warn logs to the WARNING and INFO logs.
Warn(args ...interface{})
// Warnf logs to the WARNING and INFO logs.
Warnf(format string, args ...interface{})
// Error logs to the ERROR, WARNING, and INFO logs.
Error(args ...interface{})
// Errorf logs to the ERROR, WARNING, and INFO logs.
Errorf(format string, args ...interface{})
}

type defaultLogger struct {
logger *log.Logger
}

func (d defaultLogger) Info(args ...interface{}) {
d.logger.Print(args...)
}

func (d defaultLogger) Infof(format string, args ...interface{}) {
d.logger.Printf(format, args...)
}

func (d defaultLogger) Warn(args ...interface{}) {
d.logger.Print(args...)
}

func (d defaultLogger) Warnf(format string, args ...interface{}) {
d.logger.Printf(format, args...)
}

func (d defaultLogger) Error(args ...interface{}) {
d.logger.Print(args...)
}

func (d defaultLogger) Errorf(format string, args ...interface{}) {
d.logger.Printf(format, args...)
}

// NewDefaultLogger Creates a new Log
func NewDefaultLogger(logger *log.Logger) Log {
return &defaultLogger{
logger: logger,
}
}
33 changes: 21 additions & 12 deletions reporter/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/internal/tool"
"github.com/SkyAPM/go2sky/logger"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
Expand All @@ -45,7 +46,7 @@ const (
// NewGRPCReporter create a new reporter to send data to gRPC oap server. Only one backend address is allowed.
func NewGRPCReporter(serverAddr string, opts ...GRPCReporterOption) (go2sky.Reporter, error) {
r := &gRPCReporter{
logger: log.New(os.Stderr, defaultLogPrefix, log.LstdFlags),
logger: logger.NewDefaultLogger(log.New(os.Stderr, defaultLogPrefix, log.LstdFlags)),
sendCh: make(chan *agentv3.SegmentObject, maxSendQueueSize),
checkInterval: defaultCheckInterval,
}
Expand Down Expand Up @@ -85,7 +86,15 @@ func NewGRPCReporter(serverAddr string, opts ...GRPCReporterOption) (go2sky.Repo
type GRPCReporterOption func(r *gRPCReporter)

// WithLogger setup logger for gRPC reporter
func WithLogger(logger *log.Logger) GRPCReporterOption {
// Deprecated: WithLog is recommended
func WithLogger(log *log.Logger) GRPCReporterOption {
return func(r *gRPCReporter) {
r.logger = logger.NewDefaultLogger(log)
}
}

// WithLog setup log for gRPC reporter
func WithLog(logger logger.Log) GRPCReporterOption {
return func(r *gRPCReporter) {
r.logger = logger
}
Expand Down Expand Up @@ -137,7 +146,7 @@ type gRPCReporter struct {
service string
serviceInstance string
instanceProps map[string]string
logger *log.Logger
logger logger.Log
sendCh chan *agentv3.SegmentObject
conn *grpc.ClientConn
traceClient agentv3.TraceSegmentReportServiceClient
Expand Down Expand Up @@ -223,13 +232,13 @@ func (r *gRPCReporter) Send(spans []go2sky.ReportedSpan) {
defer func() {
// recover the panic caused by close sendCh
if err := recover(); err != nil {
r.logger.Printf("reporter segment err %v", err)
r.logger.Errorf("reporter segment err %v", err)
}
}()
select {
case r.sendCh <- segmentObject:
default:
r.logger.Printf("reach max send buffer")
r.logger.Errorf("reach max send buffer")
}
}

Expand All @@ -244,7 +253,7 @@ func (r *gRPCReporter) Close() {
func (r *gRPCReporter) closeGRPCConn() {
if r.conn != nil {
if err := r.conn.Close(); err != nil {
r.logger.Print(err)
r.logger.Error(err)
}
}
}
Expand All @@ -258,14 +267,14 @@ func (r *gRPCReporter) initSendPipeline() {
for {
stream, err := r.traceClient.Collect(metadata.NewOutgoingContext(context.Background(), r.md))
if err != nil {
r.logger.Printf("open stream error %v", err)
r.logger.Errorf("open stream error %v", err)
time.Sleep(5 * time.Second)
continue StreamLoop
}
for s := range r.sendCh {
err = stream.Send(s)
if err != nil {
r.logger.Printf("send segment error %v", err)
r.logger.Errorf("send segment error %v", err)
r.closeStream(stream)
continue StreamLoop
}
Expand Down Expand Up @@ -298,7 +307,7 @@ func (r *gRPCReporter) initCDS(cdsWatchers []go2sky.AgentConfigChangeWatcher) {
})

if err != nil {
r.logger.Printf("fetch dynamic configuration error %v", err)
r.logger.Errorf("fetch dynamic configuration error %v", err)
time.Sleep(r.checkInterval)
continue
}
Expand All @@ -316,7 +325,7 @@ func (r *gRPCReporter) initCDS(cdsWatchers []go2sky.AgentConfigChangeWatcher) {
func (r *gRPCReporter) closeStream(stream agentv3.TraceSegmentReportService_CollectClient) {
_, err := stream.CloseAndRecv()
if err != nil && err != io.EOF {
r.logger.Printf("send closing error %v", err)
r.logger.Errorf("send closing error %v", err)
}
}

Expand Down Expand Up @@ -352,7 +361,7 @@ func (r *gRPCReporter) check() {
if !instancePropertiesSubmitted {
err := r.reportInstanceProperties()
if err != nil {
r.logger.Printf("report serviceInstance properties error %v", err)
r.logger.Errorf("report serviceInstance properties error %v", err)
time.Sleep(r.checkInterval)
continue
}
Expand All @@ -365,7 +374,7 @@ func (r *gRPCReporter) check() {
})

if err != nil {
r.logger.Printf("send keep alive signal error %v", err)
r.logger.Errorf("send keep alive signal error %v", err)
}
time.Sleep(r.checkInterval)
}
Expand Down
53 changes: 49 additions & 4 deletions reporter/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (
"fmt"
"log"
"os"
"reflect"
"testing"
"time"
"unsafe"

"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/logger"
"github.com/SkyAPM/go2sky/propagation"
mock "github.com/SkyAPM/go2sky/reporter/grpc/management/mock_management"
"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -133,7 +136,10 @@ func TestGRPCReporterOption(t *testing.T) {
instanceProps["org"] = "SkyAPM"

// log
logger := log.New(os.Stderr, "WithLogger", log.LstdFlags)
log1 := log.New(os.Stderr, "WithLogger", log.LstdFlags)

// custom log
log2 := &testLog{}

// tls
creds, err := credentials.NewClientTLSFromFile("../test/test-data/certs/cert.crt", "SkyAPM.org")
Expand Down Expand Up @@ -180,13 +186,24 @@ func TestGRPCReporterOption(t *testing.T) {
},
{
name: "with logger",
option: WithLogger(logger),
option: WithLogger(log1),
verifyFunc: func(t *testing.T, reporter *gRPCReporter) {
if reporter.logger != logger {
log3 := reflect.ValueOf(reporter.logger).Elem().FieldByName("logger")
log3 = reflect.NewAt(log3.Type(), unsafe.Pointer(log3.UnsafeAddr())).Elem()
if log3.Interface() != log1 {
t.Error("error are not set logger")
}
},
},
{
name: "with log",
option: WithLog(log2),
verifyFunc: func(t *testing.T, reporter *gRPCReporter) {
if reporter.logger != log2 {
t.Error("error are not set log")
}
},
},
{
name: "with auth",
option: WithAuthentication("test"),
Expand Down Expand Up @@ -258,7 +275,7 @@ func TestGRPCReporter_reportInstanceProperties(t *testing.T) {

func createGRPCReporter() *gRPCReporter {
reporter := &gRPCReporter{
logger: log.New(os.Stderr, "go2sky", log.LstdFlags),
logger: logger.NewDefaultLogger(log.New(os.Stderr, "go2sky", log.LstdFlags)),
}
return reporter
}
Expand Down Expand Up @@ -288,3 +305,31 @@ func (e instancePropertiesMatcher) Matches(x interface{}) bool {
func (e instancePropertiesMatcher) String() string {
return fmt.Sprintf("is equal to %v", e.x)
}

// testLog test only
type testLog struct {
}

func (t testLog) Info(args ...interface{}) {
fmt.Print(args...)
}

func (t testLog) Infof(format string, args ...interface{}) {
fmt.Printf(format, args...)
}

func (t testLog) Warn(args ...interface{}) {
fmt.Print(args...)
}

func (t testLog) Warnf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}

func (t testLog) Error(args ...interface{}) {
fmt.Print(args...)
}

func (t testLog) Errorf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}