Skip to content

Commit

Permalink
[CloudWatch] Refactor getMetrics in validation
Browse files Browse the repository at this point in the history
  • Loading branch information
avano authored and mmuzikar committed Feb 10, 2023
1 parent 2372883 commit ceb0126
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -28,56 +23,32 @@ public CloudwatchValidation(CloudWatchClient client) {
this.client = client;
}

public List<MetricDataResult> getMetrics(String namespace, String metricName, Instant start) {
LOG.debug("Fetching Couldwatch metrics {} from namespace {}", metricName, namespace);
List<MetricDataResult> 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<MetricDataQuery> 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<MetricDataResult> getMetrics(Consumer<MetricsRequest.MetricsRequestBuilder> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit ceb0126

Please sign in to comment.