From ceb012662f37ff990c4b2ca9d42f9f3bdf1869af Mon Sep 17 00:00:00 2001 From: Andrej Vano Date: Thu, 9 Feb 2023 14:12:47 +0100 Subject: [PATCH] [CloudWatch] Refactor getMetrics in validation --- .../validation/CloudwatchValidation.java | 79 ++++------- .../validation/model/MetricsRequest.java | 125 ++++++++++++++++++ .../aws/cloudwatch/validation/model/Stat.java | 21 +++ 3 files changed, 171 insertions(+), 54 deletions(-) create mode 100644 system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/model/MetricsRequest.java create mode 100644 system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/model/Stat.java diff --git a/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/CloudwatchValidation.java b/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/CloudwatchValidation.java index b6c707ef9..ff2415ba5 100644 --- a/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/CloudwatchValidation.java +++ b/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/CloudwatchValidation.java @@ -1,23 +1,18 @@ package software.tnb.aws.cloudwatch.validation; +import software.tnb.aws.cloudwatch.validation.model.MetricsRequest; import software.tnb.common.service.Validation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.time.Instant; -import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; -import software.amazon.awssdk.services.cloudwatch.model.GetMetricDataRequest; import software.amazon.awssdk.services.cloudwatch.model.GetMetricDataResponse; -import software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest; import software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse; -import software.amazon.awssdk.services.cloudwatch.model.Metric; -import software.amazon.awssdk.services.cloudwatch.model.MetricDataQuery; import software.amazon.awssdk.services.cloudwatch.model.MetricDataResult; -import software.amazon.awssdk.services.cloudwatch.model.MetricStat; public class CloudwatchValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(CloudwatchValidation.class); @@ -28,56 +23,32 @@ public CloudwatchValidation(CloudWatchClient client) { this.client = client; } - public List getMetrics(String namespace, String metricName, Instant start) { - LOG.debug("Fetching Couldwatch metrics {} from namespace {}", metricName, namespace); - List data = new ArrayList<>(); - Instant endDate = Instant.now(); - - Metric met = Metric.builder() - .metricName(metricName) - .namespace(namespace) - .build(); - - MetricStat metStat = MetricStat.builder() - .stat("Minimum") - .period(60) - .metric(met) - .build(); - - MetricDataQuery dataQUery = MetricDataQuery.builder() - .metricStat(metStat) - .id("foo2") - .returnData(true) - .build(); - - List dq = new ArrayList<>(); - dq.add(dataQUery); - - GetMetricDataRequest getMetReq = GetMetricDataRequest.builder() - .maxDatapoints(100) - .startTime(start) - .endTime(endDate) - .metricDataQueries(dq) - .build(); - - GetMetricDataResponse response = client.getMetricData(getMetReq); - - if (response.hasMetricDataResults()) { - data = response.metricDataResults(); - } - return data; + public List getMetrics(Consumer requestBuilder) { + MetricsRequest.MetricsRequestBuilder builder = new MetricsRequest.MetricsRequestBuilder(); + requestBuilder.accept(builder); + MetricsRequest request = builder.build(); + LOG.debug("Fetching Couldwatch metrics {} from namespace {}", request.metricName(), request.namespace()); + + GetMetricDataResponse response = client.getMetricData(b -> b + .maxDatapoints(request.maxDataPoints()) + .startTime(request.start()) + .endTime(request.end()) + .metricDataQueries(dq -> dq + .id(request.queryId()) + .returnData(true) + .metricStat(ms -> ms + .stat(request.stat()) + .period(request.period()) + .metric(m -> m.metricName(request.metricName()).namespace(request.namespace())) + ) + ) + ); + + return response.hasMetricDataResults() ? response.metricDataResults() : List.of(); } public ListMetricsResponse listMetrics(String namespace, String metricName) { LOG.debug("Fetching Couldwatch metric {} from namespace {}", metricName, namespace); - - ListMetricsRequest request = ListMetricsRequest.builder() - .metricName(metricName) - .namespace(namespace) - .build(); - - ListMetricsResponse response = client.listMetrics(request); - - return response; + return client.listMetrics(b -> b.metricName(metricName).namespace(namespace)); } } diff --git a/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/model/MetricsRequest.java b/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/model/MetricsRequest.java new file mode 100644 index 000000000..98f8e489e --- /dev/null +++ b/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/model/MetricsRequest.java @@ -0,0 +1,125 @@ +package software.tnb.aws.cloudwatch.validation.model; + +import org.apache.commons.lang3.RandomStringUtils; + +import java.time.Instant; + +public class MetricsRequest { + private String metricName; + private String namespace; + private String stat; + private int maxDataPoints; + private int period; + private Instant start; + private Instant end; + private String queryId; + + public String metricName() { + return metricName; + } + + public String namespace() { + return namespace; + } + + public String stat() { + return stat; + } + + public int maxDataPoints() { + return maxDataPoints; + } + + public int period() { + return period; + } + + public Instant start() { + return start; + } + + public Instant end() { + return end; + } + + public String queryId() { + return queryId; + } + + public static final class MetricsRequestBuilder { + private String metricName; + private String namespace; + private String stat; + private int maxDataPoints = 100; + private int period = 60; + private Instant start; + private Instant end = Instant.now(); + private String queryId = RandomStringUtils.randomAlphabetic(8).toLowerCase(); + + public MetricsRequestBuilder() { + } + + public MetricsRequestBuilder metricName(String metricName) { + this.metricName = metricName; + return this; + } + + public MetricsRequestBuilder namespace(String namespace) { + this.namespace = namespace; + return this; + } + + public MetricsRequestBuilder stat(Stat stat) { + this.stat = stat.value(); + return this; + } + + public MetricsRequestBuilder stat(String stat) { + this.stat = stat; + return this; + } + + public MetricsRequestBuilder maxDataPoints(int maxDataPoints) { + this.maxDataPoints = maxDataPoints; + return this; + } + + public MetricsRequestBuilder period(int period) { + this.period = period; + return this; + } + + public MetricsRequestBuilder start(Instant start) { + this.start = start; + return this; + } + + public MetricsRequestBuilder end(Instant end) { + this.end = end; + return this; + } + + public MetricsRequestBuilder queryId(String queryId) { + this.queryId = queryId; + return this; + } + + public MetricsRequest build() { + if (metricName == null || namespace == null || stat == null || start == null) { + throw new IllegalArgumentException( + "At least one required parameter missing. Required parameters are metricName, namespace, stat, start"); + } + + MetricsRequest metricsRequest = new MetricsRequest(); + metricsRequest.maxDataPoints = this.maxDataPoints; + metricsRequest.start = this.start; + metricsRequest.metricName = this.metricName; + metricsRequest.end = this.end; + metricsRequest.namespace = this.namespace; + metricsRequest.stat = this.stat; + metricsRequest.queryId = this.queryId; + metricsRequest.period = this.period; + return metricsRequest; + } + } +} diff --git a/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/model/Stat.java b/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/model/Stat.java new file mode 100644 index 000000000..51e848e2e --- /dev/null +++ b/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/validation/model/Stat.java @@ -0,0 +1,21 @@ +package software.tnb.aws.cloudwatch.validation.model; + +public enum Stat { + AVERAGE("Average"), + MINIMUM("Minimum"), + MAXIMUM("Maximum"), + SUM("Sum"), + SAMPLE_COUNT("SampleCount"), + IQM("IQM"); + // Others like percentile, trimmed mean, etc. require a value, so those can be supplied via a plain string for now + + private final String value; + + Stat(String value) { + this.value = value; + } + + public String value() { + return value; + } +}