How to Log AWSRequestId with Micronaut Lambda Application Native Image

621 Views Asked by At

Based on this guide about logging AWSRequestId, I can add %X{AWSRequestId} on my logback.xml to print the request id on my lambda function. On the example, the method that adds this key on MDC is populateMappingDiagnosticContextValues of class MicronautRequestHandler. But since my lambda function has two endpoints, I created it as an Application type and it says it's using the MicronautLambdaHandler as the handler class. guide

What I did is I created my custom handler that extends the MicronautLambdaHandler and added the populateMappingDiagnosticContextValues method.

@Override
public AwsProxyResponse handleRequest(AwsProxyRequest input, Context context) {
    if (context != null) {
        populateMappingDiagnosticContextValues(context);
    }
    return handler.proxy(input, context);
}

logback.xml

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <withJansi>false</withJansi>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1} - %m%n</pattern>
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

On the lambda setup I pointed the handler on my custom handler and the app is working. Except that the logs is not showing the AWSRequestId. Is this functionality only works on MicronautRequestHandler class and not on MicronautLambdaHandler?

I also tried the aws guide here that uses log4j2 for logging the request id. But log4j doesn't work when building the GraalVM native image, that's why I sticked to logback.

1

There are 1 best solutions below

0
On

If you copied populateMappingDiagnosticContextValues and mdcput into your Handler (I suppose you did it) it should work without any additional changes.