Skip to content

Commit

Permalink
Fix Log4j context attributes on AWS Lambda logger (#179)
Browse files Browse the repository at this point in the history
Fix a log4j exception when using the custom AWS Lambda logger and propagate the missing Restate and AWS request id context attributes to log4j MDC.
  • Loading branch information
pcholakov authored Dec 13, 2023
1 parent 9be7ced commit b80409f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 45 deletions.
3 changes: 0 additions & 3 deletions sdk-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ dependencies {
implementation(coreLibs.grpc.protobuf)
implementation(coreLibs.log4j.api)

// We don't want a hard-dependency on it
compileOnly(coreLibs.log4j.core)

implementation(platform(coreLibs.opentelemetry.bom))
implementation(coreLibs.opentelemetry.api)
implementation(coreLibs.opentelemetry.semconv)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.apache.logging.log4j.CloseableThreadContext;

/**
* Base implementation of a Lambda handler to execute restate services
Expand All @@ -27,6 +28,8 @@
public abstract class BaseRestateLambdaHandler
implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

private static final String AWS_REQUEST_ID = "AWSRequestId";

private final RestateLambdaEndpoint restateLambdaEndpoint;

protected BaseRestateLambdaHandler() {
Expand All @@ -41,6 +44,8 @@ protected BaseRestateLambdaHandler() {
@Override
public APIGatewayProxyResponseEvent handleRequest(
APIGatewayProxyRequestEvent input, Context context) {
return restateLambdaEndpoint.handleRequest(input, context);
try (var requestId = CloseableThreadContext.put(AWS_REQUEST_ID, context.getAwsRequestId())) {
return restateLambdaEndpoint.handleRequest(input, context);
}
}
}

0 comments on commit b80409f

Please sign in to comment.