fix bug where many points would result in a kinesis alert, batching at 500 per PUT

This commit is contained in:
James Lamb 2015-12-10 18:14:41 +11:00
parent e559183a52
commit 6e3ad123b2
2 changed files with 39 additions and 17 deletions

View File

@ -40,4 +40,5 @@ kinesis throughput using a different partition key on different hosts or host gr
## todo ## todo
Check if the stream exists so that we have a graceful exit. * Check if the stream exists so that we have a graceful exit.
* Better client smarts to ensure that the plugin operates within the Amazon service limits.

View File

@ -4,6 +4,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
@ -90,22 +92,8 @@ func formatmetric(k *KinesisOutput, point *client.Point) (string, error) {
} }
} }
func (k *KinesisOutput) Write(points []*client.Point) error { func writekinesis(k *KinesisOutput, r []*kinesis.PutRecordsRequestEntry) time.Duration {
if len(points) == 0 { start := time.Now()
return nil
}
r := []*kinesis.PutRecordsRequestEntry{}
for _, p := range points {
metric, _ := formatmetric(k, p)
d := kinesis.PutRecordsRequestEntry{
Data: []byte(metric),
PartitionKey: aws.String(k.PartitionKey),
}
r = append(r, &d)
}
payload := &kinesis.PutRecordsInput{ payload := &kinesis.PutRecordsInput{
Records: r, Records: r,
StreamName: aws.String(k.StreamName), StreamName: aws.String(k.StreamName),
@ -117,12 +105,45 @@ func (k *KinesisOutput) Write(points []*client.Point) error {
log.Printf("Unable to write to Kinesis : %+v \n", err.Error()) log.Printf("Unable to write to Kinesis : %+v \n", err.Error())
} }
log.Printf("%+v \n", resp) log.Printf("%+v \n", resp)
} else { } else {
_, err := k.svc.PutRecords(payload) _, err := k.svc.PutRecords(payload)
if err != nil { if err != nil {
log.Printf("Unable to write to Kinesis : %+v \n", err.Error()) log.Printf("Unable to write to Kinesis : %+v \n", err.Error())
} }
} }
return time.Since(start)
}
func (k *KinesisOutput) Write(points []*client.Point) error {
var sz uint32 = 0
if len(points) == 0 {
return nil
}
r := []*kinesis.PutRecordsRequestEntry{}
for _, p := range points {
atomic.AddUint32(&sz, 1)
metric, _ := formatmetric(k, p)
d := kinesis.PutRecordsRequestEntry{
Data: []byte(metric),
PartitionKey: aws.String(k.PartitionKey),
}
r = append(r, &d)
if sz == 500 {
// Max Messages Per PutRecordRequest is 500
elapsed := writekinesis(k, r)
log.Printf("Wrote a %+v point batch to Kinesis in %+v.\n", sz, elapsed)
atomic.StoreUint32(&sz, 0)
r = nil
}
}
writekinesis(k, r)
return nil return nil
} }