forked from smancke/guble
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from cosminrentea/feature/reporting-kafka
Reporting using kafka
- Loading branch information
Showing
15 changed files
with
307 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package configstring | ||
|
||
import ( | ||
"strings" | ||
|
||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
type List []string | ||
|
||
func NewFromKingpin(settings kingpin.Settings) *List { | ||
sl := make(List, 0) | ||
settings.SetValue(&sl) | ||
return &sl | ||
} | ||
|
||
func (sl *List) Set(value string) error { | ||
delimiter := " " | ||
if strings.Contains(value, ",") { | ||
delimiter = "," | ||
} | ||
slice := strings.Split(value, delimiter) | ||
for _, s := range slice { | ||
if s != "" { | ||
*sl = append(*sl, s) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (sl *List) IsEmpty() bool { | ||
return len(*sl) == 0 | ||
} | ||
|
||
func (sl List) String() string { | ||
res := "[" | ||
for _, s := range sl { | ||
res = res + " " + s | ||
} | ||
res = res + "]" | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package configstring | ||
|
||
import ( | ||
"testing" | ||
"github.com/cosminrentea/gobbler/testutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
func TestList_IsEmpty(t *testing.T) { | ||
ctrl, finish := testutil.NewMockCtrl(t) | ||
defer finish() | ||
|
||
m := NewMockSettings(ctrl) | ||
m.EXPECT().SetValue(gomock.Any()) | ||
|
||
cs := NewFromKingpin(m) | ||
|
||
assert.True(t, cs.IsEmpty()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Automatically generated by MockGen. DO NOT EDIT! | ||
// Source: gopkg.in/alecthomas/kingpin.v2 (interfaces: Settings) | ||
|
||
package configstring | ||
|
||
import ( | ||
gomock "github.com/golang/mock/gomock" | ||
kingpin_v2 "gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
// Mock of Settings interface | ||
type MockSettings struct { | ||
ctrl *gomock.Controller | ||
recorder *_MockSettingsRecorder | ||
} | ||
|
||
// Recorder for MockSettings (not exported) | ||
type _MockSettingsRecorder struct { | ||
mock *MockSettings | ||
} | ||
|
||
func NewMockSettings(ctrl *gomock.Controller) *MockSettings { | ||
mock := &MockSettings{ctrl: ctrl} | ||
mock.recorder = &_MockSettingsRecorder{mock} | ||
return mock | ||
} | ||
|
||
func (_m *MockSettings) EXPECT() *_MockSettingsRecorder { | ||
return _m.recorder | ||
} | ||
|
||
func (_m *MockSettings) SetValue(_param0 kingpin_v2.Value) { | ||
_m.ctrl.Call(_m, "SetValue", _param0) | ||
} | ||
|
||
func (_mr *_MockSettingsRecorder) SetValue(arg0 interface{}) *gomock.Call { | ||
return _mr.mock.ctrl.RecordCall(_mr.mock, "SetValue", arg0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package kafka | ||
|
||
import ( | ||
"github.com/Shopify/sarama" | ||
"github.com/cosminrentea/gobbler/server/configstring" | ||
"github.com/cosminrentea/gobbler/server/service" | ||
"time" | ||
) | ||
|
||
type Producer interface { | ||
service.Startable | ||
service.Stopable | ||
Report(topic string, bytes []byte, key string) | ||
} | ||
|
||
type Config struct { | ||
Brokers *configstring.List | ||
} | ||
|
||
type producer struct { | ||
Config | ||
|
||
asyncProducer sarama.AsyncProducer | ||
} | ||
|
||
func NewProducer(c Config) (Producer, error) { | ||
logger.WithField("config", c).Info("NewProducer") | ||
saramaConfig := sarama.NewConfig() | ||
saramaConfig.Version = sarama.V0_10_1_0 | ||
saramaConfig.Producer.Retry.Max = 10 | ||
saramaConfig.Producer.Flush.Frequency = time.Second | ||
p, err := sarama.NewAsyncProducer(*c.Brokers, saramaConfig) | ||
if err != nil { | ||
logger.WithError(err).Error("Could not create AsyncProducer") | ||
return nil, err | ||
} | ||
return &producer{ | ||
Config: c, | ||
asyncProducer: p, | ||
}, nil | ||
} | ||
|
||
func (p *producer) Report(topic string, bytes []byte, key string) { | ||
logger.WithField("topic", topic).Debug("Reporting to Kafka topic") | ||
p.asyncProducer.Input() <- &sarama.ProducerMessage{ | ||
Topic: topic, | ||
Key: sarama.StringEncoder(key), | ||
Value: sarama.ByteEncoder(bytes), | ||
} | ||
} | ||
|
||
func (p *producer) Start() error { | ||
logger.Info("Start") | ||
go func() { | ||
for err := range p.asyncProducer.Errors() { | ||
logger.WithError(err).Error("Could not write to Kafka") | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
func (p *producer) Stop() error { | ||
logger.Info("Stop") | ||
if err := p.asyncProducer.Close(); err != nil { | ||
logger.WithError(err).Error("Could not close Kafka Producer") | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kafka | ||
|
||
import ( | ||
log "github.com/Sirupsen/logrus" | ||
) | ||
|
||
var logger = log.WithField("module", "kafka") |
Oops, something went wrong.