forked from aws-observability/aws-otel-test-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
965ec7b
commit 3ba9457
Showing
11 changed files
with
195 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="WARN"> | ||
<Appenders> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="Console"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 128 additions & 80 deletions
208
validator/src/main/java/com/amazon/aoc/validators/CWLogValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,146 @@ | ||
package com.amazon.aoc.validators; | ||
|
||
import com.amazon.aoc.callers.ICaller; | ||
import com.amazon.aoc.exception.BaseException; | ||
import com.amazon.aoc.exception.ExceptionCode; | ||
import com.amazon.aoc.fileconfigs.FileConfig; | ||
import com.amazon.aoc.helpers.MustacheHelper; | ||
import com.amazon.aoc.helpers.RetryHelper; | ||
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.amazon.aoc.services.CloudWatchService; | ||
import com.amazonaws.services.logs.model.OutputLogEvent; | ||
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 com.github.fge.jsonschema.main.JsonSchema; | ||
import com.github.fge.jsonschema.main.JsonSchemaFactory; | ||
import com.github.fge.jsonschema.report.ListReportProvider; | ||
import com.github.fge.jsonschema.report.LogLevel; | ||
import com.github.fge.jsonschema.report.ProcessingReport; | ||
import com.github.fge.jsonschema.util.JsonLoader; | ||
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 { | ||
|
||
|
||
import java.time.temporal.ChronoUnit; | ||
import java.util.*; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
// String getJsonSchemaMappingKey(JsonNode jsonNode) { | ||
// // Your implementation for getting the JSON schema mapping key | ||
// return null; | ||
// } | ||
@Log4j2 | ||
public class CWLogValidator extends AbstractStructuredLogValidator { | ||
protected Map<String, JsonSchema> schemasToValidate = new HashMap<>(); | ||
protected Set<String> validatedSchema = new HashSet<>(); | ||
protected Set<String> logStreamNames = new HashSet<>(); | ||
protected String logGroupName; | ||
|
||
@Override | ||
public void init(Context context, ValidationConfig validationConfig, ICaller caller, FileConfig expectedDataTemplate) throws Exception { | ||
protected String logStreamName = "otlp-logs"; | ||
|
||
} | ||
protected CloudWatchService cloudWatchService; | ||
private static final int CHECK_INTERVAL_IN_MILLI = 30 * 1000; | ||
private static final int CHECK_DURATION_IN_SECONDS = 2 * 60; | ||
private static final int MAX_RETRY_COUNT = 12; | ||
private static final int QUERY_LIMIT = 100; | ||
private JsonSchema schema; | ||
|
||
@Override | ||
public void validate() throws Exception { | ||
var lines = new HashSet<String>(); | ||
InputStream inputStream = getClass().getResourceAsStream("/logs/testingJSON.log"); | ||
private Context context; | ||
|
||
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); | ||
} | ||
@Override | ||
public void init(Context context, 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")); | ||
} | ||
|
||
var cwClient = CloudWatchLogsClient.builder().build(); | ||
var objectMapper = new ObjectMapper(); | ||
// @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")); | ||
// } | ||
|
||
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()); | ||
@Override | ||
String getJsonSchemaMappingKey(JsonNode jsonNode) { | ||
return jsonNode.get("Type").asText(); | ||
} | ||
|
||
var events = response.events(); | ||
var receivedMessages = events.stream().map(x -> x.message()).collect(Collectors.toSet()); | ||
@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); | ||
}); | ||
} | ||
|
||
// 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()); | ||
@Override | ||
protected void fetchAndValidateLogs(Instant startTime) throws Exception { | ||
log.info(("CWLog fetch starting")); | ||
List<OutputLogEvent> logEvents = | ||
cloudWatchService.getLogs( | ||
logGroupName, logStreamName, startTime.toEpochMilli(), QUERY_LIMIT); | ||
if (logEvents.isEmpty()) { | ||
throw new BaseException( | ||
ExceptionCode.LOG_FORMAT_NOT_MATCHED, | ||
String.format( | ||
"[StructuredLogValidator] no logs found under log stream %s" + " in log group %s", | ||
logStreamName, logGroupName)); | ||
} | ||
for (OutputLogEvent logEvent : logEvents) { | ||
log.info("Log message: " + logEvent.getMessage()); | ||
validateJsonSchema(logEvent.getMessage()); | ||
} | ||
} | ||
|
||
// 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; | ||
}); | ||
@Override | ||
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 | ||
// may want to log this to a different level in the future. | ||
log.info("[StructuredLogValidator] failed to validate schema \n"); | ||
log.info(report.toString() + "\n"); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected int getMaxRetryCount() { | ||
return MAX_RETRY_COUNT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
validator/src/main/resources/expected-data-template/otlpExpectedLog.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[ | ||
{ | ||
"body": "{{body}}", | ||
"severity_number": 9, | ||
"severity_text": "INFO", | ||
"flags": 1, | ||
"trace_id": "{{trace_id}}", | ||
"span_id": "{{span_id}}", | ||
"resource": { | ||
"service.name": "{{resource.service.name}}" | ||
} | ||
|
||
} | ||
] |
Oops, something went wrong.