Skip to content

Commit

Permalink
Merge pull request #9 from felippeduran/remove-logrus
Browse files Browse the repository at this point in the history
Remove logrus from API and use a generic logger interface instead
  • Loading branch information
leohahn authored Sep 15, 2020
2 parents aea7d31 + 1c43a0b commit 815bb9a
Show file tree
Hide file tree
Showing 22 changed files with 296 additions and 78 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ About client.NewClient(...) arguments:

* configPrefix: whatever comes just before `client` portion of your config
* config: a viper config with at least a `client` key holding Events Gateway settings
* logger: a logrus.FieldLogger instance
* logger: a logger.Logger instance
* client: should be nil for most cases, except for unit testing
* opts: extra []grpc.DialOption objects

Expand Down Expand Up @@ -45,14 +45,14 @@ import (

"github.com/spf13/viper"
"github.com/topfreegames/eventsgateway"
"github.com/sirupsen/logrus"
"github.com/topfreegames/eventsgateway/logger"
)

func ConfigureEventsGateway() (*eventsgateway.Client, error) {
config := viper.New() // empty Viper config
config.Set("eventsgateway.client.async", true)
config.Set("eventsgateway.client.kafkatopic", "my-client-default-topic")
logger := logrus.WithFields(logrus.Fields{"some": "field"})
logger := &logger.NullLogger{} // Initialize you logger.Logger implementation here
client, err := eventsgateway.NewClient("eventsgateway", config, logger, nil)
if err != nil {
return nil, err
Expand Down
7 changes: 3 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"time"

goMetrics "github.com/rcrowley/go-metrics"
"github.com/topfreegames/eventsgateway/logger"
"github.com/topfreegames/eventsgateway/metrics"
"github.com/topfreegames/eventsgateway/sender"
"github.com/topfreegames/extensions/jaeger"
Expand All @@ -46,8 +47,6 @@ import (
"github.com/spf13/viper"
kafka "github.com/topfreegames/go-extensions-kafka"
pb "github.com/topfreegames/protos/eventsgateway/grpc/generated"

"github.com/sirupsen/logrus"
)

// App is the app structure
Expand All @@ -56,12 +55,12 @@ type App struct {
config *viper.Viper
grpcServer *grpc.Server
host string
log logrus.FieldLogger
log logger.Logger
port int
}

// NewApp creates a new App object
func NewApp(host string, port int, log logrus.FieldLogger, config *viper.Viper) (*App, error) {
func NewApp(host string, port int, log logger.Logger, config *viper.Viper) (*App, error) {
a := &App{
host: host,
port: port,
Expand Down
6 changes: 3 additions & 3 deletions app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ package app
import (
"context"

"github.com/sirupsen/logrus"
"github.com/topfreegames/eventsgateway/logger"
"github.com/topfreegames/eventsgateway/sender"
pb "github.com/topfreegames/protos/eventsgateway/grpc/generated"
)

// Server struct
type Server struct {
logger logrus.FieldLogger
logger logger.Logger
sender sender.Sender
}

// NewServer returns a new grpc server
func NewServer(sender sender.Sender, logger logrus.FieldLogger) *Server {
func NewServer(sender sender.Sender, logger logger.Logger) *Server {
s := &Server{
logger: logger,
sender: sender,
Expand Down
9 changes: 3 additions & 6 deletions app/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/viper"

"testing"

"github.com/topfreegames/eventsgateway/logger"
"github.com/topfreegames/eventsgateway/mocks"
. "github.com/topfreegames/eventsgateway/testing"
mockpb "github.com/topfreegames/protos/eventsgateway/grpc/mock"
Expand All @@ -45,16 +44,14 @@ func TestClient(t *testing.T) {

var (
config *viper.Viper
hook *test.Hook
logger *logrus.Logger
log logger.Logger
mockCtrl *gomock.Controller
mockGRPCServer *mockpb.MockGRPCForwarderServer
mockForwarder *mocks.MockForwarder
)

var _ = BeforeEach(func() {
logger, hook = test.NewNullLogger()
logger.Level = logrus.DebugLevel
log = &logger.NullLogger{}
config, _ = GetDefaultConfig()

mockCtrl = gomock.NewController(GinkgoT())
Expand Down
4 changes: 2 additions & 2 deletions app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var _ = Describe("Client", func() {

BeforeEach(func() {
nowMs = time.Now().UnixNano() / 1000000
sender := sender.NewKafkaSender(mockForwarder, logger, config)
s = app.NewServer(sender, logger)
sender := sender.NewKafkaSender(mockForwarder, log, config)
s = app.NewServer(sender, log)
Expect(s).NotTo(BeNil())
})

Expand Down
20 changes: 10 additions & 10 deletions client/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/opentracing/opentracing-go"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/topfreegames/eventsgateway/logger"
"github.com/topfreegames/eventsgateway/metrics"
pb "github.com/topfreegames/protos/eventsgateway/grpc/generated"
"google.golang.org/grpc"
Expand All @@ -31,7 +31,7 @@ type gRPCClientAsync struct {
eventsChannel chan *pb.Event
lingerInterval time.Duration
batchSize int
logger logrus.FieldLogger
logger logger.Logger
maxRetries int
retryInterval time.Duration
timeout time.Duration
Expand All @@ -41,7 +41,7 @@ type gRPCClientAsync struct {
func newGRPCClientAsync(
configPrefix string,
config *viper.Viper,
logger logrus.FieldLogger,
logger logger.Logger,
serverAddress string,
client pb.GRPCForwarderClient,
opts ...grpc.DialOption,
Expand Down Expand Up @@ -76,7 +76,7 @@ func newGRPCClientAsync(
a.config.SetDefault(timeoutConf, 500*time.Millisecond)
a.timeout = a.config.GetDuration(timeoutConf)

a.logger = a.logger.WithFields(logrus.Fields{
a.logger = a.logger.WithFields(map[string]interface{}{
"lingerInterval": a.lingerInterval,
"batchSize": a.batchSize,
"channelBuffer": channelBuffer,
Expand All @@ -91,7 +91,7 @@ func newGRPCClientAsync(
a.config.SetDefault(numRoutinesConf, 5)
numSendRoutines := a.config.GetInt(numRoutinesConf)

a.logger = a.logger.WithFields(logrus.Fields{
a.logger = a.logger.WithFields(map[string]interface{}{
"numRoutines": numSendRoutines,
})

Expand All @@ -111,7 +111,7 @@ func (a *gRPCClientAsync) configureGRPCForwarderClient(
a.client = client
return nil
}
a.logger.WithFields(logrus.Fields{
a.logger.WithFields(map[string]interface{}{
"operation": "configureGRPCForwarderClient",
}).Info("connecting to grpc server")
tracer := opentracing.GlobalTracer()
Expand Down Expand Up @@ -144,7 +144,7 @@ func (a *gRPCClientAsync) metricsReporterInterceptor(
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
l := a.logger.WithFields(logrus.Fields{
l := a.logger.WithFields(map[string]interface{}{
"method": method,
})

Expand All @@ -160,7 +160,7 @@ func (a *gRPCClientAsync) metricsReporterInterceptor(
retry,
).Observe(elapsedTime)
}
l.WithFields(logrus.Fields{
l.WithFields(map[string]interface{}{
"elapsedTime": elapsedTime,
"reply": reply.(*pb.SendEventsResponse),
}).Debug("request processed")
Expand Down Expand Up @@ -240,7 +240,7 @@ func (a *gRPCClientAsync) sendRoutine() {
}

func (a *gRPCClientAsync) sendEvents(req *pb.SendEventsRequest, retryCount int) {
l := a.logger.WithFields(logrus.Fields{
l := a.logger.WithFields(map[string]interface{}{
"operation": "sendEvents",
"requestId": req.Id,
"retryCount": retryCount,
Expand Down Expand Up @@ -273,7 +273,7 @@ func (a *gRPCClientAsync) sendEvents(req *pb.SendEventsRequest, retryCount int)
return
}
if res != nil && len(res.FailureIndexes) != 0 {
l.WithFields(logrus.Fields{
l.WithFields(map[string]interface{}{
"failureIndexes": res.FailureIndexes,
}).Error("failed to send events")
time.Sleep(time.Duration(math.Pow(2, float64(retryCount))) * a.retryInterval)
Expand Down
26 changes: 20 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/topfreegames/eventsgateway/logger"
logruswrapper "github.com/topfreegames/eventsgateway/logger/logrus"
pb "github.com/topfreegames/protos/eventsgateway/grpc/generated"
"google.golang.org/grpc"
)
Expand All @@ -25,20 +27,32 @@ import (
type Client struct {
client GRPCClient
config *viper.Viper
logger logrus.FieldLogger
logger logger.Logger
topic string
wg sync.WaitGroup
serverAddress string
}

// NewClient ctor
// NewClient ctor (DEPRECATED, use New() instead)
// configPrefix is whatever comes before `client` subpart of config
func NewClient(
configPrefix string,
config *viper.Viper,
logger logrus.FieldLogger,
client pb.GRPCForwarderClient,
opts ...grpc.DialOption,
) (*Client, error) {
return New(configPrefix, config, logruswrapper.NewWithLogger(logger), client, opts...)
}

// New ctor
// configPrefix is whatever comes before `client` subpart of config
func New(
configPrefix string,
config *viper.Viper,
logger logger.Logger,
client pb.GRPCForwarderClient,
opts ...grpc.DialOption,
) (*Client, error) {
if configPrefix != "" && !strings.HasSuffix(configPrefix, ".") {
configPrefix = strings.Join([]string{configPrefix, "."}, "")
Expand All @@ -52,7 +66,7 @@ func NewClient(
if c.topic == "" {
return nil, fmt.Errorf("no kafka topic informed at %s", topicConf)
}
c.logger = c.logger.WithFields(logrus.Fields{
c.logger = c.logger.WithFields(map[string]interface{}{
"source": "eventsgateway/client",
"topic": c.topic,
})
Expand All @@ -76,7 +90,7 @@ func (c *Client) newGRPCClient(
asyncConf := fmt.Sprintf("%sclient.async", configPrefix)
c.config.SetDefault(asyncConf, false)
async := c.config.GetBool(asyncConf)
c.logger = c.logger.WithFields(logrus.Fields{
c.logger = c.logger.WithFields(map[string]interface{}{
"serverAddress": c.serverAddress,
"async": async,
})
Expand All @@ -92,7 +106,7 @@ func (c *Client) Send(
name string,
props map[string]string,
) error {
l := c.logger.WithFields(logrus.Fields{
l := c.logger.WithFields(map[string]interface{}{
"operation": "send",
"event": name,
})
Expand All @@ -111,7 +125,7 @@ func (c *Client) SendToTopic(
props map[string]string,
topic string,
) error {
l := c.logger.WithFields(logrus.Fields{
l := c.logger.WithFields(map[string]interface{}{
"operation": "sendToTopic",
"event": name,
"topic": topic,
Expand Down
9 changes: 3 additions & 6 deletions client/client_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/viper"
"github.com/topfreegames/eventsgateway/logger"

"testing"

Expand All @@ -29,15 +28,13 @@ func TestClient(t *testing.T) {

var (
config *viper.Viper
hook *test.Hook
logger *logrus.Logger
log logger.Logger
mockCtrl *gomock.Controller
mockGRPCClient *mockpb.MockGRPCForwarderClient
)

var _ = BeforeEach(func() {
logger, hook = test.NewNullLogger()
logger.Level = logrus.DebugLevel
log = &logger.NullLogger{}
config, _ = GetDefaultConfig()

mockCtrl = gomock.NewController(GinkgoT())
Expand Down
10 changes: 5 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,29 @@ var _ = Describe("Client", func() {

BeforeEach(func() {
var err error
c, err = client.NewClient("", config, logger, mockGRPCClient)
c, err = client.New("", config, log, mockGRPCClient)
Expect(err).NotTo(HaveOccurred())
now = time.Now().UnixNano() / 1000000
})

Describe("NewClient", func() {
It("should return client if no error", func() {
c, err := client.NewClient("", config, logger, mockGRPCClient)
c, err := client.New("", config, log, mockGRPCClient)
Expect(err).NotTo(HaveOccurred())
Expect(c).NotTo(BeNil())
})

It("should return an error if no kafka topic", func() {
config.Set("client.kafkatopic", "")
c, err := client.NewClient("", config, logger, mockGRPCClient)
c, err := client.New("", config, log, mockGRPCClient)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("no kafka topic informed"))
Expect(c).To(BeNil())
})

It("should return an error if no server address", func() {
config.Set("client.grpc.serveraddress", "")
c, err := client.NewClient("", config, logger, mockGRPCClient)
c, err := client.New("", config, log, mockGRPCClient)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("no grpc server address informed"))
Expect(c).To(BeNil())
Expand All @@ -76,7 +76,7 @@ var _ = Describe("Client", func() {

BeforeEach(func() {
var err error
c, err = client.NewClient("", config, logger, mockGRPCClient)
c, err = client.New("", config, log, mockGRPCClient)
Expect(err).NotTo(HaveOccurred())
now = time.Now().UnixNano() / int64(time.Millisecond)
})
Expand Down
5 changes: 3 additions & 2 deletions client/client_whitebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
logruswrapper "github.com/topfreegames/eventsgateway/logger/logrus"
. "github.com/topfreegames/eventsgateway/testing"
mockpb "github.com/topfreegames/protos/eventsgateway/grpc/mock"
)
Expand All @@ -33,10 +34,10 @@ var _ = Describe("Client Whitebox", func() {
mockCtrl := gomock.NewController(GinkgoT())
mockGRPCClient := mockpb.NewMockGRPCForwarderClient(mockCtrl)
config.Set("client.async", true)
c, err = NewClient(
c, err = New(
"",
config,
logger,
logruswrapper.NewWithLogger(logger),
mockGRPCClient,
)
Expect(err).NotTo(HaveOccurred())
Expand Down
Loading

0 comments on commit 815bb9a

Please sign in to comment.