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() {