Skip to content

Commit

Permalink
[CLOUDTRUST-5178] Update Sarama lib + add sarama logger (#124)
Browse files Browse the repository at this point in the history
* [CLOUDTRUST-5178] Update Sarama lib + add sarama logger + Force number of digit for id generated
  • Loading branch information
basbeu authored Sep 5, 2023
1 parent 73f7d52 commit 1fcfe29
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 21 deletions.
2 changes: 1 addition & 1 deletion events/auditeventreporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/base64"
"encoding/json"

"github.com/Shopify/sarama"
"github.com/IBM/sarama"
"github.com/cloudtrust/common-service/v2/log"
)

Expand Down
2 changes: 1 addition & 1 deletion events/kafkaproducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/cloudtrust/common-service/v2/log"

"github.com/Shopify/sarama"
"github.com/IBM/sarama"
cs "github.com/cloudtrust/common-service/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion events/mock_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package events

//go:generate mockgen --build_flags=--mod=mod -destination=./mock/sarama.go -package=mock -mock_names=SyncProducer=SyncProducer github.com/Shopify/sarama SyncProducer
//go:generate mockgen --build_flags=--mod=mod -destination=./mock/sarama.go -package=mock -mock_names=SyncProducer=SyncProducer github.com/IBM/sarama SyncProducer
//go:generate mockgen --build_flags=--mod=mod -destination=./mock/log.go -package=mock -mock_names=Logger=Logger github.com/cloudtrust/common-service/v2/log Logger
29 changes: 28 additions & 1 deletion events/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package events
import (
"context"

"github.com/Shopify/sarama"
"github.com/IBM/sarama"
)

// NoopKafkaConsumerGroup is an consumer group that does nothing.
Expand Down Expand Up @@ -49,3 +49,30 @@ func (n *NoopKafkaProducer) SendMessages(msgs []*sarama.ProducerMessage) error {

// Close does noop
func (n *NoopKafkaProducer) Close() error { return nil }

// noop
func (n *NoopKafkaProducer) TxnStatus() sarama.ProducerTxnStatusFlag {
return 0
}

// noop
func (n *NoopKafkaProducer) IsTransactional() bool { return true }

// noop
func (n *NoopKafkaProducer) BeginTxn() error { return nil }

// noop
func (n *NoopKafkaProducer) CommitTxn() error { return nil }

// noop
func (n *NoopKafkaProducer) AbortTxn() error { return nil }

// noop
func (n *NoopKafkaProducer) AddOffsetsToTxn(offsets map[string][]*sarama.PartitionOffsetMetadata, groupId string) error {
return nil
}

// noop
func (n *NoopKafkaProducer) AddMessageToTxn(msg *sarama.ConsumerMessage, groupId string, metadata *string) error {
return nil
}
44 changes: 43 additions & 1 deletion events/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package events
import (
"testing"

"github.com/Shopify/sarama"
"github.com/IBM/sarama"
"github.com/stretchr/testify/assert"
)

Expand All @@ -30,3 +30,45 @@ func TestClose(t *testing.T) {
err := noopProducer.Close()
assert.Nil(t, err)
}

func TestTxnStatus(t *testing.T) {
noopProducer := NoopKafkaProducer{}
txnStatus := noopProducer.TxnStatus()
assert.Equal(t, sarama.ProducerTxnStatusFlag(0), txnStatus)
}

func TestIsTransactional(t *testing.T) {
noopProducer := NoopKafkaProducer{}
b := noopProducer.IsTransactional()
assert.True(t, b)
}

func TestBeginTxn(t *testing.T) {
noopProducer := NoopKafkaProducer{}
err := noopProducer.BeginTxn()
assert.Nil(t, err)
}

func TestCommitTxn(t *testing.T) {
noopProducer := NoopKafkaProducer{}
err := noopProducer.CommitTxn()
assert.Nil(t, err)
}

func TestAbortTxn(t *testing.T) {
noopProducer := NoopKafkaProducer{}
err := noopProducer.AbortTxn()
assert.Nil(t, err)
}

func TestAddOffsetsToTxn(t *testing.T) {
noopProducer := NoopKafkaProducer{}
err := noopProducer.AddOffsetsToTxn(map[string][]*sarama.PartitionOffsetMetadata{}, "")
assert.Nil(t, err)
}

func TestAddMessageToTxn(t *testing.T) {
noopProducer := NoopKafkaProducer{}
err := noopProducer.AddMessageToTxn(nil, "", nil)
assert.Nil(t, err)
}
26 changes: 26 additions & 0 deletions events/saramalogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package events

import (
"context"
"io"
"log"

"github.com/IBM/sarama"
cloudtrust_log "github.com/cloudtrust/common-service/v2/log"
)

func NewSaramaLogger(logger cloudtrust_log.Logger, enabled bool) sarama.StdLogger {
if enabled {
return log.New(&cloudtrustLoggerWrapper{logger}, "[Sarama] ", log.LstdFlags)
}
return log.New(io.Discard, "[Sarama] ", log.LstdFlags)
}

type cloudtrustLoggerWrapper struct {
logger cloudtrust_log.Logger
}

func (c *cloudtrustLoggerWrapper) Write(p []byte) (n int, err error) {
c.logger.Info(context.Background(), "msg", string(p), "tag", "sarama")
return len(p), nil
}
22 changes: 22 additions & 0 deletions events/saramalogger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package events

import (
"testing"

"github.com/cloudtrust/common-service/v2/events/mock"
"github.com/golang/mock/gomock"
)

func TestWrite(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
logger := mock.NewLogger(mockCtrl)

writer := cloudtrustLoggerWrapper{
logger: logger,
}
test := "test"
logger.EXPECT().Info(gomock.Any(), "msg", test, "tag", "sarama")

writer.Write([]byte(test))
}
2 changes: 1 addition & 1 deletion events/tokenprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package events
import (
"context"

"github.com/Shopify/sarama"
"github.com/IBM/sarama"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
Expand Down
27 changes: 15 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/cloudtrust/common-service/v2
go 1.17

require (
github.com/Shopify/sarama v1.32.0
github.com/IBM/sarama v1.41.1
github.com/gbrlsnchs/jwt/v2 v2.0.0
github.com/getsentry/raven-go v0.2.0
github.com/go-kit/kit v0.12.0
Expand All @@ -16,9 +16,9 @@ require (
github.com/nyaruka/phonenumbers v1.0.74
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.8.4
github.com/uber/jaeger-client-go v2.30.0+incompatible
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
golang.org/x/net v0.14.0
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a
gopkg.in/h2non/gentleman.v2 v2.0.5
)
Expand All @@ -27,28 +27,31 @@ require (
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/eapache/go-resiliency v1.2.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect
github.com/eapache/go-resiliency v1.4.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.0.0 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.2 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/klauspost/compress v1.14.4 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 1fcfe29

Please sign in to comment.