From bc21adf712c979bb86ecbf30b30c97e77143f023 Mon Sep 17 00:00:00 2001 From: Matthew Coltman Date: Mon, 17 May 2021 07:32:32 +0200 Subject: [PATCH] CloudWatch: Allow use of missing AWS namespaces using custom metrics (#30961) * add tests * CloudWatch: Allow use of missing AWS namespaces using custom metrics * CloudWatch: Allow use of missing AWS namespaces using custom metrics --- pkg/tsdb/cloudwatch/metric_find_query.go | 5 ++- pkg/tsdb/cloudwatch/metric_find_query_test.go | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/tsdb/cloudwatch/metric_find_query.go b/pkg/tsdb/cloudwatch/metric_find_query.go index dbeb05a8f88..c851e3494b2 100644 --- a/pkg/tsdb/cloudwatch/metric_find_query.go +++ b/pkg/tsdb/cloudwatch/metric_find_query.go @@ -813,5 +813,8 @@ func isDuplicate(nameList []string, target string) bool { } func isCustomMetrics(namespace string) bool { - return strings.Index(namespace, "AWS/") != 0 + if _, ok := metricsMap[namespace]; ok { + return false + } + return true } diff --git a/pkg/tsdb/cloudwatch/metric_find_query_test.go b/pkg/tsdb/cloudwatch/metric_find_query_test.go index 01f4bd40484..d58312485d5 100644 --- a/pkg/tsdb/cloudwatch/metric_find_query_test.go +++ b/pkg/tsdb/cloudwatch/metric_find_query_test.go @@ -465,6 +465,49 @@ func TestQuery_ResourceARNs(t *testing.T) { }) } +func Test_isCustomMetrics(t *testing.T) { + metricsMap = map[string][]string{ + "AWS/EC2": {"ExampleMetric"}, + } + + type args struct { + namespace string + } + + tests := []struct { + name string + args args + want bool + }{ + {name: "A custom metric should return true", + want: true, + args: args{ + namespace: "Custom/MyApp", + }, + }, + {name: "An AWS metric not included in this package should return true", + want: true, + args: args{ + namespace: "AWS/MyApp", + }, + }, + {name: "An AWS metric included in this package should return false", + want: false, + args: args{ + namespace: "AWS/EC2", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isCustomMetrics(tt.args.namespace); got != tt.want { + t.Errorf("isCustomMetrics() = %v, want %v", got, tt.want) + } + }) + } +} + func TestQuery_ListMetricsPagination(t *testing.T) { origNewCWClient := NewCWClient t.Cleanup(func() {