Skip to content

Commit

Permalink
fix: handle valid json message which isn't a GreengrassLogMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeDombo committed Oct 25, 2023
1 parent 614be25 commit 55b0f0d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,11 @@ private Optional<GreengrassLogMessage> tryGetStructuredLogMessage(String data) {
return Optional.empty();
}
try {
return Optional.ofNullable(DESERIALIZER.readValue(data, GreengrassLogMessage.class));
GreengrassLogMessage message = DESERIALIZER.readValue(data, GreengrassLogMessage.class);
if (message == null || Utils.isEmpty(message.getLevel())) {
return Optional.empty();
}
return Optional.of(message);
} catch (JsonProcessingException ignored) {
// If unable to deserialize, then we treat it as a normal log line and do not need to smartly upload.
return Optional.empty();
Expand Down Expand Up @@ -359,7 +363,7 @@ private Pair<Boolean, AtomicInteger> checkAndAddNewLogEvent(AtomicInteger totalB
GreengrassLogMessage logMessage) {
Level currentLogLevel = Level.valueOf(logMessage.getLevel());
if (currentLogLevel.toInt() < desiredLogLevel.toInt()) {
return new Pair(false, new AtomicInteger());
return new Pair<>(false, new AtomicInteger());
}
return addNewLogEvent(totalBytesRead, attemptLogInformation, data, dataSize,
Instant.ofEpochMilli(logMessage.getTimestamp()));
Expand Down Expand Up @@ -439,7 +443,7 @@ private Pair<Boolean, AtomicInteger> addNewLogEvent(AtomicInteger totalBytesRead

currChunk++;
}
return new Pair(reachedMaxBatchSize, currBytesRead);
return new Pair<>(reachedMaxBatchSize, currBytesRead);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ void GIVEN_one_components_two_file_less_than_max_WHEN_merge_THEN_reads_and_merge
String logStream2 = "/2020/12/18/thing/testThing";
assertTrue(attempt.getLogStreamsToLogEventsMap().containsKey(logStream));
assertTrue(attempt.getLogStreamsToLogEventsMap().containsKey(logStream2));
CloudWatchAttemptLogInformation logEventsForStream1 = attempt.getLogStreamsToLogEventsMap().get(logStream);
CloudWatchAttemptLogInformation logEventsForStream2 = attempt.getLogStreamsToLogEventsMap().get(logStream2);
CloudWatchAttemptLogInformation logEventsForStream1 = attempt.getLogStreamsToLogEventsMap().remove(logStream);
CloudWatchAttemptLogInformation logEventsForStream2 = attempt.getLogStreamsToLogEventsMap().remove(logStream2);
assertNotNull(logEventsForStream1.getLogEvents());
assertEquals(13, logEventsForStream1.getLogEvents().size());
assertTrue(logEventsForStream1.getAttemptLogFileInformationMap().containsKey(fileHash1));
Expand All @@ -494,10 +494,15 @@ void GIVEN_one_components_two_file_less_than_max_WHEN_merge_THEN_reads_and_merge
}

assertNotNull(logEventsForStream2.getLogEvents());
assertEquals(4, logEventsForStream2.getLogEvents().size());
assertEquals(3, logEventsForStream2.getLogEvents().size());
// Read the 1 remaining cloudwatch log stream which will have today's date because the log lines
// had no parsed timestamp.
assertEquals(2, attempt.getLogStreamsToLogEventsMap()
.get(attempt.getLogStreamsToLogEventsMap().keySet().stream().findFirst().get()).getLogEvents().size());
assertTrue(logEventsForStream2.getAttemptLogFileInformationMap().containsKey(fileHash2));
assertEquals(0, logEventsForStream2.getAttemptLogFileInformationMap().get(fileHash2).getStartPosition());
assertEquals(1239, logEventsForStream2.getAttemptLogFileInformationMap().get(fileHash2).getBytesRead());
assertEquals(1237, logEventsForStream2.getAttemptLogFileInformationMap().get(fileHash2).getBytesRead());
System.out.println(logEventsForStream2.getAttemptLogFileInformationMap().get(fileHash2));
assertEquals("TestComponent", logEventsForStream2.getComponentName());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{"contexts":{"component":"demo","device":"asdf"},"eventType":"th1-event","level":"INFO","loggerName":"com.aws.greengrass.logging.examples.LoggerDemo","message":"test th1 info","timestamp":1608292800000}
{"contexts":{"component":"demo","device":"asdf"},"eventType":"th1-event","level":null,"loggerName":"com.aws.greengrass.logging.examples.LoggerDemo","message":"test th1 info","timestamp":1608292800000}
{"contexts":{"component":"demo","device":"asdf"},"eventType":"th2-event","level":"INFO","loggerName":"com.aws.greengrass.logging.examples.LoggerDemo","message":"test th2 info","timestamp":1608292800000}
{"contexts":{"component":"th1-override","device":"asdf"},"level":"DEBUG","loggerName":"com.aws.greengrass.logging.examples.LoggerDemo","message":"test th1 debug","timestamp":1608292800000}
{"contexts":{"component":"demo","device":"asdf"},"level":"INFO","loggerName":"com.aws.greengrass.logging.examples.LoggerDemo","message":"test main info","timestamp":1608292800000}
{"cause":{"localizedMessage":"some error","message":"some error","stackTrace":[{"className":"com.aws.greengrass.logging.examples.LoggerDemo","fileName":"LoggerDemo.java","lineNumber":56,"methodName":"main","nativeMethod":false}],"suppressed":[]},"contexts":{"key2":"value2","component":"demo","device":"asdf"},"eventType":"error-event","level":"ERROR","loggerName":"com.aws.greengrass.logging.examples.LoggerDemo","message":"test error","timestamp":1608292800000}
{"something which parses": "as json, but isn't a GreengrassStructuredLogMessage"}

0 comments on commit 55b0f0d

Please sign in to comment.