Skip to content

Commit

Permalink
AccessLogger for http server (#3339)
Browse files Browse the repository at this point in the history
* AccessLogger for httpserver
  • Loading branch information
croudet38 authored May 27, 2020
1 parent 7efb0f0 commit 61b6be2
Show file tree
Hide file tree
Showing 55 changed files with 3,589 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public interface ChannelPipelineCustomizer {
String HANDLER_HTTP2_PROTOCOL_NEGOTIATOR = "http2-protocol-negotiator";
String HANDLER_WEBSOCKET_UPGRADE = "websocket-upgrade-handler";
String HANDLER_MICRONAUT_INBOUND = "micronaut-inbound-handler";
String HANDLER_ACCESS_LOGGER = "http-access-logger";

/**
* @return Is this customizer the client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import io.micronaut.http.server.netty.configuration.NettyHttpServerConfiguration;
import io.micronaut.http.server.netty.decoders.HttpRequestDecoder;
import io.micronaut.http.server.netty.encoders.HttpResponseEncoder;
import io.micronaut.http.server.netty.handler.accesslog.HttpAccessLogHandler;
import io.micronaut.http.server.netty.ssl.HttpRequestCertificateHandler;
import io.micronaut.http.server.netty.ssl.ServerSslBuilder;
import io.micronaut.http.server.netty.types.NettyCustomizableResponseTypeHandlerRegistry;
Expand Down Expand Up @@ -603,7 +604,6 @@ private HttpToHttp2ConnectionHandler newHttpToHttp2ConnectionHandler() {
serverConfiguration.isValidateHeaders(),
true
);

final HttpToHttp2ConnectionHandlerBuilder builder = new HttpToHttp2ConnectionHandlerBuilder()
.frameListener(http2ToHttpAdapter);

Expand All @@ -630,7 +630,9 @@ public void doOnConnect(@NonNull ChannelPipelineListener listener) {
@ChannelHandler.Sharable
private final class Http2OrHttpHandler extends ApplicationProtocolNegotiationHandler {
private final boolean useSsl;
// both are Sharable
// all are Sharable
final HttpAccessLogHandler accessLogHandler = serverConfiguration.getAccessLogger() != null && serverConfiguration.getAccessLogger().isEnabled() ?
new HttpAccessLogHandler(serverConfiguration.getAccessLogger().getLoggerName(), serverConfiguration.getAccessLogger().getLogFormat()) : null;
final HttpRequestDecoder requestDecoder = new HttpRequestDecoder(NettyHttpServer.this, environment, serverConfiguration);
final HttpResponseEncoder responseDecoder = new HttpResponseEncoder(mediaTypeCodecRegistry, serverConfiguration);

Expand Down Expand Up @@ -688,11 +690,17 @@ private Map<String, ChannelHandler> getHandlerForProtocol(@Nullable String proto
}
if (protocol == null) {
handlers.put(ChannelPipelineCustomizer.HANDLER_FLOW_CONTROL, new FlowControlHandler());
if (accessLogHandler != null) {
handlers.put(HANDLER_ACCESS_LOGGER, accessLogHandler);
}
} else if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
final HttpToHttp2ConnectionHandler httpToHttp2ConnectionHandler = newHttpToHttp2ConnectionHandler();
handlers.put(HANDLER_HTTP2_CONNECTION, httpToHttp2ConnectionHandler);
registerMicronautChannelHandlers(handlers);
handlers.put(ChannelPipelineCustomizer.HANDLER_FLOW_CONTROL, new FlowControlHandler());
if (accessLogHandler != null) {
handlers.put(HANDLER_ACCESS_LOGGER, accessLogHandler);
}
} else {
handlers.put(HANDLER_HTTP_SERVER_CODEC, new HttpServerCodec(
serverConfiguration.getMaxInitialLineLength(),
Expand All @@ -701,13 +709,15 @@ private Map<String, ChannelHandler> getHandlerForProtocol(@Nullable String proto
serverConfiguration.isValidateHeaders(),
serverConfiguration.getInitialBufferSize()
));
if (accessLogHandler != null) {
handlers.put(HANDLER_ACCESS_LOGGER, accessLogHandler);
}
registerMicronautChannelHandlers(handlers);
handlers.put(HANDLER_FLOW_CONTROL, new FlowControlHandler());
handlers.put(HANDLER_HTTP_KEEP_ALIVE, new HttpServerKeepAliveHandler());
handlers.put(HANDLER_HTTP_COMPRESSOR, new SmartHttpContentCompressor(httpCompressionStrategy));
handlers.put(HANDLER_HTTP_DECOMPRESSOR, new HttpContentDecompressor());
}

handlers.put(HANDLER_HTTP_STREAM, new HttpStreamsServerHandler());
handlers.put(HANDLER_HTTP_CHUNK, new ChunkedWriteHandler());
handlers.put(HttpRequestDecoder.ID, requestDecoder);
Expand Down Expand Up @@ -787,7 +797,6 @@ public void upgradeTo(ChannelHandlerContext ctx, FullHttpRequest upgradeRequest)
new CleartextHttp2ServerUpgradeHandler(sourceCodec, upgradeHandler, connectionHandler);

pipeline.addLast(cleartextHttp2ServerUpgradeHandler);

pipeline.addLast(fallbackHandlerName, new SimpleChannelInboundHandler<HttpMessage>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public class NettyHttpServerConfiguration extends HttpServerConfiguration {
private int compressionLevel = DEFAULT_COMPRESSIONLEVEL;
private boolean useNativeTransport = DEFAULT_USE_NATIVE_TRANSPORT;
private String fallbackProtocol = ApplicationProtocolNames.HTTP_1_1;
private AccessLogger accessLogger;

/**
* Default empty constructor.
Expand Down Expand Up @@ -141,6 +142,22 @@ public NettyHttpServerConfiguration(
this.pipelineCustomizers = pipelineCustomizers;
}

/**
* Returns the AccessLogger configuration.
* @return The AccessLogger configuration.
*/
public AccessLogger getAccessLogger() {
return accessLogger;
}

/**
* Sets the AccessLogger configuration.
* @param accessLogger The configuration .
*/
public void setAccessLogger(AccessLogger accessLogger) {
this.accessLogger = accessLogger;
}

/**
* @return The pipeline customizers
*/
Expand Down Expand Up @@ -402,6 +419,65 @@ public void setCompressionLevel(@ReadableBytes int compressionLevel) {
this.compressionLevel = compressionLevel;
}

/**
* Access logger configuration.
*/
@ConfigurationProperties("access-logger")
public static class AccessLogger {
private boolean enabled;
private String loggerName;
private String logFormat;

/**
* Returns whether the access logger is enabled.
* @return Whether the access logger is enabled.
*/
public boolean isEnabled() {
return enabled;
}

/**
* Enables or Disables the access logger.
* @param enabled The flag.
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

/**
* The logger name to use. Access logs will be logged at info level.
* @return The logger name.
*/
public String getLoggerName() {
return loggerName;
}

/**
* Sets the logger name to use. If not specified 'HTTP_ACCESS_LOGGER' will be used.
* @param loggerName A logger name,
*/
public void setLoggerName(String loggerName) {
this.loggerName = loggerName;
}

/**
* Returns the log format to use.
* @return The log format.
*/
public String getLogFormat() {
return logFormat;
}

/**
* Sets the log format to use. When not specified, the Common Log Format (CLF) will be used.
* @param logFormat The log format.
*/
public void setLogFormat(String logFormat) {
this.logFormat = logFormat;
}

}

/**
* Configuration for Netty worker.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http.server.netty.handler.accesslog;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.micronaut.http.server.netty.handler.accesslog.element.AccessLogFormatParser;
import io.micronaut.http.server.netty.handler.accesslog.element.AccessLog;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http2.Http2Exception;
import io.netty.handler.codec.http2.HttpConversionUtil.ExtensionHeaderNames;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

/**
* Logging handler for HTTP access logs.
* Access logs will be logged at info level.
*
* @author croudet
* @since 2.0
*/
@Sharable
public class HttpAccessLogHandler extends ChannelDuplexHandler {
/**
* The default logger name.
*/
public static final String HTTP_ACCESS_LOGGER = "HTTP_ACCESS_LOGGER";

private static final AttributeKey<AccessLog> ACCESS_LOGGER = AttributeKey.valueOf("ACCESS_LOGGER");
private static final String H2_PROTOCOL_NAME = "HTTP/2.0";

private final Logger logger;
private final AccessLogFormatParser accessLogFormatParser;

/**
* Creates a HttpAccessLogHandler.
*
* @param loggerName A logger name.
* @param spec The log format specification.
*/
public HttpAccessLogHandler(String loggerName, String spec) {
this(loggerName == null || loggerName.isEmpty() ? null : LoggerFactory.getLogger(loggerName), spec);
}

/**
* Creates a HttpAccessLogHandler.
*
* @param logger A logger. Will log at info level.
* @param spec The log format specification.
*/
public HttpAccessLogHandler(Logger logger, String spec) {
super();
this.logger = logger == null ? LoggerFactory.getLogger(HTTP_ACCESS_LOGGER) : logger;
this.accessLogFormatParser = new AccessLogFormatParser(spec);
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Http2Exception {
if (logger.isInfoEnabled() && msg instanceof HttpRequest) {
final SocketChannel channel = (SocketChannel) ctx.channel();
final HttpRequest request = (HttpRequest) msg;
final HttpHeaders headers = request.headers();
// Trying to detect http/2
String protocol;
if (headers.contains(ExtensionHeaderNames.STREAM_ID.text()) || headers.contains(ExtensionHeaderNames.SCHEME.text())) {
protocol = H2_PROTOCOL_NAME;
} else {
protocol = request.protocolVersion().text();
}
accessLog(channel).onRequestHeaders(channel, request.method().name(), request.headers(), request.uri(), protocol);
}
ctx.fireChannelRead(msg);
}

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (logger.isInfoEnabled()) {
processWriteEvent(ctx, msg, promise);
} else {
super.write(ctx, msg, promise);
}
}

private AccessLog accessLog(SocketChannel channel) {
final Attribute<AccessLog> attr = channel.attr(ACCESS_LOGGER);
AccessLog accessLog = attr.get();
if (accessLog == null) {
accessLog = accessLogFormatParser.newAccessLogger();
attr.set(accessLog);
} else {
accessLog.reset();
}
return accessLog;
}

private void log(ChannelHandlerContext ctx, Object msg, ChannelPromise promise, AccessLog accessLog) {
ctx.write(msg, promise.unvoid()).addListener(future -> {
if (future.isSuccess()) {
accessLog.log(logger);
}
});
}

private static boolean processHttpResponse(HttpResponse response, AccessLog accessLogger, ChannelHandlerContext ctx, ChannelPromise promise) {
final HttpResponseStatus status = response.status();
if (status.equals(HttpResponseStatus.CONTINUE)) {
ctx.write(response, promise);
return true;
}
accessLogger.onResponseHeaders(ctx, response.headers(), status.codeAsText().toString());
return false;
}

private void processWriteEvent(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
final AccessLog accessLogger = ctx.channel().attr(ACCESS_LOGGER).get();
if (accessLogger != null) {
if (msg instanceof HttpResponse && processHttpResponse((HttpResponse) msg, accessLogger, ctx, promise)) {
return;
}
if (msg instanceof LastHttpContent) {
accessLogger.onLastResponseWrite(((LastHttpContent) msg).content().readableBytes());
log(ctx, msg, promise, accessLogger);
return;
} else if (msg instanceof ByteBufHolder) {
accessLogger.onResponseWrite(((ByteBufHolder) msg).content().readableBytes());
} else if (msg instanceof ByteBuf) {
accessLogger.onResponseWrite(((ByteBuf) msg).readableBytes());
}
}
super.write(ctx, msg, promise);
}
}
Loading

0 comments on commit 61b6be2

Please sign in to comment.