Skip to content

Commit

Permalink
Check 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 58101e3 commit 0db3337
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 49 deletions.
30 changes: 16 additions & 14 deletions test/e2e/jmx/jmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,24 @@ func testTomcatSessions(t *testing.T) {
}
}

time.Sleep(10 * time.Minute)
time.Sleep(5 * time.Minute)

startTime := time.Now().Add(-15 * time.Minute)
startTime := time.Now().Add(-5 * time.Minute)
endTime := time.Now()

hasActiveSessions := awsservice.ValidateSampleCountFloat(
maxSessions, err := awsservice.GetMetricMaximum(
"tomcat.sessions",
"JVM_TOMCAT_E2E",
nil,
startTime,
endTime,
0.01,
1000,
60,
)
if err != nil {
t.Errorf("Failed to get metric maximum: %v", err)
return
}

if !hasActiveSessions {
if maxSessions == 0 {
t.Error("Expected non-zero tomcat.sessions after applying traffic")
}
})
Expand Down Expand Up @@ -359,23 +360,24 @@ func testTomcatRejectedSessions(t *testing.T) {
}
}

time.Sleep(10 * time.Minute)
time.Sleep(5 * time.Minute)

startTime := time.Now().Add(-15 * time.Minute)
startTime := time.Now().Add(-5 * time.Minute)
endTime := time.Now()

hasActiveSessions := awsservice.ValidateSampleCountFloat(
maxRejectedSessions, err := awsservice.GetMetricMaximum(
"catalina_manager_rejectedsessions",
"ContainerInsights/Prometheus",
nil,
startTime,
endTime,
0.01,
1000,
60,
)
if err != nil {
t.Errorf("Failed to get metric maximum: %v", err)
return
}

if !hasActiveSessions {
if maxRejectedSessions == 0 {
t.Error("Expected non-zero catalina_manager_rejectedsessions after applying traffic")
}
})
Expand Down
97 changes: 62 additions & 35 deletions util/awsservice/cloudwatchmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,6 @@ func ValidateSampleCount(metricName, namespace string, dimensions []types.Dimens
return false
}

func ValidateSampleCountFloat(metricName, namespace string, dimensions []types.Dimension,
startTime time.Time, endTime time.Time,
lowerBoundInclusive float64, upperBoundInclusive float64, periodInSeconds int32) bool {

metricStatsInput := cloudwatch.GetMetricStatisticsInput{
MetricName: aws.String(metricName),
Namespace: aws.String(namespace),
StartTime: aws.Time(startTime),
EndTime: aws.Time(endTime),
Period: aws.Int32(periodInSeconds),
Dimensions: dimensions,
Statistics: []types.Statistic{types.StatisticSampleCount},
}
data, err := CwmClient.GetMetricStatistics(ctx, &metricStatsInput)
if err != nil {
return false
}

var dataPoints float64
log.Printf("These are the data points: %v", data)
log.Printf("These are the data points: %v", data.Datapoints)

for _, datapoint := range data.Datapoints {
dataPoints += *datapoint.SampleCount
}
log.Printf("Number of datapoints for start time %v with endtime %v and period %d is %.3f is inclusive between %.3f and %.3f",
startTime, endTime, periodInSeconds, dataPoints, lowerBoundInclusive, upperBoundInclusive)

if lowerBoundInclusive <= dataPoints && dataPoints <= upperBoundInclusive {
return true
}

return false
}

func GetMetricStatistics(
metricName string,
namespace string,
Expand Down Expand Up @@ -169,6 +134,68 @@ func GetMetricStatistics(
return CwmClient.GetMetricStatistics(ctx, &metricStatsInput)
}

func GetMetricMaximum(
metricName string,
namespace string,
startTime time.Time,
endTime time.Time,
periodInSeconds int32,
) (float64, error) {
// List metrics without any dimension filter to see what's available
listMetricsInput := cloudwatch.ListMetricsInput{
MetricName: aws.String(metricName),
Namespace: aws.String(namespace),
RecentlyActive: "PT3H",
}

metrics, err := CwmClient.ListMetrics(ctx, &listMetricsInput)
if err != nil {
return 0, err
}
if len(metrics.Metrics) == 0 {
return 0, fmt.Errorf("no metrics found for %s", metricName)
}

// Log all available metrics and their dimensions
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)
}
}

// 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)
}

maxValue := float64(0)
for _, datapoint := range data.Datapoints {
if *datapoint.Maximum > maxValue {
maxValue = *datapoint.Maximum
}
}
log.Printf("Maximum value found: %v", maxValue)

return maxValue, nil
}

// GetMetricData takes the metric name, metric dimension and metric namespace and return the query metrics
func GetMetricData(metricDataQueries []types.MetricDataQuery, startTime, endTime time.Time) (*cloudwatch.GetMetricDataOutput, error) {
getMetricDataInput := cloudwatch.GetMetricDataInput{
Expand Down

0 comments on commit 0db3337

Please sign in to comment.