Allow for wildcard in dimension values to select all available metrics.

This commit is contained in:
John Engelman 2016-05-05 09:05:36 -05:00
parent eac7f23609
commit c12b89c8ca
2 changed files with 65 additions and 15 deletions

View File

@ -3,9 +3,9 @@ package aws
import ( import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client" "github.com/aws/aws-sdk-go/aws/client"
"github.com/awslabs/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/kelseyhightower/confd/vendor/github.com/aws/aws-sdk-go/aws/credentials/stscreds" "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/session"
) )
type AwsCredentials struct { type AwsCredentials struct {

View File

@ -110,8 +110,10 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
if c.Metrics != nil { if c.Metrics != nil {
metrics = []*cloudwatch.Metric{} metrics = []*cloudwatch.Metric{}
for _, m := range c.Metrics { for _, m := range c.Metrics {
if !hasWilcard(m.Dimensions) {
dimensions := make([]*cloudwatch.Dimension, len(m.Dimensions)) dimensions := make([]*cloudwatch.Dimension, len(m.Dimensions))
for k, d := range m.Dimensions { for k, d := range m.Dimensions {
fmt.Printf("Dimension [%s]:[%s]\n", d.Name, d.Value)
dimensions[k] = &cloudwatch.Dimension{ dimensions[k] = &cloudwatch.Dimension{
Name: aws.String(d.Name), Name: aws.String(d.Name),
Value: aws.String(d.Value), Value: aws.String(d.Value),
@ -124,6 +126,24 @@ func (c *CloudWatch) Gather(acc telegraf.Accumulator) error {
Dimensions: dimensions, 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,
})
}
}
}
}
} }
} else { } else {
var err error var err error
@ -310,3 +330,33 @@ func (c *CloudWatch) getStatisticsInput(metric *cloudwatch.Metric, now time.Time
func (c *MetricCache) IsValid() bool { func (c *MetricCache) IsValid() bool {
return c.Metrics != nil && time.Since(c.Fetched) < c.TTL 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
}