Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudwatch: Update Namespace Metrics and Dimensions tests, add missing dimensions #218

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/cloudWatchConsts/metrics.go
Original file line number Diff line number Diff line change
@@ -3330,6 +3330,7 @@ var NamespaceDimensionKeysMap = map[string][]string{
"AWS/ElasticMapReduce": {"ClusterId", "JobFlowId", "JobId"},
"AWS/EMRServerless": {"ApplicationId", "WorkerType", "CapacityAllocationType"},
"AWS/ElasticTranscoder": {"Operation", "PipelineId"},
"AWS/EventBridge/Pipes": {},
"AWS/Events": {"EventBusName", "EventSourceName", "RuleName"},
"AWS/FSx": {"FileSystemId"},
"AWS/Firehose": {"DeliveryStreamName"},
@@ -3345,6 +3346,7 @@ var NamespaceDimensionKeysMap = map[string][]string{
"AWS/IoTSiteWise": {"AccountId", "GatewayId", "PropertyGroup", "Reason", "SourceName", "StreamName", "ThrottledAt"},
"AWS/KMS": {"KeyId"},
"AWS/Kafka": {"Broker ID", "Cluster Name", "Consumer Group", "Topic"},
"AWS/KafkaConnect": {},
"AWS/Kendra": {"DataSourceId", "IndexId"},
"AWS/Kinesis": {"ShardId", "StreamName"},
"AWS/KinesisAnalytics": {"Application", "Flow", "Id", "Operator", "Parallelism", "Task"},
48 changes: 42 additions & 6 deletions pkg/cloudWatchConsts/metrics_test.go
Original file line number Diff line number Diff line change
@@ -2,22 +2,48 @@ package cloudWatchConsts

import (
"fmt"
"maps"
"slices"
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

// test to check NamespaceMetricsMap is sorted alphabetically
func TestNamespaceMetricsMap(t *testing.T) {
// test to check NamespaceMetricsMap metrics are sorted alphabetically
func TestNamespaceMetricsAlphabetized(t *testing.T) {
unsortedMetricNamespaces := namespacesWithUnsortedMetrics(NamespaceMetricsMap)
if len(unsortedMetricNamespaces) != 0 {
assert.Fail(t, "NamespaceMetricsMap is not sorted alphabetically. Please replace the printed services")
printNamespacesThatNeedSorted(unsortedMetricNamespaces)
}
}

func TestNamespaceMetricKeysAllHaveDimensions(t *testing.T) {
namespaceMetricsKeys := slices.Collect(maps.Keys(NamespaceMetricsMap))
namespaceDimensionKeys := slices.Collect(maps.Keys(NamespaceDimensionKeysMap))

namespaceMetricsMissingKeys := findMetricKeysFromAMissingInB(namespaceDimensionKeys, namespaceMetricsKeys)

if len(namespaceMetricsMissingKeys) != 0 {
assert.Fail(t, "NamespaceMetricsMap is missing key(s) from NamespaceDimensionKeysMap.")
fmt.Println(strings.Join(namespaceMetricsMissingKeys, "\n"))
}
}

func TestNamespaceDimensionKeysAllHaveMetrics(t *testing.T) {
namespaceMetricsKeys := slices.Collect(maps.Keys(NamespaceMetricsMap))
namespaceDimensionKeys := slices.Collect(maps.Keys(NamespaceDimensionKeysMap))

namespaceDimensionMissingKeys := findMetricKeysFromAMissingInB(namespaceMetricsKeys, namespaceDimensionKeys)

if len(namespaceDimensionMissingKeys) != 0 {
assert.Fail(t, "NamespaceDimensionKeysMap is missing key(s) from NamespaceMetricsMap.")
fmt.Println(strings.Join(namespaceDimensionMissingKeys, "\n"))
}
}

func printNamespacesThatNeedSorted(unsortedMetricNamespaces []string) {
slices.Sort(unsortedMetricNamespaces)

@@ -37,10 +63,8 @@ func printNamespacesThatNeedSorted(unsortedMetricNamespaces []string) {
// namespacesWithUnsortedMetrics returns which namespaces have unsorted metrics
func namespacesWithUnsortedMetrics(NamespaceMetricsMap map[string][]string) []string {
// Extract keys from the map and sort them
keys := make([]string, 0, len(NamespaceMetricsMap))
for k := range NamespaceMetricsMap {
keys = append(keys, k)
}
keys := slices.Collect(maps.Keys(NamespaceMetricsMap))

sort.Strings(keys)

var unsortedNamespace []string
@@ -62,3 +86,15 @@ func namespacesWithUnsortedMetrics(NamespaceMetricsMap map[string][]string) []st

return unsortedNamespace
}

func findMetricKeysFromAMissingInB(a []string, b []string) []string {
var missingKeys []string

for i := range a {
if !slices.Contains(b, a[i]) {
missingKeys = append(missingKeys, a[i])
}
}

return missingKeys
}