Skip to content

Commit

Permalink
Improve logic to check nodes being ran.
Browse files Browse the repository at this point in the history
  • Loading branch information
musa-asad committed Dec 17, 2024
1 parent 9591142 commit 6fcde7e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
13 changes: 13 additions & 0 deletions test/e2e/jmx/jmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var testRegistry = map[string][]func(*testing.T){
},
}

var nodeNames []string

func TestMain(m *testing.M) {
flag.Parse()
if flag.Lookup("test.run").Value.String() == "NO_MATCH" {
Expand Down Expand Up @@ -139,6 +141,15 @@ func TestResources(t *testing.T) {
t.Fatalf("Error creating Kubernetes client: %v", err)
}

nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
t.Fatalf("Error listing nodes: %v", err)
}

for _, node := range nodes.Items {
nodeNames = append(nodeNames, node.Name)
}

daemonSet, err := clientset.AppsV1().DaemonSets("amazon-cloudwatch").Get(context.TODO(), "cloudwatch-agent", metav1.GetOptions{})
if err != nil {
t.Errorf("Error getting CloudWatch Agent DaemonSet: %v", err)
Expand Down Expand Up @@ -263,6 +274,7 @@ func testTomcatSessions(t *testing.T) {
startTime,
endTime,
60,
nodeNames,
)
if err != nil {
t.Errorf("Failed to get metric maximum: %v", err)
Expand Down Expand Up @@ -365,6 +377,7 @@ func testTomcatRejectedSessions(t *testing.T) {
startTime,
endTime,
60,
nodeNames,
)
if err != nil {
t.Errorf("Failed to get metric maximum: %v", err)
Expand Down
57 changes: 36 additions & 21 deletions util/awsservice/cloudwatchmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func GetMetricMaximum(
startTime time.Time,
endTime time.Time,
periodInSeconds int32,
nodeNames []string,
) (float64, error) {
// List metrics without any dimension filter to see what's available
listMetricsInput := cloudwatch.ListMetricsInput{
MetricName: aws.String(metricName),
Namespace: aws.String(namespace),
Expand All @@ -157,28 +157,43 @@ func GetMetricMaximum(
}

maxValue := float64(0)
// Iterate through all metrics found
for _, metric := range metrics.Metrics {
log.Printf("Found metric: %s", *metric.MetricName)
data, err := GetMetricStatistics(
metricName,
namespace,
metric.Dimensions,
startTime,
endTime,
periodInSeconds,
[]types.Statistic{types.StatisticMaximum},
nil,
)
if err != nil {
log.Printf("Error getting statistics for metric with dimensions %v: %v", metric.Dimensions, err)
continue // Continue with next metric if this one fails
var nodeNameMatch bool
var nodeName string
for _, dim := range metric.Dimensions {
if *dim.Name == "k8s.node.name" {
nodeName = *dim.Value
for _, name := range nodeNames {
if nodeName == name {
nodeNameMatch = true
break
}
}
break
}
}

// Check datapoints for this metric
for _, datapoint := range data.Datapoints {
if *datapoint.Maximum > maxValue {
maxValue = *datapoint.Maximum
if nodeNameMatch {
log.Printf("Found metric: %s for node: %s", *metric.MetricName, nodeName)
data, err := GetMetricStatistics(
metricName,
namespace,
metric.Dimensions,
startTime,
endTime,
periodInSeconds,
[]types.Statistic{types.StatisticMaximum},
nil,
)
if err != nil {
log.Printf("Error getting statistics for metric with dimensions %v: %v", metric.Dimensions, err)
continue
}

for _, datapoint := range data.Datapoints {
if *datapoint.Maximum > maxValue {
maxValue = *datapoint.Maximum
}
}
}
}
Expand All @@ -187,7 +202,7 @@ func GetMetricMaximum(
return 0, fmt.Errorf("no valid datapoints found for metric %s", metricName)
}

log.Printf("Maximum value found across all metrics: %v", maxValue)
log.Printf("Maximum value found across specified nodes: %v", maxValue)
return maxValue, nil
}

Expand Down

0 comments on commit 6fcde7e

Please sign in to comment.