Skip to content
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
Expand Up @@ -15,6 +15,7 @@
package org.apache.logging.log4j.layout.template.json.resolver;

import static java.util.Arrays.stream;
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.CORRELATION_ID;
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_ARN;
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_COLD_START;
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_MEMORY_SIZE;
Expand Down Expand Up @@ -112,7 +113,7 @@ public boolean isResolvable(LogEvent logEvent) {
final String samplingRate =
logEvent.getContextData().getValue(PowertoolsLoggedFields.SAMPLING_RATE.getName());
try {
return (null != samplingRate && Float.parseFloat(samplingRate) > 0.f);
return null != samplingRate && Float.parseFloat(samplingRate) > 0.f;
} catch (NumberFormatException nfe) {
return false;
}
Expand Down Expand Up @@ -142,6 +143,22 @@ public void resolve(LogEvent logEvent, JsonWriter jsonWriter) {
}
};

private static final EventResolver CORRELATION_ID_RESOLVER = new EventResolver() {
@Override
public boolean isResolvable(LogEvent logEvent) {
final String correlationId =
logEvent.getContextData().getValue(PowertoolsLoggedFields.CORRELATION_ID.getName());
return null != correlationId;
}

@Override
public void resolve(LogEvent logEvent, JsonWriter jsonWriter) {
final String correlationId =
logEvent.getContextData().getValue(PowertoolsLoggedFields.CORRELATION_ID.getName());
jsonWriter.writeString(correlationId);
}
};

private static final EventResolver SERVICE_RESOLVER =
(final LogEvent logEvent, final JsonWriter jsonWriter) -> {
final String service = logEvent.getContextData().getValue(PowertoolsLoggedFields.SERVICE.getName());
Expand Down Expand Up @@ -214,6 +231,7 @@ public void resolve(LogEvent logEvent, JsonWriter jsonWriter) {
{ FUNCTION_REQUEST_ID.getName(), FUNCTION_REQ_RESOLVER },
{ FUNCTION_COLD_START.getName(), COLD_START_RESOLVER },
{ FUNCTION_TRACE_ID.getName(), XRAY_TRACE_RESOLVER },
{ CORRELATION_ID.getName(), CORRELATION_ID_RESOLVER },
{ SAMPLING_RATE.getName(), SAMPLING_RATE_RESOLVER },
{ "region", REGION_RESOLVER },
{ "account_id", ACCOUNT_ID_RESOLVER }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
"$resolver": "thread",
"field": "name"
},
"cloud.provider" : "aws",
"cloud.service.name" : "lambda",
"cloud.region" : {
"cloud.provider": "aws",
"cloud.service.name": "lambda",
"cloud.region": {
"$resolver": "powertools",
"field": "region"
},
"cloud.account.id" : {
"cloud.account.id": {
"$resolver": "powertools",
"field": "account_id"
},
Expand Down Expand Up @@ -83,7 +83,11 @@
"$resolver": "powertools",
"field": "xray_trace_id"
},
"correlation.id": {
"$resolver": "powertools",
"field": "correlation_id"
},
"": {
"$resolver": "powertools"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"$resolver": "powertools",
"field": "xray_trace_id"
},
"correlation_id": {
"$resolver": "powertools",
"field": "correlation_id"
},
"": {
"$resolver": "powertools"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ void shouldLogArgumentsAsJsonWhenUsingRawJson() {
.contains(
"\"input\":{\"awsRegion\":\"us-east-1\",\"body\":\"plop\",\"eventSource\":\"eb\",\"messageAttributes\":{\"keyAttribute\":{\"stringListValues\":[\"val1\",\"val2\",\"val3\"]}},\"messageId\":\"1212abcd\"}")
.contains("\"message\":\"1212abcd\"")
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"");
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"")
.contains("\"correlation_id\":\"1212abcd\"");
// Reserved keys should be ignored
PowertoolsLoggedFields.stringValues().stream().forEach(reservedKey -> {
assertThat(contentOf(logFile)).doesNotContain("\"" + reservedKey + "\":\"shouldBeIgnored\"");
Expand Down Expand Up @@ -122,7 +123,8 @@ void shouldLogArgumentsAsJsonWhenUsingKeyValue() {
.contains(
"\"input\":{\"awsRegion\":\"us-east-1\",\"body\":\"plop\",\"eventSource\":\"eb\",\"messageAttributes\":{\"keyAttribute\":{\"stringListValues\":[\"val1\",\"val2\",\"val3\"]}},\"messageId\":\"1212abcd\"}")
.contains("\"message\":\"1212abcd\"")
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"");
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"")
.contains("\"correlation_id\":\"1212abcd\"");

// Reserved keys should be ignored
PowertoolsLoggedFields.stringValues().stream().forEach(reservedKey -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package software.amazon.lambda.powertools.logging.logback;

import static java.nio.charset.StandardCharsets.UTF_8;
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.CORRELATION_ID;
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_ARN;
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_COLD_START;
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_MEMORY_SIZE;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class LambdaEcsEncoder extends EncoderBase<ILoggingEvent> {
protected static final String FUNCTION_VERSION_ATTR_NAME = "faas.version";
protected static final String FUNCTION_MEMORY_ATTR_NAME = "faas.memory";
protected static final String FUNCTION_TRACE_ID_ATTR_NAME = "trace.id";
protected static final String CORRELATION_ID_ATTR_NAME = "correlation.id";

protected static final String ECS_VERSION = "1.2.0";
protected static final String CLOUD_PROVIDER = "aws";
Expand Down Expand Up @@ -156,6 +158,11 @@ private void serializeFunctionInfo(JsonSerializer serializer, String arn, Map<St
serializer.writeStringField(FUNCTION_COLD_START_ATTR_NAME, mdcPropertyMap.get(FUNCTION_COLD_START.getName()));
serializer.writeRaw(',');
serializer.writeStringField(FUNCTION_TRACE_ID_ATTR_NAME, mdcPropertyMap.get(FUNCTION_TRACE_ID.getName()));
String correlationId = mdcPropertyMap.get(CORRELATION_ID.getName());
if (correlationId != null) {
serializer.writeRaw(',');
serializer.writeStringField(CORRELATION_ID_ATTR_NAME, correlationId);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ void shouldNotLogFunctionInfo() {

// THEN
assertThat(result).contains(
"\"faas.id\":\"arn:aws:lambda:us-east-1:123456789012:function:test\",\"faas.name\":\"test-function\",\"faas.version\":\"1\",\"faas.memory\":\"128\",\"faas.execution\":\"test-request-id\",\"faas.coldstart\":\"false\"");
"\"faas.id\":\"arn:aws:lambda:us-east-1:123456789012:function:test\",\"faas.name\":\"test-function\",\"faas.version\":\"1\",\"faas.memory\":\"128\",\"faas.execution\":\"test-request-id\",\"faas.coldstart\":\"false\"")
.contains("\"correlation.id\":\"test-correlation-id\"");

// WHEN (includeFaasInfo = false)
encoder.setIncludeFaasInfo(false);
Expand Down Expand Up @@ -175,6 +176,7 @@ private void setMDC() {
MDC.put(PowertoolsLoggedFields.FUNCTION_COLD_START.getName(), "false");
MDC.put(PowertoolsLoggedFields.SAMPLING_RATE.getName(), "0.2");
MDC.put(PowertoolsLoggedFields.SERVICE.getName(), "Service");
MDC.put(PowertoolsLoggedFields.CORRELATION_ID.getName(), "test-correlation-id");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ void shouldLogArgumentsAsJsonWhenUsingRawJson() {
"\"input\":{\"awsRegion\":\"eu-central-1\",\"body\":\"plop\",\"eventSource\":\"eb\",\"messageAttributes\":{\"keyAttribute\":{\"stringListValues\":[\"val1\",\"val2\",\"val3\"]}},\"messageId\":\"1212abcd\"}")
.contains("\"message\":\"1212abcd\"")
// Should auto-escape double quotes around id
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"");
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"")
.contains("\"correlation_id\":\"1212abcd\"");
// Reserved keys should be ignored
PowertoolsLoggedFields.stringValues().stream().forEach(reservedKey -> {
assertThat(contentOf(logFile)).doesNotContain("\"" + reservedKey + "\":\"shouldBeIgnored\"");
Expand Down Expand Up @@ -168,7 +169,8 @@ void shouldLogArgumentsAsJsonWhenUsingKeyValue() {
"\"input\":{\"awsRegion\":\"eu-central-1\",\"body\":\"plop\",\"eventSource\":\"eb\",\"messageAttributes\":{\"keyAttribute\":{\"stringListValues\":[\"val1\",\"val2\",\"val3\"]}},\"messageId\":\"1212abcd\"}")
.contains("\"message\":\"1212abcd\"")
// Should auto-escape double quotes around id
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"");
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"")
.contains("\"correlation_id\":\"1212abcd\"");
// Reserved keys should be ignored
PowertoolsLoggedFields.stringValues().stream().forEach(reservedKey -> {
assertThat(contentOf(logFile)).doesNotContain("\"" + reservedKey + "\":\"shouldBeIgnored\"");
Expand Down
Loading