Throughout telegraf, use telegraf.Metric rather than client.Point
closes #599
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
## Amazon CloudWatch Output for Telegraf
|
||||
|
||||
This plugin will send points to Amazon CloudWatch.
|
||||
This plugin will send metrics to Amazon CloudWatch.
|
||||
|
||||
## Amazon Authentication
|
||||
|
||||
|
||||
@@ -14,9 +14,8 @@ import (
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
|
||||
"github.com/influxdata/influxdb/client/v2"
|
||||
"github.com/influxdata/telegraf/plugins/outputs"
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/outputs"
|
||||
)
|
||||
|
||||
type CloudWatch struct {
|
||||
@@ -73,9 +72,9 @@ func (c *CloudWatch) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CloudWatch) Write(points []*client.Point) error {
|
||||
for _, pt := range points {
|
||||
err := c.WriteSinglePoint(pt)
|
||||
func (c *CloudWatch) Write(metrics []telegraf.Metric) error {
|
||||
for _, m := range metrics {
|
||||
err := c.WriteSinglePoint(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -87,10 +86,10 @@ func (c *CloudWatch) Write(points []*client.Point) error {
|
||||
// Write data for a single point. A point can have many fields and one field
|
||||
// is equal to one MetricDatum. There is a limit on how many MetricDatums a
|
||||
// request can have so we process one Point at a time.
|
||||
func (c *CloudWatch) WriteSinglePoint(point *client.Point) error {
|
||||
func (c *CloudWatch) WriteSinglePoint(point telegraf.Metric) error {
|
||||
datums := BuildMetricDatum(point)
|
||||
|
||||
const maxDatumsPerCall = 20 // PutMetricData only supports up to 20 data points per call
|
||||
const maxDatumsPerCall = 20 // PutMetricData only supports up to 20 data metrics per call
|
||||
|
||||
for _, partition := range PartitionDatums(maxDatumsPerCall, datums) {
|
||||
err := c.WriteToCloudWatch(partition)
|
||||
@@ -144,7 +143,7 @@ func PartitionDatums(size int, datums []*cloudwatch.MetricDatum) [][]*cloudwatch
|
||||
|
||||
// Make a MetricDatum for each field in a Point. Only fields with values that can be
|
||||
// converted to float64 are supported. Non-supported fields are skipped.
|
||||
func BuildMetricDatum(point *client.Point) []*cloudwatch.MetricDatum {
|
||||
func BuildMetricDatum(point telegraf.Metric) []*cloudwatch.MetricDatum {
|
||||
datums := make([]*cloudwatch.MetricDatum, len(point.Fields()))
|
||||
i := 0
|
||||
|
||||
@@ -190,15 +189,15 @@ func BuildMetricDatum(point *client.Point) []*cloudwatch.MetricDatum {
|
||||
// Make a list of Dimensions by using a Point's tags. CloudWatch supports up to
|
||||
// 10 dimensions per metric so we only keep up to the first 10 alphabetically.
|
||||
// This always includes the "host" tag if it exists.
|
||||
func BuildDimensions(ptTags map[string]string) []*cloudwatch.Dimension {
|
||||
func BuildDimensions(mTags map[string]string) []*cloudwatch.Dimension {
|
||||
|
||||
const MaxDimensions = 10
|
||||
dimensions := make([]*cloudwatch.Dimension, int(math.Min(float64(len(ptTags)), MaxDimensions)))
|
||||
dimensions := make([]*cloudwatch.Dimension, int(math.Min(float64(len(mTags)), MaxDimensions)))
|
||||
|
||||
i := 0
|
||||
|
||||
// This is pretty ugly but we always want to include the "host" tag if it exists.
|
||||
if host, ok := ptTags["host"]; ok {
|
||||
if host, ok := mTags["host"]; ok {
|
||||
dimensions[i] = &cloudwatch.Dimension{
|
||||
Name: aws.String("host"),
|
||||
Value: aws.String(host),
|
||||
@@ -207,7 +206,7 @@ func BuildDimensions(ptTags map[string]string) []*cloudwatch.Dimension {
|
||||
}
|
||||
|
||||
var keys []string
|
||||
for k := range ptTags {
|
||||
for k := range mTags {
|
||||
if k != "host" {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
@@ -221,7 +220,7 @@ func BuildDimensions(ptTags map[string]string) []*cloudwatch.Dimension {
|
||||
|
||||
dimensions[i] = &cloudwatch.Dimension{
|
||||
Name: aws.String(k),
|
||||
Value: aws.String(ptTags[k]),
|
||||
Value: aws.String(mTags[k]),
|
||||
}
|
||||
|
||||
i += 1
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
|
||||
"github.com/influxdata/influxdb/client/v2"
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -19,7 +19,7 @@ func TestBuildDimensions(t *testing.T) {
|
||||
|
||||
assert := assert.New(t)
|
||||
|
||||
testPoint := testutil.TestPoint(1)
|
||||
testPoint := testutil.TestMetric(1)
|
||||
dimensions := BuildDimensions(testPoint.Tags())
|
||||
|
||||
tagKeys := make([]string, len(testPoint.Tags()))
|
||||
@@ -46,25 +46,25 @@ func TestBuildDimensions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Test that points with valid values have a MetricDatum created where as non valid do not.
|
||||
// Test that metrics with valid values have a MetricDatum created where as non valid do not.
|
||||
// Skips "time.Time" type as something is converting the value to string.
|
||||
func TestBuildMetricDatums(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
validPoints := []*client.Point{
|
||||
testutil.TestPoint(1),
|
||||
testutil.TestPoint(int32(1)),
|
||||
testutil.TestPoint(int64(1)),
|
||||
testutil.TestPoint(float64(1)),
|
||||
testutil.TestPoint(true),
|
||||
validMetrics := []telegraf.Metric{
|
||||
testutil.TestMetric(1),
|
||||
testutil.TestMetric(int32(1)),
|
||||
testutil.TestMetric(int64(1)),
|
||||
testutil.TestMetric(float64(1)),
|
||||
testutil.TestMetric(true),
|
||||
}
|
||||
|
||||
for _, point := range validPoints {
|
||||
for _, point := range validMetrics {
|
||||
datums := BuildMetricDatum(point)
|
||||
assert.Equal(1, len(datums), "Valid type should create a Datum")
|
||||
}
|
||||
|
||||
nonValidPoint := testutil.TestPoint("Foo")
|
||||
nonValidPoint := testutil.TestMetric("Foo")
|
||||
|
||||
assert.Equal(0, len(BuildMetricDatum(nonValidPoint)), "Invalid type should not create a Datum")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user