Skip to content

Commit 8a4f05e

Browse files
committed
refactor and cleanup
1 parent d94a0c5 commit 8a4f05e

File tree

8 files changed

+44
-55
lines changed

8 files changed

+44
-55
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
<maven-gpg-plugin.version>3.1.0</maven-gpg-plugin.version>
9595
<junit.version>5.10.0</junit.version>
9696
<aws-embedded-metrics.version>1.0.6</aws-embedded-metrics.version>
97-
<jmespath.version>0.5.1</jmespath.version>
97+
<jmespath.version>0.6.0</jmespath.version>
9898
<elastic.version>1.5.0</elastic.version>
9999
</properties>
100100

powertools-logging/powertools-logging-log4j/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/PowertoolsResolver.java

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414

1515
package org.apache.logging.log4j.layout.template.json.resolver;
1616

17+
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_ARN;
18+
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_COLD_START;
19+
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_MEMORY_SIZE;
20+
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_NAME;
21+
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_REQUEST_ID;
22+
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_TRACE_ID;
23+
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_VERSION;
24+
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.SAMPLING_RATE;
25+
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.SERVICE;
26+
27+
import java.util.Map;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
1730
import org.apache.logging.log4j.core.LogEvent;
1831
import org.apache.logging.log4j.layout.template.json.util.JsonWriter;
1932
import org.apache.logging.log4j.util.ReadOnlyStringMap;
@@ -47,7 +60,7 @@ public void resolve(LogEvent logEvent, JsonWriter jsonWriter) {
4760
private static final EventResolver FUNCTION_NAME_RESOLVER =
4861
(final LogEvent logEvent, final JsonWriter jsonWriter) -> {
4962
final String functionName =
50-
logEvent.getContextData().getValue(PowertoolsLoggedFields.FUNCTION_NAME.getName());
63+
logEvent.getContextData().getValue(FUNCTION_NAME.getName());
5164
jsonWriter.writeString(functionName);
5265
};
5366

@@ -168,48 +181,29 @@ public void resolve(LogEvent logEvent, JsonWriter jsonWriter) {
168181

169182
private final EventResolver internalResolver;
170183

184+
private static final Map<String, EventResolver> eventResolverMap = Stream.of(new Object[][] {
185+
{ SERVICE.getName(), SERVICE_RESOLVER },
186+
{ FUNCTION_NAME.getName(), FUNCTION_NAME_RESOLVER },
187+
{ FUNCTION_VERSION.getName(), FUNCTION_VERSION_RESOLVER },
188+
{ FUNCTION_ARN.getName(), FUNCTION_ARN_RESOLVER },
189+
{ FUNCTION_MEMORY_SIZE.getName(), FUNCTION_MEMORY_RESOLVER },
190+
{ FUNCTION_REQUEST_ID.getName(), FUNCTION_REQ_RESOLVER },
191+
{ FUNCTION_COLD_START.getName(), COLD_START_RESOLVER },
192+
{ FUNCTION_TRACE_ID.getName(), XRAY_TRACE_RESOLVER },
193+
{ SAMPLING_RATE.getName(), SAMPLING_RATE_RESOLVER },
194+
{ "region", REGION_RESOLVER },
195+
{ "account_id", ACCOUNT_ID_RESOLVER },
196+
}).collect(Collectors.toMap(data -> (String) data[0], data -> (EventResolver) data[1]));
197+
198+
171199
PowertoolsResolver(final TemplateResolverConfig config) {
172200
final String fieldName = config.getString("field");
173201
if (fieldName == null) {
174202
internalResolver = NON_POWERTOOLS_FIELD_RESOLVER;
175203
} else {
176-
switch (fieldName) {
177-
case "service":
178-
internalResolver = SERVICE_RESOLVER;
179-
break;
180-
case "function_name":
181-
internalResolver = FUNCTION_NAME_RESOLVER;
182-
break;
183-
case "function_version":
184-
case "service_version":
185-
internalResolver = FUNCTION_VERSION_RESOLVER;
186-
break;
187-
case "function_arn":
188-
internalResolver = FUNCTION_ARN_RESOLVER;
189-
break;
190-
case "function_memory_size":
191-
internalResolver = FUNCTION_MEMORY_RESOLVER;
192-
break;
193-
case "function_request_id":
194-
internalResolver = FUNCTION_REQ_RESOLVER;
195-
break;
196-
case "cold_start":
197-
internalResolver = COLD_START_RESOLVER;
198-
break;
199-
case "xray_trace_id":
200-
internalResolver = XRAY_TRACE_RESOLVER;
201-
break;
202-
case "region":
203-
internalResolver = REGION_RESOLVER;
204-
break;
205-
case "account_id":
206-
internalResolver = ACCOUNT_ID_RESOLVER;
207-
break;
208-
case "sampling_rate":
209-
internalResolver = SAMPLING_RATE_RESOLVER;
210-
break;
211-
default:
212-
throw new IllegalArgumentException("unknown field: " + fieldName);
204+
internalResolver = eventResolverMap.get(fieldName);
205+
if (internalResolver == null) {
206+
throw new IllegalArgumentException("unknown field: " + fieldName);
213207
}
214208
}
215209
}

powertools-logging/powertools-logging-log4j/src/main/resources/LambdaEcsLayout.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"service.version": {
3838
"$resolver": "powertools",
39-
"field": "service_version"
39+
"field": "function_version"
4040
},
4141
"log.logger": {
4242
"$resolver": "logger",

powertools-logging/powertools-logging-log4j/src/main/resources/LambdaJsonLayout.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"timestamp": {
6060
"$resolver": "timestamp",
6161
"pattern": {
62-
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSZz"
62+
"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
6363
}
6464
},
6565
"xray_trace_id": {

powertools-logging/powertools-logging-log4j/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/PowerToolsResolverFactoryTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.mockito.Mock;
3535
import org.slf4j.MDC;
3636
import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor;
37-
import software.amazon.lambda.powertools.logging.internal.LambdaLoggingAspect;
3837
import software.amazon.lambda.powertools.logging.internal.handler.PowertoolsLogEnabled;
3938

4039
@Order(1)
@@ -56,7 +55,6 @@ void setUp() throws IllegalAccessException, IOException {
5655
} catch (NoSuchFileException e) {
5756
// file may not exist on the first launch
5857
}
59-
writeStaticField(LambdaLoggingAspect.class, "SAMPLING_RATE", null, true);
6058
}
6159

6260
@AfterEach
@@ -66,15 +64,13 @@ void cleanUp() throws IOException{
6664
}
6765

6866
@Test
69-
void shouldLogInJsonFormat() throws IllegalAccessException {
70-
writeStaticField(LambdaLoggingAspect.class, "SAMPLING_RATE", "0.000000001", true);
71-
67+
void shouldLogInJsonFormat() {
7268
PowertoolsLogEnabled handler = new PowertoolsLogEnabled();
7369
handler.handleRequest("Input", context);
7470

7571
File logFile = new File("target/logfile.json");
7672
assertThat(contentOf(logFile)).contains(
77-
"{\"level\":\"DEBUG\",\"message\":\"Test debug event\",\"cold_start\":true,\"function_arn\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"function_memory_size\":1024,\"function_name\":\"testFunction\",\"function_request_id\":\"RequestId\",\"function_version\":\"1\",\"sampling_rate\":1.0E-9,\"service\":\"testLog4j\",\"timestamp\":")
73+
"{\"level\":\"DEBUG\",\"message\":\"Test debug event\",\"cold_start\":true,\"function_arn\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"function_memory_size\":1024,\"function_name\":\"testFunction\",\"function_request_id\":\"RequestId\",\"function_version\":\"1\",\"service\":\"testLog4j\",\"timestamp\":")
7874
.contains("\"xray_trace_id\":\"1-63441c4a-abcdef012345678912345678\",\"myKey\":\"myValue\"}\n");
7975
}
8076

powertools-logging/powertools-logging-logback/src/main/java/software/amazon/lambda/powertools/logging/LambdaJsonEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class LambdaJsonEncoder extends EncoderBase<ILoggingEvent> {
3333

3434
private final ThrowableProxyConverter throwableProxyConverter = new ThrowableProxyConverter();
3535
protected ThrowableHandlingConverter throwableConverter = null;
36-
protected String timestampFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZz";
36+
protected String timestampFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
3737
protected String timestampFormatTimezoneId = null;
3838
private boolean includeThreadInfo = false;
3939
private boolean includePowertoolsInfo = true;
@@ -88,7 +88,7 @@ public byte[] footerBytes() {
8888
}
8989

9090
/**
91-
* Specify the format of the timestamp (default is <b>yyyy-MM-dd'T'HH:mm:ss.SSSZz</b>):
91+
* Specify the format of the timestamp (default is <b>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</b>):
9292
* <br/>
9393
* <pre>{@code
9494
* <encoder class="software.amazon.lambda.powertools.logging.LambdaJsonEncoder">

powertools-logging/powertools-logging-logback/src/main/java/software/amazon/lambda/powertools/logging/internal/LambdaJsonSerializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ public static void serializePowertools(StringBuilder builder, Map<String, String
114114
sortedMap.forEach((k, v) -> {
115115
if ((PowertoolsLoggedFields.stringValues().contains(k) && includePowertoolsInfo)
116116
|| !PowertoolsLoggedFields.stringValues().contains(k)) {
117-
serializeAttribute(builder, k, v);
117+
if (!k.equals(PowertoolsLoggedFields.SAMPLING_RATE.getName()) || !v.equals("0.0")) { // do not log sampling rate when 0
118+
serializeAttribute(builder, k, v);
119+
}
118120
}
119121
});
120122
}

powertools-logging/powertools-logging-logback/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaJsonEncoderTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ void setUp() throws IllegalAccessException, IOException {
5858
openMocks(this);
5959
MDC.clear();
6060
writeStaticField(LambdaHandlerProcessor.class, "IS_COLD_START", null, true);
61-
writeStaticField(LambdaLoggingAspect.class, "SAMPLING_RATE", null, true);
6261
setupContext();
6362
// Make sure file is cleaned up before running tests
6463
try {
@@ -74,10 +73,8 @@ void cleanUp() throws IOException{
7473
}
7574

7675
@Test
77-
void shouldLogInJsonFormat() throws IllegalAccessException {
76+
void shouldLogInJsonFormat() {
7877
// GIVEN
79-
writeStaticField(LambdaLoggingAspect.class, "SAMPLING_RATE", "0.000000001", true);
80-
8178
PowertoolsLogEnabled handler = new PowertoolsLogEnabled();
8279

8380
// WHEN
@@ -86,7 +83,7 @@ void shouldLogInJsonFormat() throws IllegalAccessException {
8683
// THEN
8784
File logFile = new File("target/logfile.json");
8885
assertThat(contentOf(logFile)).contains(
89-
"{\"level\":\"DEBUG\",\"message\":\"Test debug event\",\"cold_start\":true,\"function_arn\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"function_memory_size\":1024,\"function_name\":\"testFunction\",\"function_request_id\":\"RequestId\",\"function_version\":1,\"myKey\":\"myValue\",\"sampling_rate\":\"1.0E-9\",\"service\":\"testLogback\",\"xray_trace_id\":\"1-63441c4a-abcdef012345678912345678\",\"timestamp\":");
86+
"{\"level\":\"DEBUG\",\"message\":\"Test debug event\",\"cold_start\":true,\"function_arn\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"function_memory_size\":1024,\"function_name\":\"testFunction\",\"function_request_id\":\"RequestId\",\"function_version\":1,\"myKey\":\"myValue\",\"service\":\"testLogback\",\"xray_trace_id\":\"1-63441c4a-abcdef012345678912345678\",\"timestamp\":");
9087
}
9188

9289
private final LoggingEvent loggingEvent = new LoggingEvent("fqcn", logger, Level.INFO, "message", null, null);

0 commit comments

Comments
 (0)