Skip to content

Commit

Permalink
Merge pull request #1852 from Netflix/alaborda/fix_log_original_remot…
Browse files Browse the repository at this point in the history
…e_ip

Allow ACCESS logs fields overrides
  • Loading branch information
lalernehl authored Dec 11, 2024
2 parents 51dc4b7 + 588ca06 commit 706f7f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,25 @@ public final class AccessLogChannelHandler {

private static final Logger LOG = LoggerFactory.getLogger(AccessLogChannelHandler.class);

public static final class AccessLogInboundChannelHandler extends ChannelInboundHandlerAdapter {
public static class AccessLogInboundChannelHandler extends ChannelInboundHandlerAdapter {
private final AccessLogPublisher publisher;

public AccessLogInboundChannelHandler(AccessLogPublisher publisher) {
this.publisher = publisher;
}

protected Integer getLocalPort(ChannelHandlerContext ctx) {
return ctx.channel()
.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT)
.get();
}

protected String getRemoteIp(ChannelHandlerContext ctx) {
return ctx.channel()
.attr(SourceAddressChannelHandler.ATTR_SOURCE_ADDRESS)
.get();
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
Expand Down Expand Up @@ -77,12 +89,9 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc

// Response complete, so now write to access log.
long durationNs = System.nanoTime() - state.startTimeNs;
String remoteIp = ctx.channel()
.attr(SourceAddressChannelHandler.ATTR_SOURCE_ADDRESS)
.get();
Integer localPort = ctx.channel()
.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT)
.get();

Integer localPort = getLocalPort(ctx);
String remoteIp = getRemoteIp(ctx);

if (state.response == null) {
LOG.debug(
Expand Down Expand Up @@ -132,7 +141,7 @@ private static class RequestState {
HttpRequest request;
HttpResponse response;
long startTimeNs;
int requestBodySize = 0;
int responseBodySize = 0;
long requestBodySize = 0;
long responseBodySize = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public void log(
Integer localPort,
String remoteIp,
Long durationNs,
Integer requestBodySize,
Integer responseBodySize) {
StringBuilder sb = new StringBuilder();
Long requestBodySize,
Long responseBodySize) {
StringBuilder sb = new StringBuilder(512);

String dateTimeStr = dateTime != null ? dateTime.format(DATE_TIME_FORMATTER) : "-----T-:-:-";
String remoteIpStr = (remoteIp != null && !remoteIp.isEmpty()) ? remoteIp : "-";
Expand Down Expand Up @@ -134,6 +134,6 @@ void includeMatchingHeaders(StringBuilder builder, List<String> requiredHeaders,

String headerAsString(HttpHeaders headers, String headerName) {
List<String> values = headers.getAll(headerName);
return (values.size() == 0) ? "-" : String.join(",", values);
return values.isEmpty() ? "-" : String.join(",", values);
}
}

0 comments on commit 706f7f7

Please sign in to comment.