From c12b89c8caff0a26c2b30502bbd2c2bdace66679 Mon Sep 17 00:00:00 2001 From: John Engelman Date: Thu, 5 May 2016 09:05:36 -0500 Subject: [PATCH] Allow for wildcard in dimension values to select all available metrics. --- internal/config/aws/credentials.go | 6 +- plugins/inputs/cloudwatch/cloudwatch.go | 74 +++++++++++++++++++++---- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/internal/config/aws/credentials.go b/internal/config/aws/credentials.go index 9516702d0..5a89d8bff 100644 --- a/internal/config/aws/credentials.go +++ b/internal/config/aws/credentials.go @@ -3,9 +3,9 @@ package aws import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/client" - "github.com/awslabs/aws-sdk-go/aws/credentials" - "github.com/kelseyhightower/confd/vendor/github.com/aws/aws-sdk-go/aws/credentials/stscreds" - "github.com/kelseyhightower/confd/vendor/github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/credentials/stscreds" + "github.com/aws/aws-sdk-go/aws/session" ) type AwsCredentials struct { diff --git a/plugins/inputs/cloudwatch/cloudwatch.go b/plugins/inputs/cloudwatch/cloudwatch.go index 509457ecd..8530ee656 100644 --- a/plugins/inputs/cloudwatch/cloudwatch.go +++ b/plugins/inputs/cloudwatch/cloudwatch.go @@ -110,20 +110,40 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error { if c.Metrics != nil { metrics = []*cloudwatch.Metric{} for _, m := range c.Metrics { - dimensions := make([]*cloudwatch.Dimension, len(m.Dimensions)) - for k, d := range m.Dimensions { - dimensions[k] = &cloudwatch.Dimension{ - Name: aws.String(d.Name), - Value: aws.String(d.Value), + if !hasWilcard(m.Dimensions) { + dimensions := make([]*cloudwatch.Dimension, len(m.Dimensions)) + for k, d := range m.Dimensions { + fmt.Printf("Dimension [%s]:[%s]\n", d.Name, d.Value) + dimensions[k] = &cloudwatch.Dimension{ + Name: aws.String(d.Name), + Value: aws.String(d.Value), + } + } + for _, name := range m.MetricNames { + metrics = append(metrics, &cloudwatch.Metric{ + Namespace: aws.String(c.Namespace), + MetricName: aws.String(name), + Dimensions: dimensions, + }) + } + } else { + allMetrics, err := c.fetchNamespaceMetrics() + if err != nil { + return err + } + for _, name := range m.MetricNames { + for _, metric := range allMetrics { + if isSelected(metric, m.Dimensions) { + metrics = append(metrics, &cloudwatch.Metric{ + Namespace: aws.String(c.Namespace), + MetricName: aws.String(name), + Dimensions: metric.Dimensions, + }) + } + } } } - for _, name := range m.MetricNames { - metrics = append(metrics, &cloudwatch.Metric{ - Namespace: aws.String(c.Namespace), - MetricName: aws.String(name), - Dimensions: dimensions, - }) - } + } } else { var err error @@ -310,3 +330,33 @@ func (c *CloudWatch) getStatisticsInput(metric *cloudwatch.Metric, now time.Time func (c *MetricCache) IsValid() bool { return c.Metrics != nil && time.Since(c.Fetched) < c.TTL } + +func hasWilcard(dimensions []*Dimension) bool { + wildcard := false + for _, d := range dimensions { + if d.Value == "" || d.Value == "*" { + wildcard = true + } + } + return wildcard +} + +func isSelected(metric *cloudwatch.Metric, dimensions []*Dimension) bool { + if len(metric.Dimensions) != len(dimensions) { + return false + } + for _, d := range dimensions { + selected := false + for _, d2 := range metric.Dimensions { + if d.Name == *d2.Name { + if d.Value == "" || d.Value == "*" || d.Value == *d2.Value { + selected = true + } + } + } + if !selected { + return false + } + } + return true +}