From 881af1f07868238421dc73504447ad34c4c26646 Mon Sep 17 00:00:00 2001 From: Stephen Kwong Date: Tue, 19 Jan 2016 16:34:17 -0800 Subject: [PATCH] Limit number of CloudWatch Dimensions to 10. --- plugins/outputs/cloudwatch/cloudwatch.go | 36 ++++++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/plugins/outputs/cloudwatch/cloudwatch.go b/plugins/outputs/cloudwatch/cloudwatch.go index 589b5ddd4..cb8375d09 100644 --- a/plugins/outputs/cloudwatch/cloudwatch.go +++ b/plugins/outputs/cloudwatch/cloudwatch.go @@ -2,6 +2,8 @@ package cloudwatch import ( "log" + "math" + "sort" "strings" "time" @@ -162,19 +164,41 @@ func buildMetricDatum(point *client.Point) []*cloudwatch.MetricDatum { return datums } -// Make a list of Dimensions by using a Point's tags. +// 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 { - dimensions := make([]*cloudwatch.Dimension, len(ptTags)) + const MaxDimensions = 10 + dimensions := make([]*cloudwatch.Dimension, int(math.Min(float64(len(ptTags)), MaxDimensions))) + i := 0 - for k, v := range ptTags { + // This is pretty ugly but we always want to include the "host" tag if it exists. + if host, ok := ptTags["host"]; ok { dimensions[i] = &cloudwatch.Dimension{ - Name: aws.String(k), - Value: aws.String(v), + Name: aws.String("host"), + Value: aws.String(host), + } + delete(ptTags, "host") + i += 1 + } + + var keys []string + for k := range ptTags { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, k := range keys { + if i <= MaxDimensions { + break } - i += 1 + dimensions[i] = &cloudwatch.Dimension{ + Name: aws.String(k), + Value: aws.String(ptTags[k]), + } } return dimensions