2015-12-10 02:07:55 +00:00
|
|
|
package kinesis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/kinesis"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2015-12-10 02:07:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type KinesisOutput struct {
|
|
|
|
Region string `toml:"region"`
|
|
|
|
StreamName string `toml:"streamname"`
|
|
|
|
PartitionKey string `toml:"partitionkey"`
|
|
|
|
Format string `toml:"format"`
|
|
|
|
Debug bool `toml:"debug"`
|
|
|
|
svc *kinesis.Kinesis
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Amazon REGION of kinesis endpoint.
|
2015-12-10 02:07:55 +00:00
|
|
|
region = "ap-southeast-2"
|
2016-02-18 21:26:51 +00:00
|
|
|
## Kinesis StreamName must exist prior to starting telegraf.
|
2015-12-10 02:07:55 +00:00
|
|
|
streamname = "StreamName"
|
2016-02-18 21:26:51 +00:00
|
|
|
## PartitionKey as used for sharding data.
|
2015-12-10 02:07:55 +00:00
|
|
|
partitionkey = "PartitionKey"
|
2016-02-18 21:26:51 +00:00
|
|
|
## format of the Data payload in the kinesis PutRecord, supported
|
|
|
|
## String and Custom.
|
2015-12-10 02:07:55 +00:00
|
|
|
format = "string"
|
2016-02-18 21:26:51 +00:00
|
|
|
## debug will show upstream aws messages.
|
2015-12-10 02:07:55 +00:00
|
|
|
debug = false
|
|
|
|
`
|
|
|
|
|
|
|
|
func (k *KinesisOutput) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KinesisOutput) Description() string {
|
|
|
|
return "Configuration for the AWS Kinesis output."
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkstream(l []*string, s string) bool {
|
|
|
|
// Check if the StreamName exists in the slice returned from the ListStreams API request.
|
|
|
|
for _, stream := range l {
|
|
|
|
if *stream == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KinesisOutput) Connect() error {
|
|
|
|
// We attempt first to create a session to Kinesis using an IAMS role, if that fails it will fall through to using
|
|
|
|
// environment variables, and then Shared Credentials.
|
|
|
|
if k.Debug {
|
|
|
|
log.Printf("kinesis: Establishing a connection to Kinesis in %+v", k.Region)
|
|
|
|
}
|
|
|
|
Config := &aws.Config{
|
|
|
|
Region: aws.String(k.Region),
|
|
|
|
}
|
|
|
|
svc := kinesis.New(session.New(Config))
|
|
|
|
|
|
|
|
KinesisParams := &kinesis.ListStreamsInput{
|
|
|
|
Limit: aws.Int64(100),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := svc.ListStreams(KinesisParams)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("kinesis: Error in ListSteams API call : %+v \n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if checkstream(resp.StreamNames, k.StreamName) {
|
|
|
|
if k.Debug {
|
|
|
|
log.Printf("kinesis: Stream Exists")
|
|
|
|
}
|
|
|
|
k.svc = svc
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
log.Printf("kinesis : You have configured a StreamName %+v which does not exist. exiting.", k.StreamName)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KinesisOutput) Close() error {
|
2016-01-23 00:26:48 +00:00
|
|
|
return nil
|
2015-12-10 02:07:55 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func FormatMetric(k *KinesisOutput, point telegraf.Metric) (string, error) {
|
2015-12-10 02:07:55 +00:00
|
|
|
if k.Format == "string" {
|
|
|
|
return point.String(), nil
|
|
|
|
} else {
|
|
|
|
m := fmt.Sprintf("%+v,%+v,%+v",
|
|
|
|
point.Name(),
|
|
|
|
point.Tags(),
|
|
|
|
point.String())
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writekinesis(k *KinesisOutput, r []*kinesis.PutRecordsRequestEntry) time.Duration {
|
|
|
|
start := time.Now()
|
|
|
|
payload := &kinesis.PutRecordsInput{
|
|
|
|
Records: r,
|
|
|
|
StreamName: aws.String(k.StreamName),
|
|
|
|
}
|
|
|
|
|
|
|
|
if k.Debug {
|
|
|
|
resp, err := k.svc.PutRecords(payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("kinesis: Unable to write to Kinesis : %+v \n", err.Error())
|
|
|
|
}
|
|
|
|
log.Printf("%+v \n", resp)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
_, err := k.svc.PutRecords(payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("kinesis: Unable to write to Kinesis : %+v \n", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Since(start)
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (k *KinesisOutput) Write(metrics []telegraf.Metric) error {
|
2015-12-10 02:07:55 +00:00
|
|
|
var sz uint32 = 0
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
if len(metrics) == 0 {
|
2015-12-10 02:07:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r := []*kinesis.PutRecordsRequestEntry{}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, p := range metrics {
|
2015-12-10 02:07:55 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("kinesis", func() telegraf.Output {
|
2015-12-10 02:07:55 +00:00
|
|
|
return &KinesisOutput{}
|
|
|
|
})
|
|
|
|
}
|