Skip to content

Commit 51354b9

Browse files
committed
improve correlationId:
- use jmespath for correlationId - add appsync + test - rename constant
1 parent 9378684 commit 51354b9

File tree

10 files changed

+70
-14
lines changed

10 files changed

+70
-14
lines changed

docs/core/logging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ for known event sources, where either a request ID or X-Ray Trace ID are present
357357
=== "App.java"
358358

359359
```java hl_lines="10"
360-
import software.amazon.lambda.powertools.logging.CorrelationIdPathConstants;
360+
import software.amazon.lambda.powertools.logging.CorrelationIdPaths;
361361

362362
/**
363363
* Handler for requests to Lambda function.
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@
1717
/**
1818
* Supported Event types from which Correlation ID can be extracted
1919
*/
20-
public class CorrelationIdPathConstants {
20+
public class CorrelationIdPaths {
2121
/**
2222
* To use when function is expecting API Gateway Rest API Request event
2323
*/
24-
public static final String API_GATEWAY_REST = "/requestContext/requestId";
24+
public static final String API_GATEWAY_REST = "requestContext.requestId";
2525
/**
2626
* To use when function is expecting API Gateway HTTP API Request event
2727
*/
28-
public static final String API_GATEWAY_HTTP = "/requestContext/requestId";
28+
public static final String API_GATEWAY_HTTP = "requestContext.requestId";
2929
/**
3030
* To use when function is expecting Application Load balancer Request event
3131
*/
32-
public static final String APPLICATION_LOAD_BALANCER = "/headers/x-amzn-trace-id";
32+
public static final String APPLICATION_LOAD_BALANCER = "headers.\"x-amzn-trace-id\"";
3333
/**
3434
* To use when function is expecting Event Bridge Request event
3535
*/
36-
public static final String EVENT_BRIDGE = "/id";
36+
public static final String EVENT_BRIDGE = "id";
37+
/**
38+
* To use when function is expecting an AppSync request
39+
*/
40+
public static final String APPSYNC_RESOLVER = "request.headers.\"x-amzn-trace-id\"";
3741
}

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/Logging.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
public @interface Logging {
6565

6666
/**
67-
* Set to true if you want to log the event received by the Lambda function handler
67+
* Set to true if you want to log the event received by the Lambda function handler.
68+
* Can also be configured with the 'POWERTOOLS_LOGGER_LOG_EVENT' environment variable
6869
*/
6970
boolean logEvent() default false;
7071

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.SERVICE;
3333

3434
import com.amazonaws.services.lambda.runtime.Context;
35-
import com.fasterxml.jackson.core.JsonPointer;
3635
import com.fasterxml.jackson.core.JsonProcessingException;
3736
import com.fasterxml.jackson.databind.JsonNode;
37+
import io.burt.jmespath.Expression;
3838
import java.io.ByteArrayInputStream;
3939
import java.io.ByteArrayOutputStream;
4040
import java.io.IOException;
@@ -292,7 +292,8 @@ private Object[] captureCorrelationId(final String correlationIdPath,
292292
}
293293

294294
private void setCorrelationIdFromNode(String correlationIdPath, ProceedingJoinPoint pjp, JsonNode jsonNode) {
295-
JsonNode node = jsonNode.at(JsonPointer.compile(correlationIdPath));
295+
Expression<JsonNode> jmesExpression = JsonConfig.get().getJmesPath().compile(correlationIdPath);
296+
JsonNode node = jmesExpression.search(jsonNode);
296297

297298
String asText = node.asText();
298299
if (null != asText && !asText.isEmpty()) {

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/handlers/PowertoolsLogAlbCorrelationId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package software.amazon.lambda.powertools.logging.handlers;
1616

17-
import static software.amazon.lambda.powertools.logging.CorrelationIdPathConstants.APPLICATION_LOAD_BALANCER;
17+
import static software.amazon.lambda.powertools.logging.CorrelationIdPaths.APPLICATION_LOAD_BALANCER;
1818

1919
import com.amazonaws.services.lambda.runtime.Context;
2020
import com.amazonaws.services.lambda.runtime.RequestHandler;

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/handlers/PowertoolsLogApiGatewayHttpApiCorrelationId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package software.amazon.lambda.powertools.logging.handlers;
1616

17-
import static software.amazon.lambda.powertools.logging.CorrelationIdPathConstants.API_GATEWAY_HTTP;
17+
import static software.amazon.lambda.powertools.logging.CorrelationIdPaths.API_GATEWAY_HTTP;
1818

1919
import com.amazonaws.services.lambda.runtime.Context;
2020
import com.amazonaws.services.lambda.runtime.RequestHandler;

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/handlers/PowertoolsLogApiGatewayRestApiCorrelationId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package software.amazon.lambda.powertools.logging.handlers;
1616

17-
import static software.amazon.lambda.powertools.logging.CorrelationIdPathConstants.API_GATEWAY_REST;
17+
import static software.amazon.lambda.powertools.logging.CorrelationIdPaths.API_GATEWAY_REST;
1818

1919
import com.amazonaws.services.lambda.runtime.Context;
2020
import com.amazonaws.services.lambda.runtime.RequestHandler;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
15+
package software.amazon.lambda.powertools.logging.handlers;
16+
17+
import static software.amazon.lambda.powertools.logging.CorrelationIdPaths.APPSYNC_RESOLVER;
18+
19+
import com.amazonaws.services.lambda.runtime.Context;
20+
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.OutputStream;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import software.amazon.lambda.powertools.logging.Logging;
27+
28+
public class PowertoolsLogAppSyncCorrelationId implements RequestStreamHandler {
29+
30+
private final Logger LOG = LoggerFactory.getLogger(PowertoolsLogAppSyncCorrelationId.class);
31+
32+
@Override
33+
@Logging(correlationIdPath = APPSYNC_RESOLVER)
34+
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
35+
LOG.info("Test event");
36+
}
37+
}

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/handlers/PowertoolsLogEventBridgeCorrelationId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package software.amazon.lambda.powertools.logging.handlers;
1616

17-
import static software.amazon.lambda.powertools.logging.CorrelationIdPathConstants.EVENT_BRIDGE;
17+
import static software.amazon.lambda.powertools.logging.CorrelationIdPaths.EVENT_BRIDGE;
1818

1919
import com.amazonaws.services.lambda.runtime.Context;
2020
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogAlbCorrelationId;
6565
import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogApiGatewayHttpApiCorrelationId;
6666
import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogApiGatewayRestApiCorrelationId;
67+
import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogAppSyncCorrelationId;
6768
import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogClearState;
6869
import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogDisabled;
6970
import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogDisabledForStream;
@@ -314,10 +315,22 @@ void shouldLogCorrelationIdOnALBEvent(ApplicationLoadBalancerRequestEvent event)
314315
void shouldLogCorrelationIdOnStreamHandler() throws IOException {
315316
RequestStreamHandler handler = new PowertoolsLogEventBridgeCorrelationId();
316317
String eventId = "3";
317-
String event = "{\"id\":" + eventId + "}"; // CorrelationIdPathConstants.EVENT_BRIDGE
318+
String event = "{\"id\":" + eventId + "}"; // CorrelationIdPath.EVENT_BRIDGE
318319
ByteArrayInputStream inputStream = new ByteArrayInputStream(event.getBytes());
319320
handler.handleRequest(inputStream, new ByteArrayOutputStream(), context);
320321

322+
assertThat(MDC.getCopyOfContextMap())
323+
.hasSize(EXPECTED_CONTEXT_SIZE + 1)
324+
.containsEntry("correlation_id", eventId);
325+
}
326+
327+
@Test
328+
void shouldLogCorrelationIdOnAppSyncEvent() throws IOException {
329+
RequestStreamHandler handler = new PowertoolsLogAppSyncCorrelationId();
330+
String eventId = "456";
331+
String event = "{\"request\":{\"headers\":{\"x-amzn-trace-id\":" + eventId + "}}}"; // CorrelationIdPath.APPSYNC_RESOLVER
332+
ByteArrayInputStream inputStream = new ByteArrayInputStream(event.getBytes());
333+
handler.handleRequest(inputStream, new ByteArrayOutputStream(), context);
321334

322335
assertThat(MDC.getCopyOfContextMap())
323336
.hasSize(EXPECTED_CONTEXT_SIZE + 1)

0 commit comments

Comments
 (0)