Skip to content

Commit

Permalink
Initial commit, Add base for testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
vasireddy99 committed Oct 20, 2023
1 parent 5a0284a commit 965ec7b
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
30 changes: 30 additions & 0 deletions terraform/testcases/otlp_logs/otconfig.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extensions:
pprof:
endpoint: 0.0.0.0:1777
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:${grpc_port}

processors:
batch:

exporters:
logging:
verbosity: detailed
awscloudwatchlogs:
log_group_name: "otlp-receiver"
log_stream_name: "otlp-logs"
region: ${region}

service:
pipelines:
metrics:
receivers: [otlp]
processors: [batch]
exporters: [logging, awscloudwatchlogs]
extensions: [pprof]
telemetry:
logs:
level: ${log_level}
5 changes: 5 additions & 0 deletions terraform/testcases/otlp_logs/parameters.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
validation_config = "spark-otel-log-validation.yml"

sample_app = "spark"

sample_app_image = "public.ecr.aws/aws-otel-test/aws-otel-java-spark:latest"
1 change: 1 addition & 0 deletions validator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ dependencies {

// k8s client
implementation "io.kubernetes:client-java-extended:18.0.1"
testImplementation("com.github.rholder:guava-retrying:2.0.0")
}

application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public enum PredefinedExpectedTemplate implements FileConfig {
CONTAINER_INSIGHT_ECS_LOG("/expected-data-template/container-insight/ecs/ecs-instance"),
CONTAINER_INSIGHT_ECS_PROMETHEUS_LOG("/expected-data-template/container-insight/ecs/prometheus"),
CONTAINER_INSIGHT_FARGATE_EKS_LOG("/expected-data-template/container-insight/eks/fargate"),
DEFAULT_EXPECTED_LOG("/expected-data-template/otlpExpectedLog.mustache"),
;

private String path;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.amazon.aoc.validators;

import com.amazon.aoc.callers.ICaller;
import com.amazon.aoc.fileconfigs.FileConfig;
import com.amazon.aoc.models.Context;
import com.amazon.aoc.models.ValidationConfig;
import com.amazonaws.services.logs.CloudWatchLogsClient;
import com.amazonaws.services.logs.model.GetLogEventsRequest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.awaitility.core.RetryerBuilder;
import org.awaitility.core.StopStrategies;
import org.awaitility.core.WaitStrategies;
import org.opentest4j.AssertionFailedError;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.Duration;
import java.time.Instant;
import java.util.HashSet;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class CWLogValidator implements IValidator {



// String getJsonSchemaMappingKey(JsonNode jsonNode) {
// // Your implementation for getting the JSON schema mapping key
// return null;
// }

@Override
public void init(Context context, ValidationConfig validationConfig, ICaller caller, FileConfig expectedDataTemplate) throws Exception {

}

@Override
public void validate() throws Exception {
var lines = new HashSet<String>();
InputStream inputStream = getClass().getResourceAsStream("/logs/testingJSON.log");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
throw new RuntimeException("Error reading from the file: " + inputStream, e);
}

var cwClient = CloudWatchLogsClient.builder().build();
var objectMapper = new ObjectMapper();

RetryerBuilder.<Void>newBuilder()
.retryIfException()
.retryIfRuntimeException()
.retryIfExceptionOfType(AssertionFailedError.class)
.withWaitStrategy(WaitStrategies.fixedWait(10, TimeUnit.SECONDS))
.withStopStrategy(StopStrategies.stopAfterAttempt(5))
.build()
.call(() -> {
var now = Instant.now();
var start = now.minus(Duration.ofMinutes(2));
var end = now.plus(Duration.ofMinutes(2));
var response = cwClient.getLogEvents(GetLogEventsRequest.builder()
.logGroupName("adot-testbed/logs-component-testing/logs")
.logStreamName(testLogStreamName)
.startTime(start.toEpochMilli())
.endTime(end.toEpochMilli())
.build());

var events = response.events();
var receivedMessages = events.stream().map(x -> x.message()).collect(Collectors.toSet());

// Extract the "body" field from each received message that is received from CloudWatch in JSON Format
var messageToValidate = receivedMessages.stream()
.map(message -> {
try {
JsonNode jsonNode = objectMapper.readTree(message);
return jsonNode.get("body").asText();
} catch (Exception e) {
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());

// Validate the body field in JSON-messageToValidate with actual log lines from the log file.
assertThat(messageToValidate.containsAll(lines)).isTrue();
assertThat(messageToValidate).containsExactlyInAnyOrderElementsOf(lines);
return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public IValidator launchValidator(ValidationConfig validationConfig) throws Exce
validator = new LoadBalancingValidator();
break;
case "cw-metric":
validator = new CWLogValidator();
expectedData = validationConfig.getExpectedMetricTemplate();
break;
case "cw-logs":
validator = new CWMetricValidator();
expectedData = validationConfig.getExpectedMetricTemplate();
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-
validationType: "cw-logs"
httpPath: "/outgoing-http-call"
httpMethod: "get"
callingType: "http"
expectedMetricTemplate: "DEFAULT_EXPECTED_LOG"

0 comments on commit 965ec7b

Please sign in to comment.