Skip to content

Commit

Permalink
undo validator refactor for simpler usage, update validator again
Browse files Browse the repository at this point in the history
  • Loading branch information
jj22ee committed May 6, 2024
1 parent ce34df7 commit 8a6086e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.amazonaws.services.logs.model.OutputLogEvent;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import kotlin.Pair;
import lombok.extern.log4j.Log4j2;

/** a wrapper of cloudwatch client. */
Expand Down Expand Up @@ -65,20 +63,15 @@ public CloudWatchService(String region) {
public List<Metric> listMetrics(
final String namespace,
final String metricName,
final List<Pair<String, String>> dimensionList) {
final List<DimensionFilter> dimensionFilters =
dimensionList.stream()
.map(
dimension ->
new DimensionFilter()
.withName(dimension.getFirst())
.withValue(dimension.getSecond()))
.collect(Collectors.toList());
final String dimensionKey,
final String dimensionValue) {
final DimensionFilter dimensionFilter =
new DimensionFilter().withName(dimensionKey).withValue(dimensionValue);
final ListMetricsRequest listMetricsRequest =
new ListMetricsRequest()
.withNamespace(namespace)
.withMetricName(metricName)
.withDimensions(dimensionFilters)
.withDimensions(dimensionFilter)
.withRecentlyActive("PT3H");
return amazonCloudWatch.listMetrics(listMetricsRequest).getMetrics();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import kotlin.Pair;
import lombok.extern.log4j.Log4j2;

@Log4j2
Expand Down Expand Up @@ -92,46 +90,37 @@ public void validate() throws Exception {
RetryHelper.retry(
maxRetryCount,
() -> {
// We will query metrics that include Service, RemoteService, or RemoteTarget dimensions
// to ensure we get all metrics from all aggregations, specifically the [RemoteService] aggregation.
// We will query the Service, RemoteService, and RemoteTarget dimensions to ensure we
// get all metrics from all aggregations, specifically the [RemoteService] aggregation.
List<String> serviceNames =
Lists.newArrayList(
context.getServiceName(), context.getRemoteServiceDeploymentName());
List<String> remoteServiceNames =
Lists.newArrayList(context.getRemoteServiceDeploymentName());
List<String> remoteTargetNames = Lists.newArrayList();
if (context.getRemoteServiceName() != null && !context.getRemoteServiceName().isEmpty()) {
serviceNames.add(context.getRemoteServiceName());
}

List<Metric> actualMetricList = Lists.newArrayList();

// Add sets of dimension filters to use for each query to CloudWatch.
List<List<Pair<String, String>>> dimensionLists = Lists.newArrayList();
// Query metrics that includes any of these <service> values.
for (String serviceName : serviceNames) {
dimensionLists.add(
Arrays.asList(new Pair<>(CloudWatchService.SERVICE_DIMENSION, serviceName)));
}
// Query metrics that includes any of these <remoteService> values.
for (String remoteServiceName : remoteServiceNames) {
dimensionLists.add(
Arrays.asList(
new Pair<>(CloudWatchService.REMOTE_SERVICE_DIMENSION, remoteServiceName)));
}
// Query for metrics that includes both of these <remoteService, remoteTarget> values.
// Querying just 'remoteService="AWS.SDK.S3"' would also work, but that can result in
// returning too many canary metrics and may cause issues.
if (context.getTestingId() != null && !context.getTestingId().isEmpty()) {
dimensionLists.add(
Arrays.asList(
new Pair<>(CloudWatchService.REMOTE_SERVICE_DIMENSION, "AWS.SDK.S3"),
new Pair<>(CloudWatchService.REMOTE_TARGET_DIMENSION, "::s3:::e2e-test-bucket-name-" + context.getTestingId())));
remoteTargetNames.add("::s3:::e2e-test-bucket-name-" + context.getTestingId());
}

// Populate actualMetricList with metrics that pass through at least one of the dimension filters
for (List<Pair<String, String>> dimensionList : dimensionLists) {
addMetrics(dimensionList, expectedMetricList, actualMetricList);
}
List<Metric> actualMetricList = Lists.newArrayList();
addMetrics(
CloudWatchService.SERVICE_DIMENSION,
serviceNames,
expectedMetricList,
actualMetricList);
addMetrics(
CloudWatchService.REMOTE_SERVICE_DIMENSION,
remoteServiceNames,
expectedMetricList,
actualMetricList);
addMetrics(
CloudWatchService.REMOTE_TARGET_DIMENSION,
remoteTargetNames,
expectedMetricList,
actualMetricList);

// remove the skip dimensions
log.info("dimensions to be skipped in validation: {}", skippedDimensionNameList);
Expand All @@ -151,12 +140,16 @@ public void validate() throws Exception {
}

private void addMetrics(
List<Pair<String, String>> dimensionList,
String dimensionName,
List<String> dimensionValues,
List<Metric> expectedMetricList,
List<Metric> actualMetricList)
throws Exception {
actualMetricList.addAll(
this.listMetricFromCloudWatch(cloudWatchService, expectedMetricList, dimensionList));
for (String dimensionValue : dimensionValues) {
actualMetricList.addAll(
this.listMetricFromCloudWatch(
cloudWatchService, expectedMetricList, dimensionName, dimensionValue));
}
}

/**
Expand Down Expand Up @@ -209,7 +202,8 @@ private void compareMetricLists(List<Metric> toBeCheckedMetricList, List<Metric>
private List<Metric> listMetricFromCloudWatch(
CloudWatchService cloudWatchService,
List<Metric> expectedMetricList,
List<Pair<String, String>> dimensionList)
String dimensionKey,
String dimensionValue)
throws IOException {
// put namespace into the map key, so that we can use it to search metric
HashMap<String, String> metricNameMap = new HashMap<>();
Expand All @@ -221,7 +215,8 @@ private List<Metric> listMetricFromCloudWatch(
List<Metric> result = new ArrayList<>();
for (String metricName : metricNameMap.keySet()) {
result.addAll(
cloudWatchService.listMetrics(metricNameMap.get(metricName), metricName, dimensionList));
cloudWatchService.listMetrics(
metricNameMap.get(metricName), metricName, dimensionKey, dimensionValue));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import com.amazon.aoc.models.ValidationConfig;
import com.amazon.aoc.services.CloudWatchService;
import com.amazonaws.services.cloudwatch.model.Metric;
import java.util.Arrays;
import java.util.List;
import kotlin.Pair;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -188,44 +186,22 @@ private CloudWatchService mockCloudWatchService(
List<Metric> remoteMetricsWithAwsSdk,
List<Metric> remoteMetricsWithS3Target) {
CloudWatchService cloudWatchService = mock(CloudWatchService.class);
Lists.newArrayList();
when(cloudWatchService.listMetrics(
any(),
any(),
eq(Arrays.asList(new Pair<String, String>(SERVICE_DIMENSION, SERVICE_NAME)))))
when(cloudWatchService.listMetrics(any(), any(), eq(SERVICE_DIMENSION), eq(SERVICE_NAME)))
.thenReturn(localServiceMetrics);
when(cloudWatchService.listMetrics(
any(),
any(),
eq(Arrays.asList(new Pair<String, String>(SERVICE_DIMENSION, REMOTE_SERVICE_NAME)))))
any(), any(), eq(SERVICE_DIMENSION), eq(REMOTE_SERVICE_NAME)))
.thenReturn(remoteServiceMetrics);
when(cloudWatchService.listMetrics(
any(),
any(),
eq(
Arrays.asList(
new Pair<String, String>(
REMOTE_SERVICE_DIMENSION, REMOTE_SERVICE_DEPLOYMENT_NAME)))))
any(), any(), eq(REMOTE_SERVICE_DIMENSION), eq(REMOTE_SERVICE_DEPLOYMENT_NAME)))
.thenReturn(remoteMetricsWithRemoteApp);
when(cloudWatchService.listMetrics(
any(),
any(),
eq(
Arrays.asList(
new Pair<String, String>(REMOTE_SERVICE_DIMENSION, "www.amazon.com")))))
any(), any(), eq(REMOTE_SERVICE_DIMENSION), eq("www.amazon.com")))
.thenReturn(remoteMetricsWithAmazon);
when(cloudWatchService.listMetrics(
any(),
any(),
eq(Arrays.asList(new Pair<String, String>(REMOTE_SERVICE_DIMENSION, "AWS.SDK.S3")))))
any(), any(), eq(REMOTE_SERVICE_DIMENSION), eq("AWS.SDK.S3")))
.thenReturn(remoteMetricsWithAwsSdk);
when(cloudWatchService.listMetrics(
any(),
any(),
eq(
Arrays.asList(
new Pair<String, String>(REMOTE_SERVICE_DIMENSION, "AWS.SDK.S3"),
new Pair<String, String>(REMOTE_TARGET_DIMENSION, "::s3:::e2e-test-bucket-name-" + context.getTestingId())))))
any(), any(), eq(REMOTE_TARGET_DIMENSION), eq("::s3:::e2e-test-bucket-name-" + context.getTestingId())))
.thenReturn(remoteMetricsWithS3Target);
return cloudWatchService;
}
Expand All @@ -248,7 +224,7 @@ private void runBasicValidation(ValidationConfig validationConfig) throws Except
CloudWatchService cloudWatchService = mock(CloudWatchService.class);

// mock listMetrics
when(cloudWatchService.listMetrics(any(), any(), any())).thenReturn(metrics);
when(cloudWatchService.listMetrics(any(), any(), any(), any())).thenReturn(metrics);

// start validation
validate(validationConfig, cloudWatchService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@
value: AWS.SDK.S3
-
name: RemoteTarget
value: remoteTargetName
value: ::s3:::e2e-test-bucket-name-testIdentifier
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
value: AWS.SDK.S3
-
name: RemoteTarget
value: remoteTargetName
value: ::s3:::e2e-test-bucket-name-testIdentifier

0 comments on commit 8a6086e

Please sign in to comment.