Skip to content

Commit

Permalink
Update logic for max metric value.
Browse files Browse the repository at this point in the history
  • Loading branch information
musa-asad committed Dec 17, 2024
1 parent 123f8e5 commit 9591142
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions util/awsservice/cloudwatchmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9591142

Please sign in to comment.