Skip to content

Commit

Permalink
cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielzolty committed Oct 26, 2023
1 parent b9ce051 commit a19a1a4
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 66 deletions.
2 changes: 0 additions & 2 deletions validator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ dependencies {
implementation 'com.amazonaws:aws-java-sdk-xray'
implementation 'com.amazonaws:aws-java-sdk-logs'
implementation 'com.amazonaws:aws-java-sdk-sts'
implementation group: 'javax.json', name: 'javax.json-api', version: '1.0-b01'

// aws ecs sdk
implementation 'com.amazonaws:aws-java-sdk-ecs'
Expand All @@ -74,7 +73,6 @@ dependencies {

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

application {
Expand Down
3 changes: 0 additions & 3 deletions validator/src/main/java/com/amazon/aoc/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,12 @@ public Integer call() throws Exception {
context.setLanguage(language);
context.setKubernetesContext(this.buildKubernetesContext());
log.info(context);
log.info("URL is: " + configPath);

// load config
List<ValidationConfig> validationConfigList =
new ConfigLoadHelper().loadConfigFromFile(configPath);
log.info("App - config was loaded");
// run validation
validate(context, validationConfigList);
log.info("App - validation completed");
Instant endTime = Instant.now();
Duration duration = Duration.between(startTime, endTime);
log.info("Validation has completed in {} minutes.", duration.toMinutes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@ public HttpCaller(String endpoint, String path) {

@Override
public SampleAppResponse callSampleApp() throws Exception {
log.info("Sample app was called");
OkHttpClient client = new OkHttpClient();
log.info("Sample app - OkHttpClient");
Request request = new Request.Builder().url(url).build();
log.info("Sample app - request builder");

AtomicReference<SampleAppResponse> sampleAppResponseAtomicReference = new AtomicReference<>();
log.info("Sample app - Atomic reference");
RetryHelper.retry(
15,
40,
() -> {
try (Response response = client.newCall(request).execute()) {
String responseBody = response.body().string();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ public String render(FileConfig fileConfig, Object dataToInject) throws IOExcept
private String render(URL path, Object dataToInject) throws IOException {
log.info("fetch config: {}", path);
String templateContent = IOUtils.toString(path);
log.info("Got template Content");
Mustache mustache = mustacheFactory.compile(new StringReader(templateContent), path.getPath());
log.info("Got mustache");
StringWriter stringWriter = new StringWriter();
log.info("fGot stringWriter");
mustache.execute(stringWriter, dataToInject).flush();
log.info("Mustache executed");
return stringWriter.getBuffer().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static boolean retry(
} catch (Exception ex) {
exceptionInTheEnd = ex;
if (retryCount != 0) { // don't sleep before leave this loop
log.info("Error that occured:" + ex.getMessage());
log.info(
"retrying after {} seconds", TimeUnit.MILLISECONDS.toSeconds(sleepInMilliSeconds));
TimeUnit.MILLISECONDS.sleep(sleepInMilliSeconds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,74 +46,35 @@ public void init(
ICaller caller,
FileConfig expectedDataTemplate)
throws Exception {
log.info("CWLog init starting");
this.context = context;
cloudWatchService = new CloudWatchService(context.getRegion());
logGroupName = String.format("otlp-receiver", context.getCloudWatchContext().getClusterName());
log.info("CW Log group name is: " + logGroupName);
// cloudWatchService = new CloudWatchService(context.getRegion());
MustacheHelper mustacheHelper = new MustacheHelper();
String templateInput = mustacheHelper.render(expectedDataTemplate, context);
log.info("Template input: " + templateInput);
JsonNode jsonNode = JsonLoader.fromString(templateInput);
log.info("Trying to get jsonNode string: " + jsonNode.textValue());
JsonSchemaFactory jsonSchemaFactory =
JsonSchemaFactory.newBuilder()
.setReportProvider(new ListReportProvider(LogLevel.INFO, LogLevel.FATAL))
.freeze();
JsonSchema jsonSchema = jsonSchemaFactory.getJsonSchema(jsonNode);
this.schema = jsonSchema;
log.info(("CWLog init ending"));
Map<String, Object> mapping = new ObjectMapper().readValue(templateInput, HashMap.class);
// ObjectMapper mapper = new ObjectMapper();
// ObjectNode jsonObject = mapper.readValue(templateInput, ObjectNode.class);
// Map<String, Object> result =
// mapper.convertValue(jsonObject, new TypeReference<Map<String, Object>>() {});
log.info("Mapper is:" + mapping.toString());
caller.callSampleApp();
}

// @Override
// public void init(
// Context context,
// ValidationConfig validationConfig,
// ICaller caller,
// FileConfig expectedDataTemplate)
// throws Exception {
// log.info("CWLog init starting");
// this.context = context;
// logGroupName = String.format("otlp-receiver",
// context.getCloudWatchContext().getClusterName());
// cloudWatchService = new CloudWatchService(context.getRegion());
// MustacheHelper mustacheHelper = new MustacheHelper();
// String templateInput = mustacheHelper.render(expectedDataTemplate, context);
// JsonNode jsonNode = JsonLoader.fromString(templateInput);
// JsonSchemaFactory jsonSchemaFactory =
// JsonSchemaFactory.newBuilder()
// .setReportProvider(new ListReportProvider(LogLevel.INFO, LogLevel.FATAL))
// .freeze();
// JsonSchema schema = jsonSchemaFactory.getJsonSchema(jsonNode);
// this.schema = schema;
// log.info(("CWLog init ending"));
// }

@Override
public void validate() throws Exception {
log.info(("CWLog validate starting"));
RetryHelper.retry(
getMaxRetryCount(),
CHECK_INTERVAL_IN_MILLI,
true,
() -> {
Instant startTime =
Instant.now().minusSeconds(CHECK_DURATION_IN_SECONDS).truncatedTo(ChronoUnit.MINUTES);
log.info("Start time is: " + startTime.toEpochMilli());
fetchAndValidateLogs(startTime);
});
}

protected void fetchAndValidateLogs(Instant startTime) throws Exception {
log.info(("CWLog fetch starting"));
List<OutputLogEvent> logEvents =
cloudWatchService.getLogs(
logGroupName, logStreamName, startTime.toEpochMilli(), QUERY_LIMIT);
Expand All @@ -124,24 +85,18 @@ protected void fetchAndValidateLogs(Instant startTime) throws Exception {
"[StructuredLogValidator] no logs found under log stream %s" + " in log group %s",
logStreamName, logGroupName));
}
log.info("Number of log events: " + logEvents.size());
for (OutputLogEvent logEvent : logEvents) {
log.info("Log message: " + logEvent.getMessage());
if (logEvent.getMessage().contains("Executing outgoing-http-call")) {
validateJsonSchema(logEvent.getMessage());
}
}
}

protected void validateJsonSchema(String logEventMsg) throws Exception {
log.info("In validateJsonSchema");
JsonNode logEventNode = mapper.readTree(logEventMsg);
log.info("In validateJsonSchema - post readTree");
if (schema != null) {
log.info("In validateJsonSchema - schema isn't null");
ProcessingReport report = schema.validate(JsonLoader.fromString(logEventNode.toString()));
if (report.isSuccess()) {
// validatedSchema.add(key);
log.info("Report was a success");
} else {
// This will probably generate a lot of extra logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import com.amazon.aoc.models.Context;
import com.amazon.aoc.models.ValidationConfig;
import com.amazon.aoc.services.TaskService;
import lombok.extern.log4j.Log4j2;

@Log4j2
public class ValidatorFactory {
private Context context;

Expand All @@ -42,7 +40,6 @@ public ValidatorFactory(Context context) {
*/
public IValidator launchValidator(ValidationConfig validationConfig) throws Exception {
// get validator
log.info("validator factory launch started");
IValidator validator;
FileConfig expectedData = null;
switch (validationConfig.getValidationType()) {
Expand All @@ -54,12 +51,10 @@ public IValidator launchValidator(ValidationConfig validationConfig) throws Exce
validator = new LoadBalancingValidator();
break;
case "cw-metric":
log.info("cw-metrics got picked");
validator = new CWMetricValidator();
expectedData = validationConfig.getExpectedMetricTemplate();
break;
case "cw-logs":
log.info("cw-logs got picked");
validator = new CWLogValidator();
expectedData = validationConfig.getExpectedLogStructureTemplate();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
httpPath: "/outgoing-http-call"
httpMethod: "get"
callingType: "http"
expectedLogStructureTemplate: "DEFAULT_EXPECTED_LOG"
expectedLogStructureTemplate: "DEFAULT_EXPECTED_LOG"

0 comments on commit a19a1a4

Please sign in to comment.