Skip to content

Commit

Permalink
Fix channel open trace logging (#81308) (#81314)
Browse files Browse the repository at this point in the history
Today we log `Tcp transport channel opened` when the channel is created,
before it is connected, which means its addresses are not configured so
it appears as `Netty4TcpChannel{localAddress=null, remoteAddress=null}`
which is pretty useless. This commit defers the logging until the
channel is actually open at which point its addresses are available.
  • Loading branch information
DaveCTurner committed Dec 3, 2021
1 parent 34fb605 commit 14e1f00
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions server/src/main/java/org/elasticsearch/transport/TcpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,10 @@ private void initiateConnection(DiscoveryNode node, ConnectionProfile connection

for (int i = 0; i < numConnections; ++i) {
try {
TcpChannel channel = initiateChannel(node);
logger.trace(() -> new ParameterizedMessage("Tcp transport client channel opened: {}", channel));
final TcpChannel channel = initiateChannel(node);
if (logger.isTraceEnabled()) {
channel.addConnectListener(new ChannelOpenTraceLogger(channel));
}
channels.add(channel);
} catch (ConnectTransportException e) {
CloseableChannel.closeChannels(channels, false);
Expand Down Expand Up @@ -1086,4 +1088,25 @@ public void onFailure(Exception e) {
}
}

private static final class ChannelOpenTraceLogger implements ActionListener<Void> {
private final TcpChannel channel;

ChannelOpenTraceLogger(TcpChannel channel) {
this.channel = channel;
}

@Override
public void onResponse(Void unused) {
logger.trace("Tcp transport client channel opened: {}", channel);
}

@Override
public void onFailure(Exception e) {
// Connection failures are generally logged elsewhere, but go via the ChannelsConnectedListener which only captures the first
// exception for each bundle of channels. If the ChannelOpenTraceLogger is installed then trace-logging is enabled so we can log
// every failure.
logger.trace(new ParameterizedMessage("failed to open transport channel: {}", channel), e);
}
}

}

0 comments on commit 14e1f00

Please sign in to comment.