-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cleanup and adding support for sharding based on metric name #3170
Merged
danielnelson
merged 6 commits into
influxdata:master
from
nevins-b:kinesis_partition_key
Aug 28, 2017
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
67b6048
cleanup and adding support for sharding based on metric name
5b4d2eb
updating readme
ba60d2b
adding tests, updating README, making partitionKey much more flexible
0bfd085
cleaning up sample config, removing extra whitespace
98c8ca7
Update kinesis.go
nevins-b be5dba8
Update kinesis.go
nevins-b File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,23 +15,31 @@ import ( | |
"github.com/influxdata/telegraf/plugins/serializers" | ||
) | ||
|
||
type KinesisOutput struct { | ||
Region string `toml:"region"` | ||
AccessKey string `toml:"access_key"` | ||
SecretKey string `toml:"secret_key"` | ||
RoleARN string `toml:"role_arn"` | ||
Profile string `toml:"profile"` | ||
Filename string `toml:"shared_credential_file"` | ||
Token string `toml:"token"` | ||
|
||
StreamName string `toml:"streamname"` | ||
PartitionKey string `toml:"partitionkey"` | ||
RandomPartitionKey bool `toml:"use_random_partitionkey"` | ||
Debug bool `toml:"debug"` | ||
svc *kinesis.Kinesis | ||
|
||
serializer serializers.Serializer | ||
} | ||
type ( | ||
KinesisOutput struct { | ||
Region string `toml:"region"` | ||
AccessKey string `toml:"access_key"` | ||
SecretKey string `toml:"secret_key"` | ||
RoleARN string `toml:"role_arn"` | ||
Profile string `toml:"profile"` | ||
Filename string `toml:"shared_credential_file"` | ||
Token string `toml:"token"` | ||
|
||
StreamName string `toml:"streamname"` | ||
PartitionKey string `toml:"partitionkey"` | ||
RandomPartitionKey bool `toml:"use_random_partitionkey"` | ||
Partition *Partition `toml:"partition"` | ||
Debug bool `toml:"debug"` | ||
svc *kinesis.Kinesis | ||
|
||
serializer serializers.Serializer | ||
} | ||
|
||
Partition struct { | ||
Method string `toml:"method"` | ||
Key string `toml:"key"` | ||
} | ||
) | ||
|
||
var sampleConfig = ` | ||
## Amazon REGION of kinesis endpoint. | ||
|
@@ -54,12 +62,32 @@ var sampleConfig = ` | |
|
||
## Kinesis StreamName must exist prior to starting telegraf. | ||
streamname = "StreamName" | ||
## PartitionKey as used for sharding data. | ||
## DEPRECATED: PartitionKey as used for sharding data, if not set the metric name will be used. | ||
partitionkey = "PartitionKey" | ||
## If set the paritionKey will be a random UUID on every put. | ||
## DEPRECATED: If set the paritionKey will be a random UUID on every put. | ||
## This allows for scaling across multiple shards in a stream. | ||
## This will cause issues with ordering. | ||
use_random_partitionkey = false | ||
## The partition key can be calculated using one of several methods: | ||
## | ||
## Use a static value for all writes: | ||
# [[outputs.kinesis.partition]] | ||
# method = "static" | ||
# key = "howdy" | ||
# | ||
## Use a random partition key on each write: | ||
# [outputs.kinesis.partition] | ||
# method = "random" | ||
# | ||
## Use the measurement name as the partition key: | ||
# [[outputs.kinesis.partition]] | ||
# method = "measurement" | ||
# | ||
## Use the value of a tag for all writes, if the tag is not set the empty | ||
## string will be used: | ||
# [[outputs.kinesis.partition]] | ||
# method = "tag" | ||
# key = "host" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use 2 space indent in the sample config. |
||
|
||
|
||
## Data format to output. | ||
|
@@ -129,6 +157,9 @@ func (k *KinesisOutput) Connect() error { | |
log.Printf("E! kinesis : You have configured a StreamName %+v which does not exist. exiting.", k.StreamName) | ||
os.Exit(1) | ||
} | ||
if k.Partition == nil { | ||
log.Print("E! kinesis : Deprecated paritionkey configuration in use, please consider using outputs.kinesis.partition") | ||
} | ||
return err | ||
} | ||
|
||
|
@@ -159,10 +190,37 @@ func writekinesis(k *KinesisOutput, r []*kinesis.PutRecordsRequestEntry) time.Du | |
if err != nil { | ||
log.Printf("E! kinesis: Unable to write to Kinesis : %+v \n", err.Error()) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
} | ||
return time.Since(start) | ||
} | ||
|
||
func (k *KinesisOutput) getPartitionKey(metric telegraf.Metric) string { | ||
if k.Partition != nil { | ||
switch k.Partition.Method { | ||
case "static": | ||
return k.Partition.Key | ||
case "random": | ||
u := uuid.NewV4() | ||
return u.String() | ||
case "measurement": | ||
return metric.Name() | ||
case "tag": | ||
if metric.HasTag(k.Partition.Key) { | ||
return metric.Tags()[k.Partition.Key] | ||
} | ||
log.Printf("E! kinesis : You have configured a Partition using tag %+v which does not exist.", k.Partition.Key) | ||
default: | ||
log.Printf("E! kinesis : You have configured a Partition method of %+v which is not supported", k.Partition.Method) | ||
} | ||
} | ||
if k.RandomPartitionKey { | ||
u := uuid.NewV4() | ||
return u.String() | ||
} | ||
return k.PartitionKey | ||
} | ||
|
||
func (k *KinesisOutput) Write(metrics []telegraf.Metric) error { | ||
var sz uint32 | ||
|
||
|
@@ -180,11 +238,7 @@ func (k *KinesisOutput) Write(metrics []telegraf.Metric) error { | |
return err | ||
} | ||
|
||
partitionKey := k.PartitionKey | ||
if k.RandomPartitionKey { | ||
u := uuid.NewV4() | ||
partitionKey = u.String() | ||
} | ||
partitionKey := k.getPartitionKey(metric) | ||
|
||
d := kinesis.PutRecordsRequestEntry{ | ||
Data: values, | ||
|
@@ -202,8 +256,10 @@ func (k *KinesisOutput) Write(metrics []telegraf.Metric) error { | |
} | ||
|
||
} | ||
|
||
writekinesis(k, r) | ||
if sz > 0 { | ||
elapsed := writekinesis(k, r) | ||
log.Printf("E! Wrote a %+v point batch to Kinesis in %+v.\n", sz, elapsed) | ||
} | ||
|
||
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,77 @@ | ||
package kinesis | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/influxdata/telegraf/testutil" | ||
uuid "github.com/satori/go.uuid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPartitionKey(t *testing.T) { | ||
|
||
assert := assert.New(t) | ||
testPoint := testutil.TestMetric(1) | ||
|
||
k := KinesisOutput{ | ||
Partition: &Partition{ | ||
Method: "static", | ||
Key: "-", | ||
}, | ||
} | ||
assert.Equal("-", k.getPartitionKey(testPoint), "PartitionKey should be '-'") | ||
|
||
k = KinesisOutput{ | ||
Partition: &Partition{ | ||
Method: "tag", | ||
Key: "tag1", | ||
}, | ||
} | ||
assert.Equal(testPoint.Tags()["tag1"], k.getPartitionKey(testPoint), "PartitionKey should be value of 'tag1'") | ||
|
||
k = KinesisOutput{ | ||
Partition: &Partition{ | ||
Method: "tag", | ||
Key: "doesnotexist", | ||
}, | ||
} | ||
assert.Equal("", k.getPartitionKey(testPoint), "PartitionKey should be value of ''") | ||
|
||
k = KinesisOutput{ | ||
Partition: &Partition{ | ||
Method: "not supported", | ||
}, | ||
} | ||
assert.Equal("", k.getPartitionKey(testPoint), "PartitionKey should be value of ''") | ||
|
||
k = KinesisOutput{ | ||
Partition: &Partition{ | ||
Method: "measurement", | ||
}, | ||
} | ||
assert.Equal(testPoint.Name(), k.getPartitionKey(testPoint), "PartitionKey should be value of measurement name") | ||
|
||
k = KinesisOutput{ | ||
Partition: &Partition{ | ||
Method: "random", | ||
}, | ||
} | ||
partitionKey := k.getPartitionKey(testPoint) | ||
u, err := uuid.FromString(partitionKey) | ||
assert.Nil(err, "Issue parsing UUID") | ||
assert.Equal(uint(4), u.Version(), "PartitionKey should be UUIDv4") | ||
|
||
k = KinesisOutput{ | ||
PartitionKey: "-", | ||
} | ||
assert.Equal("-", k.getPartitionKey(testPoint), "PartitionKey should be '-'") | ||
|
||
k = KinesisOutput{ | ||
RandomPartitionKey: true, | ||
} | ||
partitionKey = k.getPartitionKey(testPoint) | ||
u, err = uuid.FromString(partitionKey) | ||
assert.Nil(err, "Issue parsing UUID") | ||
assert.Equal(uint(4), u.Version(), "PartitionKey should be UUIDv4") | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make these all single bracket options so you can only set one.