Search in sources :

Example 1 with ChannelFutureListenerWithTracingAndMdc

use of com.nike.riposte.util.asynchelperwrapper.ChannelFutureListenerWithTracingAndMdc in project riposte by Nike-Inc.

the class AccessLogEndHandler method doAccessLogging.

protected void doAccessLogging(ChannelHandlerContext ctx) throws Exception {
    if (accessLogger == null)
        return;
    HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
    if (httpProcessingState == null) {
        runnableWithTracingAndMdc(() -> logger.warn("HttpProcessingState is null. This shouldn't happen."), ctx).run();
    }
    //      logging for this request, so make sure we only do it if appropriate
    if (httpProcessingState != null && !httpProcessingState.isAccessLogCompletedOrScheduled()) {
        Instant startTime = httpProcessingState.getRequestStartTime();
        ResponseInfo responseInfo = httpProcessingState.getResponseInfo();
        HttpResponse actualResponseObject = httpProcessingState.getActualResponseObject();
        RequestInfo requestInfo = httpProcessingState.getRequestInfo();
        ChannelFutureListener doTheAccessLoggingOperation = new ChannelFutureListenerWithTracingAndMdc((channelFuture) -> accessLogger.log(requestInfo, actualResponseObject, responseInfo, Instant.now().minusMillis(startTime.toEpochMilli()).toEpochMilli()), ctx);
        //      conditions), otherwise do it when the response finishes.
        if (!httpProcessingState.isResponseSendingLastChunkSent())
            doTheAccessLoggingOperation.operationComplete(null);
        else
            httpProcessingState.getResponseWriterFinalChunkChannelFuture().addListener(doTheAccessLoggingOperation);
        httpProcessingState.setAccessLogCompletedOrScheduled(true);
    }
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) Instant(java.time.Instant) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelFutureListenerWithTracingAndMdc(com.nike.riposte.util.asynchelperwrapper.ChannelFutureListenerWithTracingAndMdc) RequestInfo(com.nike.riposte.server.http.RequestInfo) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Aggregations

HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)1 RequestInfo (com.nike.riposte.server.http.RequestInfo)1 ResponseInfo (com.nike.riposte.server.http.ResponseInfo)1 ChannelFutureListenerWithTracingAndMdc (com.nike.riposte.util.asynchelperwrapper.ChannelFutureListenerWithTracingAndMdc)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 Instant (java.time.Instant)1