Skip to content

Commit

Permalink
Cleaning up logs
Browse files Browse the repository at this point in the history
  • Loading branch information
violetagg committed Oct 6, 2022
1 parent 45f6ce8 commit 1afda75
Show file tree
Hide file tree
Showing 31 changed files with 1,705 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2011-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -528,6 +528,17 @@ else if (err instanceof OutOfMemoryError) {
}
}

/**
* Transforms the object to a string for debug logs.
*
* @param o the object to be transformed
* @return the string to be logged
* @since 1.0.24
*/
protected String asDebugLogMessage(Object o) {
return o.toString();
}

@Override
public boolean isPersistent() {
return connection.isPersistent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import reactor.util.Loggers;

import static reactor.netty.ReactorNetty.format;
import static reactor.netty.ReactorNetty.toPrettyHexDump;

/**
* Netty {@link io.netty.channel.ChannelDuplexHandler} implementation that bridge data
Expand Down Expand Up @@ -118,13 +117,11 @@ final public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof DecoderResultProvider) {
DecoderResult decoderResult = ((DecoderResultProvider) msg).decoderResult();
if (decoderResult.isFailure()) {
log.debug(format(ctx.channel(), "Decoding failed: " + msg + " : "),
decoderResult.cause());
log.debug(format(ctx.channel(), "Decoding failed."), decoderResult.cause());
}
}

log.debug(format(ctx.channel(), "No ChannelOperation attached. Dropping: {}"),
toPrettyHexDump(msg));
log.debug(format(ctx.channel(), "No ChannelOperation attached."));
}
ReferenceCountUtil.release(msg);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2011-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -203,7 +203,7 @@ final void cleanQueue(@Nullable Queue<Object> q) {
Object o;
while ((o = q.poll()) != null) {
if (log.isDebugEnabled()) {
log.debug(format(channel, "{}: dropping frame {}"), this, o);
log.debug(format(channel, "{}: dropping frame {}"), this, parent.asDebugLogMessage(o));
}
ReferenceCountUtil.release(o);
}
Expand Down Expand Up @@ -343,7 +343,7 @@ else if (!needRead) {
final void onInboundNext(Object msg) {
if (inboundDone || isCancelled()) {
if (log.isDebugEnabled()) {
log.debug(format(channel, "{}: dropping frame {}"), this, msg);
log.debug(format(channel, "{}: dropping frame {}"), this, parent.asDebugLogMessage(msg));
}
ReferenceCountUtil.release(msg);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.netty.handler.codec.http.FullHttpMessage;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.ReferenceCountUtil;
Expand All @@ -52,12 +53,13 @@
import reactor.netty.ReactorNetty;
import reactor.netty.channel.AbortedException;
import reactor.netty.channel.ChannelOperations;
import reactor.netty.http.logging.HttpMessageArgProviderFactory;
import reactor.netty.http.logging.HttpMessageLogFactory;
import reactor.util.Logger;
import reactor.util.Loggers;
import reactor.util.annotation.Nullable;

import static reactor.netty.ReactorNetty.format;
import static reactor.netty.ReactorNetty.toPrettyHexDump;

/**
* An HTTP ready {@link ChannelOperations} with state management for status and headers
Expand All @@ -74,13 +76,17 @@ public abstract class HttpOperations<INBOUND extends NettyInbound, OUTBOUND exte
static final int HEADERS_SENT = 1;
static final int BODY_SENT = 2;

final HttpMessageLogFactory httpMessageLogFactory;

protected HttpOperations(HttpOperations<INBOUND, OUTBOUND> replaced) {
super(replaced);
this.httpMessageLogFactory = replaced.httpMessageLogFactory;
this.statusAndHeadersSent = replaced.statusAndHeadersSent;
}

protected HttpOperations(Connection connection, ConnectionObserver listener) {
protected HttpOperations(Connection connection, ConnectionObserver listener, HttpMessageLogFactory httpMessageLogFactory) {
super(connection, listener);
this.httpMessageLogFactory = httpMessageLogFactory;
}

/**
Expand Down Expand Up @@ -122,7 +128,7 @@ public NettyOutbound send(Publisher<? extends ByteBuf> source) {
if (HttpUtil.getContentLength(outboundHttpMessage(), -1) == 0) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Dropped HTTP content, " +
"since response has Content-Length: 0 {}"), toPrettyHexDump(msg));
"since response has Content-Length: 0 {}"), msg);
}
msg.release();
return FutureMono.from(channel().writeAndFlush(newFullBodyMessage(Unpooled.EMPTY_BUFFER)));
Expand Down Expand Up @@ -158,7 +164,7 @@ public NettyOutbound sendObject(Object message) {
if (HttpUtil.getContentLength(outboundHttpMessage(), -1) == 0) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Dropped HTTP content, " +
"since response has Content-Length: 0 {}"), toPrettyHexDump(b));
"since response has Content-Length: 0 {}"), b);
}
b.release();
return channel().writeAndFlush(newFullBodyMessage(Unpooled.EMPTY_BUFFER));
Expand Down Expand Up @@ -215,6 +221,17 @@ public Mono<Void> then() {
});
}

@Override
protected String asDebugLogMessage(Object o) {
return o instanceof HttpObject ?
httpMessageLogFactory.debug(HttpMessageArgProviderFactory.create(o)) :
o.toString();
}

protected HttpMessageLogFactory httpMessageLogFactory() {
return httpMessageLogFactory;
}

protected abstract void beforeMarkSentHeaders();

protected abstract void afterMarkSentHeaders();
Expand Down Expand Up @@ -249,10 +266,10 @@ else if (!HttpUtil.isContentLengthSet(outboundHttpMessage())) {
@Override
public String toString() {
if (isWebsocket()) {
return "ws{uri=" + uri() + ", connection=" + connection() + "}";
return "ws{uri=" + fullPath() + ", connection=" + connection() + "}";
}

return method().name() + "{uri=" + uri() + ", connection=" + connection() + "}";
return method().name() + "{uri=" + fullPath() + ", connection=" + connection() + "}";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import java.util.function.Supplier;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
Expand All @@ -52,6 +54,8 @@
import reactor.netty.channel.ChannelMetricsRecorder;
import reactor.netty.http.Http2SettingsSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.logging.HttpMessageLogFactory;
import reactor.netty.http.logging.ReactorNettyHttpMessageLogFactory;
import reactor.netty.http.websocket.WebsocketInbound;
import reactor.netty.http.websocket.WebsocketOutbound;
import reactor.netty.resources.ConnectionProvider;
Expand Down Expand Up @@ -1072,6 +1076,28 @@ public final HttpClient http2Settings(Consumer<Http2SettingsSpec.Builder> http2S
return dup;
}

/**
* When {@link HttpMessage} is about to be logged the configured factory will be used for
* generating a sanitized log message.
* <p>
* Default to {@link ReactorNettyHttpMessageLogFactory}:
* <ul>
* <li>hides the query from the uri</li>
* <li>hides the headers values</li>
* <li>only {@link DecoderException} message is presented</li>
* </ul>
*
* @param httpMessageLogFactory the factory for generating the log message
* @return a new {@link HttpClient}
* @since 1.0.24
*/
public final HttpClient httpMessageLogFactory(HttpMessageLogFactory httpMessageLogFactory) {
Objects.requireNonNull(httpMessageLogFactory, "httpMessageLogFactory");
HttpClient dup = duplicate();
dup.configuration().httpMessageLogFactory = httpMessageLogFactory;
return dup;
}

/**
* Configure the {@link io.netty.handler.codec.http.HttpClientCodec}'s response decoding options.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
import reactor.netty.http.Http2SettingsSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.HttpResources;
import reactor.netty.http.logging.HttpMessageLogFactory;
import reactor.netty.http.logging.ReactorNettyHttpMessageLogFactory;
import reactor.netty.resources.ConnectionProvider;
import reactor.netty.resources.LoopResources;
import reactor.netty.tcp.SslProvider;
Expand Down Expand Up @@ -112,7 +114,7 @@ public int channelHash() {

@Override
public ChannelOperations.OnSetup channelOperationsProvider() {
return (ch, c, msg) -> new HttpClientOperations(ch, c, cookieEncoder, cookieDecoder);
return (ch, c, msg) -> new HttpClientOperations(ch, c, cookieEncoder, cookieDecoder, httpMessageLogFactory);
}

@Override
Expand Down Expand Up @@ -313,6 +315,7 @@ public WebsocketClientSpec websocketClientSpec() {
BiPredicate<HttpClientRequest, HttpClientResponse> followRedirectPredicate;
HttpHeaders headers;
Http2SettingsSpec http2Settings;
HttpMessageLogFactory httpMessageLogFactory;
HttpMethod method;
HttpProtocol[] protocols;
int _protocols;
Expand All @@ -334,6 +337,7 @@ public WebsocketClientSpec websocketClientSpec() {
this.cookieEncoder = ClientCookieEncoder.STRICT;
this.decoder = new HttpResponseDecoderSpec();
this.headers = new DefaultHttpHeaders();
this.httpMessageLogFactory = ReactorNettyHttpMessageLogFactory.INSTANCE;
this.method = HttpMethod.GET;
this.protocols = new HttpProtocol[]{HttpProtocol.HTTP11};
this._protocols = h11;
Expand All @@ -360,6 +364,7 @@ public WebsocketClientSpec websocketClientSpec() {
this.followRedirectPredicate = parent.followRedirectPredicate;
this.headers = parent.headers;
this.http2Settings = parent.http2Settings;
this.httpMessageLogFactory = parent.httpMessageLogFactory;
this.method = parent.method;
this.protocols = parent.protocols;
this._protocols = parent._protocols;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
Expand Down Expand Up @@ -83,6 +82,8 @@
import reactor.netty.channel.ChannelOperations;
import reactor.netty.http.Cookies;
import reactor.netty.http.HttpOperations;
import reactor.netty.http.logging.HttpMessageArgProviderFactory;
import reactor.netty.http.logging.HttpMessageLogFactory;
import reactor.util.Logger;
import reactor.util.Loggers;
import reactor.util.annotation.Nullable;
Expand Down Expand Up @@ -144,8 +145,9 @@ class HttpClientOperations extends HttpOperations<NettyInbound, NettyOutbound>
this.trailerHeaders = replaced.trailerHeaders;
}

HttpClientOperations(Connection c, ConnectionObserver listener, ClientCookieEncoder encoder, ClientCookieDecoder decoder) {
super(c, listener);
HttpClientOperations(Connection c, ConnectionObserver listener, ClientCookieEncoder encoder,
ClientCookieDecoder decoder, HttpMessageLogFactory httpMessageLogFactory) {
super(c, listener, httpMessageLogFactory);
this.isSecure = c.channel()
.pipeline()
.get(NettyPipeline.SslHandler) != null;
Expand Down Expand Up @@ -446,7 +448,7 @@ public NettyOutbound send(Publisher<? extends ByteBuf> source) {
}
for (ByteBuf bb : list) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Ignoring accumulated bytebuf on http GET {}"), ByteBufUtil.prettyHexDump(bb));
log.debug(format(channel(), "Ignoring accumulated bytebuf on http GET {}"), bb);
}
bb.release();
}
Expand Down Expand Up @@ -607,8 +609,7 @@ protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
if (started) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "HttpClientOperations cannot proceed more than one response {}"),
response.headers()
.toString());
httpMessageLogFactory().debug(HttpMessageArgProviderFactory.create(response)));
}
ReferenceCountUtil.release(msg);
return;
Expand All @@ -627,10 +628,8 @@ protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {

if (log.isDebugEnabled()) {
log.debug(format(channel(), "Received response (auto-read:{}) : {}"),
channel().config()
.isAutoRead(),
responseHeaders().entries()
.toString());
channel().config().isAutoRead(),
httpMessageLogFactory().debug(HttpMessageArgProviderFactory.create(response)));
}

if (notRedirected(response)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* 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
*
* https://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 reactor.netty.http.logging;

import io.netty.handler.codec.DecoderResult;

abstract class AbstractHttpMessageArgProvider implements HttpMessageArgProvider {

final DecoderResult decoderResult;

AbstractHttpMessageArgProvider(DecoderResult decoderResult) {
this.decoderResult = decoderResult;
}

@Override
public DecoderResult decoderResult() {
return decoderResult;
}
}
Loading

0 comments on commit 1afda75

Please sign in to comment.