3
votes

I'm trying to implement a logging filter to log the request and response to the API endpoints of my Quarkus application. I'm using Quarkus 1.13.3.Final and quarkus-resteasy-reactive. I have a problem trying to log the request body when calling a non blocking end point. This is the code I'm using to log the request:

    @ServerRequestFilter(priority = 0)
    public void getFilter(UriInfo info,HttpServerRequest request,ContainerRequestContext ctx) {
        String uuid = UUID.randomUUID().toString();
        ctx.setProperty("log_id", uuid);
        String body = new BufferedReader(new InputStreamReader(ctx.getEntityStream())).lines().collect(Collectors.joining("\n"));
        ctx.setEntityStream(new ByteArrayInputStream(body.getBytes()));
        logger.info("Request: " + uuid + " Method: "+ ctx.getMethod() + " Path: " + info.getPath() + " Remote Address: " +  request.remoteAddress().toString() + " Body: " + body);
    }

This works fine when I'm calling an API endpoint that has the @Blocking annotations but when I call a non blocking API I get the following error:

2021-05-17 10:15:29,159 ERROR [org.jbo.res.rea.ser.cor.ExceptionMapping] (vert.x-eventloop-thread-10) Request failed : java.io.UncheckedIOException: java.io.IOException: Attempting a blocking read on io thread
        at java.base/java.io.BufferedReader$1.hasNext(BufferedReader.java:577)
        at java.base/java.util.Iterator.forEachRemaining(Iterator.java:132)
        at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
        at com.filters.LogginFilter.getFilter(LogginFilter.java:45)
        at com.filters.LogginFilter$GeneratedServerRequestFilter$getFilter.filter(LogginFilter$GeneratedServerRequestFilter$getFilter.zig:73)
        at com.filters.LogginFilter$GeneratedServerRequestFilter$getFilter_Subclass.filter$$superaccessor1(LogginFilter$GeneratedServerRequestFilter$getFilter_Subclass.zig:201)
        at com.filters.LogginFilter$GeneratedServerRequestFilter$getFilter_Subclass$$function$$3.apply(LogginFilter$GeneratedServerRequestFilter$getFilter_Subclass$$function$$3.zig:33)
        at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
        at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.proceed(InvocationInterceptor.java:63)
        at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.monitor(InvocationInterceptor.java:49)
        at io.quarkus.arc.runtime.devconsole.InvocationInterceptor_Bean.intercept(InvocationInterceptor_Bean.zig:521)
        at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
        at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
        at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)

Is there a way to get around this and read the body of the request in a non blocking way? or another way to log the request body?

1

1 Answers

1
votes

You will need to force RESTEasy Reactive all JAX-RS methods on a worker thread instead of the event loop.

To do that, just change your code to:

@io.smallrye.common.annotation.Blocking
public class MyApplication extends javax.ws.rs.core.Application {

}