Add logging when records are emitted w/ record count
This commit is contained in:
parent
c7a69e2c92
commit
3aa0f72efe
5 changed files with 21 additions and 15 deletions
|
|
@ -24,7 +24,7 @@ func main() {
|
||||||
c := connector.NewConsumer(*app, *stream)
|
c := connector.NewConsumer(*app, *stream)
|
||||||
|
|
||||||
// override default values
|
// override default values
|
||||||
c.Set("maxBatchCount", 200)
|
c.Set("maxRecordCount", 200)
|
||||||
|
|
||||||
// start consuming records from the queues
|
// start consuming records from the queues
|
||||||
c.Start(connector.HandlerFunc(func(b connector.Buffer) {
|
c.Start(connector.HandlerFunc(func(b connector.Buffer) {
|
||||||
|
|
|
||||||
11
buffer.go
11
buffer.go
|
|
@ -9,12 +9,12 @@ type Buffer struct {
|
||||||
firstSequenceNumber string
|
firstSequenceNumber string
|
||||||
lastSequenceNumber string
|
lastSequenceNumber string
|
||||||
|
|
||||||
MaxBatchCount int
|
MaxRecordCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddRecord adds a record to the buffer.
|
// AddRecord adds a record to the buffer.
|
||||||
func (b *Buffer) AddRecord(r *kinesis.Record) {
|
func (b *Buffer) AddRecord(r *kinesis.Record) {
|
||||||
if len(b.records) == 0 {
|
if b.RecordCount() == 0 {
|
||||||
b.firstSequenceNumber = *r.SequenceNumber
|
b.firstSequenceNumber = *r.SequenceNumber
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -24,7 +24,7 @@ func (b *Buffer) AddRecord(r *kinesis.Record) {
|
||||||
|
|
||||||
// ShouldFlush determines if the buffer has reached its target size.
|
// ShouldFlush determines if the buffer has reached its target size.
|
||||||
func (b *Buffer) ShouldFlush() bool {
|
func (b *Buffer) ShouldFlush() bool {
|
||||||
return len(b.records) >= b.MaxBatchCount
|
return b.RecordCount() >= b.MaxRecordCount
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush empties the buffer and resets the sequence counter.
|
// Flush empties the buffer and resets the sequence counter.
|
||||||
|
|
@ -37,6 +37,11 @@ func (b *Buffer) GetRecords() []*kinesis.Record {
|
||||||
return b.records
|
return b.records
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RecordCount returns the number of records in the buffer.
|
||||||
|
func (b *Buffer) RecordCount() int {
|
||||||
|
return len(b.records)
|
||||||
|
}
|
||||||
|
|
||||||
// FirstSequenceNumber returns the sequence number of the first record in the buffer.
|
// FirstSequenceNumber returns the sequence number of the first record in the buffer.
|
||||||
func (b *Buffer) FirstSeq() string {
|
func (b *Buffer) FirstSeq() string {
|
||||||
return b.firstSequenceNumber
|
return b.firstSequenceNumber
|
||||||
|
|
|
||||||
12
consumer.go
12
consumer.go
|
|
@ -11,7 +11,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
maxBatchCount = 1000
|
maxRecordCount = 1000
|
||||||
|
maxBufferTime = "30s"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewConsumer creates a new kinesis connection and returns a
|
// NewConsumer creates a new kinesis connection and returns a
|
||||||
|
|
@ -41,8 +42,8 @@ type Consumer struct {
|
||||||
// Set `option` to `value`
|
// Set `option` to `value`
|
||||||
func (c *Consumer) Set(option string, value interface{}) {
|
func (c *Consumer) Set(option string, value interface{}) {
|
||||||
switch option {
|
switch option {
|
||||||
case "maxBatchCount":
|
case "maxRecordCount":
|
||||||
maxBatchCount = value.(int)
|
maxRecordCount = value.(int)
|
||||||
default:
|
default:
|
||||||
log.Error("invalid option")
|
log.Error("invalid option")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
@ -81,7 +82,7 @@ func (c *Consumer) handlerLoop(shardID string, handler Handler) {
|
||||||
})
|
})
|
||||||
|
|
||||||
buf := &Buffer{
|
buf := &Buffer{
|
||||||
MaxBatchCount: maxBatchCount,
|
MaxRecordCount: maxRecordCount,
|
||||||
}
|
}
|
||||||
|
|
||||||
checkpoint := &Checkpoint{
|
checkpoint := &Checkpoint{
|
||||||
|
|
@ -108,7 +109,7 @@ func (c *Consumer) handlerLoop(shardID string, handler Handler) {
|
||||||
}
|
}
|
||||||
|
|
||||||
shardIterator := resp.ShardIterator
|
shardIterator := resp.ShardIterator
|
||||||
ctx.Info("started")
|
ctx.Info("processing")
|
||||||
|
|
||||||
for {
|
for {
|
||||||
resp, err := c.svc.GetRecords(
|
resp, err := c.svc.GetRecords(
|
||||||
|
|
@ -128,6 +129,7 @@ func (c *Consumer) handlerLoop(shardID string, handler Handler) {
|
||||||
|
|
||||||
if buf.ShouldFlush() {
|
if buf.ShouldFlush() {
|
||||||
handler.HandleRecords(*buf)
|
handler.HandleRecords(*buf)
|
||||||
|
ctx.WithField("count", buf.RecordCount()).Info("emitted")
|
||||||
checkpoint.SetCheckpoint(shardID, buf.LastSeq())
|
checkpoint.SetCheckpoint(shardID, buf.LastSeq())
|
||||||
buf.Flush()
|
buf.Flush()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Set(t *testing.T) {
|
func Test_Set(t *testing.T) {
|
||||||
defaultMaxBatchCount := 1000
|
defaultMaxRecordCount := 1000
|
||||||
assert.Equal(t, maxBatchCount, defaultMaxBatchCount)
|
assert.Equal(t, maxRecordCount, defaultMaxRecordCount)
|
||||||
|
|
||||||
c := NewConsumer("app", "stream")
|
c := NewConsumer("app", "stream")
|
||||||
c.Set("maxBatchCount", 100)
|
c.Set("maxRecordCount", 100)
|
||||||
|
|
||||||
assert.Equal(t, maxBatchCount, 100)
|
assert.Equal(t, maxRecordCount, 100)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,7 @@ func main() {
|
||||||
svc := firehose.New(session.New())
|
svc := firehose.New(session.New())
|
||||||
|
|
||||||
c := connector.NewConsumer(*app, *stream)
|
c := connector.NewConsumer(*app, *stream)
|
||||||
c.Set("maxBatchCount", 400)
|
c.Set("maxRecordCount", 400)
|
||||||
c.Set("pollInterval", "3s")
|
|
||||||
c.Start(connector.HandlerFunc(func(b connector.Buffer) {
|
c.Start(connector.HandlerFunc(func(b connector.Buffer) {
|
||||||
params := &firehose.PutRecordBatchInput{
|
params := &firehose.PutRecordBatchInput{
|
||||||
DeliveryStreamName: aws.String(*delivery),
|
DeliveryStreamName: aws.String(*delivery),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue