Skip tags with empty values in cloudwatch output (#4785)

This commit is contained in:
Daniel Nelson 2018-10-01 18:27:44 -07:00 committed by GitHub
parent 798ce7e88f
commit 5101f075e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 14 deletions

View File

@ -281,6 +281,7 @@ func PartitionDatums(size int, datums []*cloudwatch.MetricDatum) [][]*cloudwatch
func BuildMetricDatum(buildStatistic bool, point telegraf.Metric) []*cloudwatch.MetricDatum { func BuildMetricDatum(buildStatistic bool, point telegraf.Metric) []*cloudwatch.MetricDatum {
fields := make(map[string]cloudwatchField) fields := make(map[string]cloudwatchField)
tags := point.Tags()
for k, v := range point.Fields() { for k, v := range point.Fields() {
@ -298,7 +299,7 @@ func BuildMetricDatum(buildStatistic bool, point telegraf.Metric) []*cloudwatch.
fields[k] = &valueField{ fields[k] = &valueField{
metricName: point.Name(), metricName: point.Name(),
fieldName: k, fieldName: k,
tags: point.Tags(), tags: tags,
timestamp: point.Time(), timestamp: point.Time(),
value: val, value: val,
} }
@ -311,7 +312,7 @@ func BuildMetricDatum(buildStatistic bool, point telegraf.Metric) []*cloudwatch.
fields[fieldName] = &statisticField{ fields[fieldName] = &statisticField{
metricName: point.Name(), metricName: point.Name(),
fieldName: fieldName, fieldName: fieldName,
tags: point.Tags(), tags: tags,
timestamp: point.Time(), timestamp: point.Time(),
values: map[statisticType]float64{ values: map[statisticType]float64{
sType: val, sType: val,
@ -336,19 +337,15 @@ func BuildMetricDatum(buildStatistic bool, point telegraf.Metric) []*cloudwatch.
// 10 dimensions per metric so we only keep up to the first 10 alphabetically. // 10 dimensions per metric so we only keep up to the first 10 alphabetically.
// This always includes the "host" tag if it exists. // This always includes the "host" tag if it exists.
func BuildDimensions(mTags map[string]string) []*cloudwatch.Dimension { func BuildDimensions(mTags map[string]string) []*cloudwatch.Dimension {
const MaxDimensions = 10 const MaxDimensions = 10
dimensions := make([]*cloudwatch.Dimension, int(math.Min(float64(len(mTags)), MaxDimensions))) dimensions := make([]*cloudwatch.Dimension, 0, MaxDimensions)
i := 0
// This is pretty ugly but we always want to include the "host" tag if it exists. // This is pretty ugly but we always want to include the "host" tag if it exists.
if host, ok := mTags["host"]; ok { if host, ok := mTags["host"]; ok {
dimensions[i] = &cloudwatch.Dimension{ dimensions = append(dimensions, &cloudwatch.Dimension{
Name: aws.String("host"), Name: aws.String("host"),
Value: aws.String(host), Value: aws.String(host),
} })
i += 1
} }
var keys []string var keys []string
@ -360,16 +357,19 @@ func BuildDimensions(mTags map[string]string) []*cloudwatch.Dimension {
sort.Strings(keys) sort.Strings(keys)
for _, k := range keys { for _, k := range keys {
if i >= MaxDimensions { if len(dimensions) >= MaxDimensions {
break break
} }
dimensions[i] = &cloudwatch.Dimension{ value := mTags[k]
Name: aws.String(k), if value == "" {
Value: aws.String(mTags[k]), continue
} }
i += 1 dimensions = append(dimensions, &cloudwatch.Dimension{
Name: aws.String(k),
Value: aws.String(mTags[k]),
})
} }
return dimensions return dimensions

View File

@ -15,6 +15,7 @@ import (
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
// Test that each tag becomes one dimension // Test that each tag becomes one dimension
@ -115,6 +116,23 @@ func TestBuildMetricDatums(t *testing.T) {
assert.Equal(7, len(datums), fmt.Sprintf("Valid point should create a Datum {value: %v}", multiStatisticMetric)) assert.Equal(7, len(datums), fmt.Sprintf("Valid point should create a Datum {value: %v}", multiStatisticMetric))
} }
func TestBuildMetricDatums_SkipEmptyTags(t *testing.T) {
input := testutil.MustMetric(
"cpu",
map[string]string{
"host": "example.org",
"foo": "",
},
map[string]interface{}{
"value": int64(42),
},
time.Unix(0, 0),
)
datums := BuildMetricDatum(true, input)
require.Len(t, datums[0].Dimensions, 1)
}
func TestPartitionDatums(t *testing.T) { func TestPartitionDatums(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)