-
Notifications
You must be signed in to change notification settings - Fork 90
Full stack regression for structured logging #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.apache.logging.log4j.core.layout; | ||
|
||
import java.io.IOException; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import software.amazon.lambda.handlers.PowerLogToolEnabled; | ||
|
||
import static java.util.Collections.emptyMap; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
class LambdaJsonLayoutTest { | ||
|
||
private RequestHandler<Object, Object> handler = new PowerLogToolEnabled(); | ||
|
||
@Mock | ||
private Context context; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
initMocks(this); | ||
setupContext(); | ||
//Make sure file is cleaned up before running full stack logging regression | ||
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close(); | ||
} | ||
|
||
@Test | ||
void shouldLogInStructuredFormat() throws IOException { | ||
handler.handleRequest("test", context); | ||
|
||
assertThat(Files.lines(Paths.get("target/logfile.json"))) | ||
.hasSize(1) | ||
.allSatisfy(line -> assertThat(parseToMap(line)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v cool. |
||
.containsEntry("functionName", "testFunction") | ||
.containsEntry("functionVersion", "1") | ||
.containsEntry("functionMemorySize", "10") | ||
.containsEntry("functionArn", "testArn") | ||
.containsKey("timestamp") | ||
.containsKey("message") | ||
.containsKey("service")); | ||
} | ||
|
||
private Map<String, Object> parseToMap(String stringAsJson) { | ||
try { | ||
return new ObjectMapper().readValue(stringAsJson, Map.class); | ||
} catch (JsonProcessingException e) { | ||
fail("Failed parsing logger line " + stringAsJson); | ||
return emptyMap(); | ||
} | ||
} | ||
|
||
private void setupContext() { | ||
when(context.getFunctionName()).thenReturn("testFunction"); | ||
when(context.getInvokedFunctionArn()).thenReturn("testArn"); | ||
when(context.getFunctionVersion()).thenReturn("1"); | ||
when(context.getMemoryLimitInMB()).thenReturn(10); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2"> | ||
<Appenders> | ||
<Console name="JsonAppender" target="SYSTEM_OUT"> | ||
<File name="JsonAppender" fileName="target/logfile.json"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change make log4j to log to a file which we have used in our regression test to make sure the structured logging works correctly with correct lambda keys injected There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But, it is not the default appender? Why not creating two log4j config file to test both? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's so difficult to test system out. I think this is a very slick way of testing the functionality. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok good point |
||
<LambdaJsonLayout compact="true" eventEol="true"/> | ||
</Console> | ||
</File> | ||
</Appenders> | ||
<Loggers> | ||
<Logger name="JsonLogger" level="INFO" additivity="false"> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍