Skip to content

Commit 46587fc

Browse files
pankajagrawal16jeromevdl
authored andcommitted
feat: log aws request id (aws-powertools#133)
1 parent 27d608f commit 46587fc

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

docs/content/core/logging.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Key | Type | Example | Description
7070
**functionMemorySize**| String | "128"
7171
**functionArn**| String | "arn:aws:lambda:eu-west-1:012345678910:function:example-powertools-HelloWorldFunction-1P1Z6B39FLU73"
7272
**xray_trace_id**| String | "1-5759e988-bd862e3fe1be46a994272793" | X-Ray Trace ID when Lambda function has enabled Tracing
73+
**function_request_id**| String | "899856cb-83d1-40d7-8611-9e78f15f32f4"" | AWS Request ID from lambda context
7374

7475
## Capturing context Lambda info
7576

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ enum DefaultLambdaFields {
2222
FUNCTION_NAME("functionName"),
2323
FUNCTION_VERSION("functionVersion"),
2424
FUNCTION_ARN("functionArn"),
25-
FUNCTION_MEMORY_SIZE("functionMemorySize");
25+
FUNCTION_MEMORY_SIZE("functionMemorySize"),
26+
FUNCTION_REQUEST_ID("function_request_id");
2627

2728
private final String name;
2829

@@ -41,6 +42,7 @@ static Map<String, String> values(Context context) {
4142
hashMap.put(FUNCTION_VERSION.name, context.getFunctionVersion());
4243
hashMap.put(FUNCTION_ARN.name, context.getInvokedFunctionArn());
4344
hashMap.put(FUNCTION_MEMORY_SIZE.name, String.valueOf(context.getMemoryLimitInMB()));
45+
hashMap.put(FUNCTION_REQUEST_ID.name, String.valueOf(context.getAwsRequestId()));
4446

4547
return hashMap;
4648
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
class LambdaLoggingAspectTest {
7171

72-
private static final int EXPECTED_CONTEXT_SIZE = 7;
72+
private static final int EXPECTED_CONTEXT_SIZE = 8;
7373
private RequestStreamHandler requestStreamHandler;
7474
private RequestHandler<Object, Object> requestHandler;
7575

@@ -99,6 +99,7 @@ void shouldSetLambdaContextWhenEnabled() {
9999
.containsEntry(DefaultLambdaFields.FUNCTION_MEMORY_SIZE.getName(), "10")
100100
.containsEntry(DefaultLambdaFields.FUNCTION_VERSION.getName(), "1")
101101
.containsEntry(DefaultLambdaFields.FUNCTION_NAME.getName(), "testFunction")
102+
.containsEntry(DefaultLambdaFields.FUNCTION_REQUEST_ID.getName(), "RequestId")
102103
.containsKey("coldStart")
103104
.containsKey("service");
104105
}
@@ -115,6 +116,7 @@ void shouldSetLambdaContextForStreamHandlerWhenEnabled() throws IOException {
115116
.containsEntry(DefaultLambdaFields.FUNCTION_MEMORY_SIZE.getName(), "10")
116117
.containsEntry(DefaultLambdaFields.FUNCTION_VERSION.getName(), "1")
117118
.containsEntry(DefaultLambdaFields.FUNCTION_NAME.getName(), "testFunction")
119+
.containsEntry(DefaultLambdaFields.FUNCTION_REQUEST_ID.getName(), "RequestId")
118120
.containsKey("coldStart")
119121
.containsKey("service");
120122
}
@@ -232,6 +234,7 @@ private void setupContext() {
232234
when(context.getInvokedFunctionArn()).thenReturn("testArn");
233235
when(context.getFunctionVersion()).thenReturn("1");
234236
when(context.getMemoryLimitInMB()).thenReturn(10);
237+
when(context.getAwsRequestId()).thenReturn("RequestId");
235238
}
236239

237240
private void resetLogLevel(Level level) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

0 commit comments

Comments
 (0)