Skip to content

Commit

Permalink
Userexception logged as error in json public (#467)
Browse files Browse the repository at this point in the history
* Log user init and invoke exceptions as ERROR in JSON format (#49)

* RIC version bump to 2.4.2

---------

Co-authored-by: Daniel Torok <torokd@amazon.com>
  • Loading branch information
dtorok and Daniel Torok committed Feb 27, 2024
1 parent 3d8dfb6 commit d6b3b8b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
16 changes: 8 additions & 8 deletions aws-lambda-java-runtime-interface-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RUN mvn dependency:go-offline dependency:copy-dependencies

# compile the function
ADD . .
RUN mvn package
RUN mvn package

# copy the function artifact and dependencies onto a clean base
FROM base
Expand Down Expand Up @@ -70,7 +70,7 @@ pom.xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
</dependencies>
<build>
Expand Down Expand Up @@ -106,18 +106,18 @@ public class App {

### Local Testing

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.
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.

*To install the emulator and test your Lambda function*

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

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

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

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

3) Post an event to the following endpoint using a curl command:
3) Post an event to the following endpoint using a curl command:

```shell script
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
Expand Down Expand Up @@ -160,7 +160,7 @@ platform-specific JAR by setting the `<classifier>`.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
<classifier>linux-x86_64</classifier>
</dependency>
```
Expand Down
4 changes: 4 additions & 0 deletions aws-lambda-java-runtime-interface-client/RELEASE.CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Februray 27, 2024
`2.4.2`
- Exceptions caught by the runtime are logged as ERROR in JSON mode

### September 4, 2023
`2.4.1`
- Null pointer bugfix ([#439](https://github.com/aws/aws-lambda-java-libs/pull/439))
Expand Down
2 changes: 1 addition & 1 deletion aws-lambda-java-runtime-interface-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
<packaging>jar</packaging>

<name>AWS Lambda Java Runtime Interface Client</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static void main(String[] args) {

private static void startRuntime(String handler) {
try (LogSink logSink = createLogSink()) {
LambdaLogger logger = new LambdaContextLogger(
LambdaContextLogger logger = new LambdaContextLogger(
logSink,
LogLevel.fromString(LambdaEnvironment.LAMBDA_LOG_LEVEL),
LogFormat.fromString(LambdaEnvironment.LAMBDA_LOG_FORMAT)
Expand All @@ -200,7 +200,7 @@ private static void startRuntime(String handler) {
}
}

private static void startRuntime(String handler, LambdaLogger lambdaLogger) throws Throwable {
private static void startRuntime(String handler, LambdaContextLogger lambdaLogger) throws Throwable {
UnsafeUtil.disableIllegalAccessWarning();

System.setOut(new PrintStream(new LambdaOutputStream(System.out), false, "UTF-8"));
Expand All @@ -222,7 +222,7 @@ private static void startRuntime(String handler, LambdaLogger lambdaLogger) thro
try {
requestHandler = findRequestHandler(handler, customerClassLoader);
} catch (UserFault userFault) {
lambdaLogger.log(userFault.reportableError());
lambdaLogger.log(userFault.reportableError(), lambdaLogger.getLogFormat() == LogFormat.JSON ? LogLevel.ERROR : LogLevel.UNDEFINED);
reportInitError(new Failure(userFault), runtimeClient);
System.exit(1);
return;
Expand Down Expand Up @@ -265,13 +265,13 @@ private static void startRuntime(String handler, LambdaLogger lambdaLogger) thro
serializeAsXRayJson(t));
} finally {
if (userFault != null) {
lambdaLogger.log(userFault.reportableError());
lambdaLogger.log(userFault.reportableError(), lambdaLogger.getLogFormat() == LogFormat.JSON ? LogLevel.ERROR : LogLevel.UNDEFINED);
}
}
}
}

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

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

static void reportInitError(final Failure failure,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ public void log(byte[] message) {
public void setLambdaContext(LambdaContext lambdaContext) {
this.logFormatter.setLambdaContext(lambdaContext);
}

public LogFormat getLogFormat() {
return logFormat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
</dependencies>

Expand Down

0 comments on commit d6b3b8b

Please sign in to comment.