Skip to content

Commit d6b3b8b

Browse files
authored
Userexception logged as error in json public (aws#467)
* Log user init and invoke exceptions as ERROR in JSON format (aws#49) * RIC version bump to 2.4.2 --------- Co-authored-by: Daniel Torok <[email protected]>
1 parent 3d8dfb6 commit d6b3b8b

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed

aws-lambda-java-runtime-interface-client/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ RUN mvn dependency:go-offline dependency:copy-dependencies
3737

3838
# compile the function
3939
ADD . .
40-
RUN mvn package
40+
RUN mvn package
4141

4242
# copy the function artifact and dependencies onto a clean base
4343
FROM base
@@ -70,7 +70,7 @@ pom.xml
7070
<dependency>
7171
<groupId>com.amazonaws</groupId>
7272
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
73-
<version>2.4.1</version>
73+
<version>2.4.2</version>
7474
</dependency>
7575
</dependencies>
7676
<build>
@@ -106,18 +106,18 @@ public class App {
106106

107107
### Local Testing
108108

109-
To make it easy to locally test Lambda functions packaged as container images we open-sourced a lightweight web-server, Lambda Runtime Interface Emulator (RIE), which allows your function packaged as a container image to accept HTTP requests. You can install the [AWS Lambda Runtime Interface Emulator](https://github.com/aws/aws-lambda-runtime-interface-emulator) on your local machine to test your function. Then when you run the image function, you set the entrypoint to be the emulator.
109+
To make it easy to locally test Lambda functions packaged as container images we open-sourced a lightweight web-server, Lambda Runtime Interface Emulator (RIE), which allows your function packaged as a container image to accept HTTP requests. You can install the [AWS Lambda Runtime Interface Emulator](https://github.com/aws/aws-lambda-runtime-interface-emulator) on your local machine to test your function. Then when you run the image function, you set the entrypoint to be the emulator.
110110

111111
*To install the emulator and test your Lambda function*
112112

113-
1) Run the following command to download the RIE from GitHub and install it on your local machine.
113+
1) Run the following command to download the RIE from GitHub and install it on your local machine.
114114

115115
```shell script
116116
mkdir -p ~/.aws-lambda-rie && \
117117
curl -Lo ~/.aws-lambda-rie/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && \
118118
chmod +x ~/.aws-lambda-rie/aws-lambda-rie
119119
```
120-
2) Run your Lambda image function using the docker run command.
120+
2) Run your Lambda image function using the docker run command.
121121

122122
```shell script
123123
docker run -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \
@@ -126,9 +126,9 @@ docker run -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \
126126
/usr/bin/java -cp './*' com.amazonaws.services.lambda.runtime.api.client.AWSLambda example.App::sayHello
127127
```
128128

129-
This runs the image as a container and starts up an endpoint locally at `http://localhost:9000/2015-03-31/functions/function/invocations`.
129+
This runs the image as a container and starts up an endpoint locally at `http://localhost:9000/2015-03-31/functions/function/invocations`.
130130

131-
3) Post an event to the following endpoint using a curl command:
131+
3) Post an event to the following endpoint using a curl command:
132132

133133
```shell script
134134
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
@@ -160,7 +160,7 @@ platform-specific JAR by setting the `<classifier>`.
160160
<dependency>
161161
<groupId>com.amazonaws</groupId>
162162
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
163-
<version>2.4.1</version>
163+
<version>2.4.2</version>
164164
<classifier>linux-x86_64</classifier>
165165
</dependency>
166166
```

aws-lambda-java-runtime-interface-client/RELEASE.CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Februray 27, 2024
2+
`2.4.2`
3+
- Exceptions caught by the runtime are logged as ERROR in JSON mode
4+
15
### September 4, 2023
26
`2.4.1`
37
- Null pointer bugfix ([#439](https://github.com/aws/aws-lambda-java-libs/pull/439))

aws-lambda-java-runtime-interface-client/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.amazonaws</groupId>
66
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
7-
<version>2.4.1</version>
7+
<version>2.4.2</version>
88
<packaging>jar</packaging>
99

1010
<name>AWS Lambda Java Runtime Interface Client</name>

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/AWSLambda.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public static void main(String[] args) {
189189

190190
private static void startRuntime(String handler) {
191191
try (LogSink logSink = createLogSink()) {
192-
LambdaLogger logger = new LambdaContextLogger(
192+
LambdaContextLogger logger = new LambdaContextLogger(
193193
logSink,
194194
LogLevel.fromString(LambdaEnvironment.LAMBDA_LOG_LEVEL),
195195
LogFormat.fromString(LambdaEnvironment.LAMBDA_LOG_FORMAT)
@@ -200,7 +200,7 @@ private static void startRuntime(String handler) {
200200
}
201201
}
202202

203-
private static void startRuntime(String handler, LambdaLogger lambdaLogger) throws Throwable {
203+
private static void startRuntime(String handler, LambdaContextLogger lambdaLogger) throws Throwable {
204204
UnsafeUtil.disableIllegalAccessWarning();
205205

206206
System.setOut(new PrintStream(new LambdaOutputStream(System.out), false, "UTF-8"));
@@ -222,7 +222,7 @@ private static void startRuntime(String handler, LambdaLogger lambdaLogger) thro
222222
try {
223223
requestHandler = findRequestHandler(handler, customerClassLoader);
224224
} catch (UserFault userFault) {
225-
lambdaLogger.log(userFault.reportableError());
225+
lambdaLogger.log(userFault.reportableError(), lambdaLogger.getLogFormat() == LogFormat.JSON ? LogLevel.ERROR : LogLevel.UNDEFINED);
226226
reportInitError(new Failure(userFault), runtimeClient);
227227
System.exit(1);
228228
return;
@@ -265,13 +265,13 @@ private static void startRuntime(String handler, LambdaLogger lambdaLogger) thro
265265
serializeAsXRayJson(t));
266266
} finally {
267267
if (userFault != null) {
268-
lambdaLogger.log(userFault.reportableError());
268+
lambdaLogger.log(userFault.reportableError(), lambdaLogger.getLogFormat() == LogFormat.JSON ? LogLevel.ERROR : LogLevel.UNDEFINED);
269269
}
270270
}
271271
}
272272
}
273273

274-
static void onInitComplete(final LambdaRuntimeClient runtimeClient, final LambdaLogger lambdaLogger) throws IOException {
274+
static void onInitComplete(final LambdaRuntimeClient runtimeClient, final LambdaContextLogger lambdaLogger) throws IOException {
275275
try {
276276
Core.getGlobalContext().beforeCheckpoint(null);
277277
// Blocking call to RAPID /restore/next API, will return after taking snapshot.
@@ -292,10 +292,10 @@ static void onInitComplete(final LambdaRuntimeClient runtimeClient, final Lambda
292292
}
293293
}
294294

295-
private static void logExceptionCloudWatch(LambdaLogger lambdaLogger, Exception exc) {
295+
private static void logExceptionCloudWatch(LambdaContextLogger lambdaLogger, Exception exc) {
296296
UserFault.filterStackTrace(exc);
297297
UserFault userFault = UserFault.makeUserFault(exc, true);
298-
lambdaLogger.log(userFault.reportableError());
298+
lambdaLogger.log(userFault.reportableError(), lambdaLogger.getLogFormat() == LogFormat.JSON ? LogLevel.ERROR : LogLevel.UNDEFINED);
299299
}
300300

301301
static void reportInitError(final Failure failure,

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/logging/AbstractLambdaLogger.java

+4
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ public void log(byte[] message) {
6565
public void setLambdaContext(LambdaContext lambdaContext) {
6666
this.logFormatter.setLambdaContext(lambdaContext);
6767
}
68+
69+
public LogFormat getLogFormat() {
70+
return logFormat;
71+
}
6872
}

aws-lambda-java-runtime-interface-client/test/integration/test-handler/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<dependency>
1616
<groupId>com.amazonaws</groupId>
1717
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
18-
<version>2.4.1</version>
18+
<version>2.4.2</version>
1919
</dependency>
2020
</dependencies>
2121

0 commit comments

Comments
 (0)