diff --git a/util/awsservice/cloudwatchmetrics.go b/util/awsservice/cloudwatchmetrics.go index 0bc74fb97..0da0c7c29 100644 --- a/util/awsservice/cloudwatchmetrics.go +++ b/util/awsservice/cloudwatchmetrics.go @@ -156,43 +156,38 @@ func GetMetricMaximum( return 0, fmt.Errorf("no metrics found for %s", metricName) } - // Log all available metrics and their dimensions + maxValue := float64(0) + // Iterate through all metrics found for _, metric := range metrics.Metrics { log.Printf("Found metric: %s", *metric.MetricName) - for _, dim := range metric.Dimensions { - log.Printf(" Dimension: %s = %s", *dim.Name, *dim.Value) + 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 } - } - - // Use the dimensions from the first matching metric - dimensions := metrics.Metrics[0].Dimensions - - data, err := GetMetricStatistics( - metricName, - namespace, - dimensions, - startTime, - endTime, - periodInSeconds, - []types.Statistic{types.StatisticMaximum}, - nil, - ) - if err != nil { - return 0, err - } - if len(data.Datapoints) == 0 { - return 0, fmt.Errorf("no datapoints found for metric %s", metricName) + // Check datapoints for this metric + for _, datapoint := range data.Datapoints { + if *datapoint.Maximum > maxValue { + maxValue = *datapoint.Maximum + } + } } - maxValue := float64(0) - for _, datapoint := range data.Datapoints { - if *datapoint.Maximum > maxValue { - maxValue = *datapoint.Maximum - } + if maxValue == 0 { + return 0, fmt.Errorf("no valid datapoints found for metric %s", metricName) } - log.Printf("Maximum value found: %v", maxValue) + log.Printf("Maximum value found across all metrics: %v", maxValue) return maxValue, nil }