Skip to content

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

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

@Test
void shouldLogInStructuredFormat() throws IOException {
handler.handleRequest("test", context);

assertThat(Files.lines(Paths.get("target/logfile.json")))
.hasSize(1)
.allSatisfy(line -> assertThat(parseToMap(line))
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Up @@ -2,13 +2,17 @@

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.lambda.logging.PowerToolsLogging;

public class PowerLogToolEnabled implements RequestHandler<Object, Object> {
private final Logger LOG = LogManager.getLogger(PowerToolLogEventEnabled.class);

@Override
@PowerToolsLogging
public Object handleRequest(Object input, Context context) {
LOG.info("Test event");
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/log4j2.xml
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">
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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">
Expand Down