Add logging for received messages This functionality already exists for JGroups based message forwarding. With the REST based forwarding there was some logging but it lacked details, like the request body. So we could see the forwarded message type but not the details. Move extraction of the request body up to the AbstractRestApiServlet. This method is then used by all subclasses to read the request body as json string. Once read it can be used for logging. To enable logging of all received messages set the ForwardedMessageLogger to DEBUG level. Change-Id: Ie49babc7128edfbdf0518eae8b6f1af3ad5594f8
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractIndexRestApiServlet.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractIndexRestApiServlet.java index 108c37c..343daab 100644 --- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractIndexRestApiServlet.java +++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractIndexRestApiServlet.java
@@ -23,13 +23,10 @@ import com.ericsson.gerrit.plugins.highavailability.forwarder.ForwardedIndexingHandler.Operation; import com.ericsson.gerrit.plugins.highavailability.forwarder.IndexEvent; import com.ericsson.gerrit.plugins.highavailability.forwarder.ProcessorMetricsRegistry; -import com.google.common.base.Charsets; import com.google.gerrit.common.Nullable; import com.google.gerrit.extensions.restapi.NotImplementedException; import com.google.gson.Gson; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; import java.util.Optional; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -98,7 +95,9 @@ T id = parse(path.substring(path.lastIndexOf('/') + 1)); try { - forwardedIndexingHandler.index(id, operation, parseBody(req)); + String body = readRequestBody(req); + ForwardedMessageLogger.log(req, body); + forwardedIndexingHandler.index(id, operation, parseBody(body)); rsp.setStatus(SC_NO_CONTENT); return true; } catch (IOException e) { @@ -108,13 +107,7 @@ } } - protected Optional<IndexEvent> parseBody(HttpServletRequest req) throws IOException { - String contentType = req.getContentType(); - if (contentType != null && contentType.contains("application/json")) { - try (Reader reader = new InputStreamReader(req.getInputStream(), Charsets.UTF_8)) { - return Optional.ofNullable(gson.fromJson(reader, IndexEvent.class)); - } - } - return Optional.empty(); + protected Optional<IndexEvent> parseBody(String body) { + return Optional.ofNullable(gson.fromJson(body, IndexEvent.class)); } }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractRestApiServlet.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractRestApiServlet.java index 6aa0461..a4347c5 100644 --- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractRestApiServlet.java +++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/AbstractRestApiServlet.java
@@ -21,6 +21,7 @@ import com.ericsson.gerrit.plugins.highavailability.forwarder.ProcessorMetricsRegistry; import com.google.common.base.Strings; import com.google.common.flogger.FluentLogger; +import com.google.common.io.CharStreams; import com.google.gerrit.common.Nullable; import com.google.gerrit.extensions.restapi.NotImplementedException; import java.io.IOException; @@ -106,6 +107,14 @@ } } + protected String readRequestBody(HttpServletRequest req) throws IOException { + String contentType = req.getContentType(); + if (contentType != null && contentType.contains("application/json")) { + return CharStreams.toString(req.getReader()); + } + return null; + } + protected static void updateMetrics( ProcessorMetrics metrics, HttpServletRequest req, Instant startTime, boolean success) { String eventCreatedOn = req.getHeader(HttpSession.HEADER_EVENT_CREATED_ON);
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/CacheRestApiServlet.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/CacheRestApiServlet.java index 926776b..ef2ef06 100644 --- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/CacheRestApiServlet.java +++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/CacheRestApiServlet.java
@@ -54,7 +54,8 @@ try { List<String> params = Splitter.on('/').splitToList(req.getPathInfo()); String cacheName = params.get(CACHENAME_INDEX); - String json = req.getReader().readLine(); + String json = readRequestBody(req); + ForwardedMessageLogger.log(req, json); forwardedCacheEvictionHandler.evict( CacheEntry.from(cacheName, cacheKeyParser.fromJson(cacheName, json))); rsp.setStatus(SC_NO_CONTENT);
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/EventRestApiServlet.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/EventRestApiServlet.java index 422e3df..57b6a6a 100644 --- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/EventRestApiServlet.java +++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/EventRestApiServlet.java
@@ -22,7 +22,6 @@ import com.ericsson.gerrit.plugins.highavailability.forwarder.EventType; import com.ericsson.gerrit.plugins.highavailability.forwarder.ForwardedEventHandler; import com.ericsson.gerrit.plugins.highavailability.forwarder.ProcessorMetricsRegistry; -import com.google.common.io.CharStreams; import com.google.common.net.MediaType; import com.google.gerrit.server.events.Event; import com.google.gson.Gson; @@ -56,7 +55,9 @@ sendError(rsp, SC_UNSUPPORTED_MEDIA_TYPE, "Expecting " + JSON_UTF_8 + " content type"); return false; } - Event event = getEventFromRequest(req); + String body = readRequestBody(req); + ForwardedMessageLogger.log(req, body); + Event event = getEventFromRequest(body); rsp.setStatus(SC_NO_CONTENT); forwardedEventHandler.dispatch(event); return true; @@ -66,8 +67,7 @@ } } - private Event getEventFromRequest(HttpServletRequest req) throws IOException { - String jsonEvent = CharStreams.toString(req.getReader()); - return gson.fromJson(jsonEvent, Event.class); + private Event getEventFromRequest(String body) { + return gson.fromJson(body, Event.class); } }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ForwardedMessageLogger.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ForwardedMessageLogger.java new file mode 100644 index 0000000..263cb98 --- /dev/null +++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ForwardedMessageLogger.java
@@ -0,0 +1,32 @@ +// Copyright (C) 2026 The Android Open Source Project +// +// 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 com.ericsson.gerrit.plugins.highavailability.forwarder.rest; + +import com.google.common.flogger.FluentLogger; +import javax.servlet.http.HttpServletRequest; + +public class ForwardedMessageLogger { + private static final FluentLogger log = FluentLogger.forEnclosingClass(); + + static void log(HttpServletRequest req, String body) { + StringBuilder sb = new StringBuilder(); + sb.append(req.getMethod()).append(" ").append(req.getRequestURI()); + if (body != null) { + sb.append(" "); + sb.append(body); + } + log.atFine().log("Received message: %s", sb.toString()); + } +}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ProjectListApiServlet.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ProjectListApiServlet.java index fb88edb..13ca2e7 100644 --- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ProjectListApiServlet.java +++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ProjectListApiServlet.java
@@ -52,6 +52,7 @@ } private boolean process(HttpServletRequest req, HttpServletResponse rsp, boolean delete) { + ForwardedMessageLogger.log(req, null); String requestURI = req.getRequestURI(); String projectName = requestURI.substring(requestURI.lastIndexOf('/') + 1); try {
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/CacheRestApiServletTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/CacheRestApiServletTest.java index 88a325e..4d18614 100644 --- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/CacheRestApiServletTest.java +++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/CacheRestApiServletTest.java
@@ -26,10 +26,12 @@ import com.ericsson.gerrit.plugins.highavailability.forwarder.ForwardedCacheEvictionHandler; import com.ericsson.gerrit.plugins.highavailability.forwarder.ProcessorMetrics; import com.ericsson.gerrit.plugins.highavailability.forwarder.ProcessorMetricsRegistry; +import com.google.common.net.MediaType; import com.google.gerrit.extensions.registration.DynamicMap; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.IOException; +import java.io.StringReader; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.junit.Before; @@ -42,7 +44,6 @@ public class CacheRestApiServletTest { @Mock private HttpServletRequest requestMock; @Mock private HttpServletResponse responseMock; - @Mock private BufferedReader readerMock; @Mock private ForwardedCacheEvictionHandler forwardedCacheEvictionHandlerMock; @Mock private ProcessorMetricsRegistry metricsRegistry; @Mock ProcessorMetrics metrics; @@ -110,6 +111,7 @@ public void badRequest() throws Exception { when(requestMock.getPathInfo()).thenReturn("/someCache"); String errorMessage = "someError"; + when(requestMock.getContentType()).thenReturn(MediaType.JSON_UTF_8.toString()); doThrow(new IOException(errorMessage)).when(requestMock).getReader(); servlet.doPost(requestMock, responseMock); verify(responseMock).sendError(SC_BAD_REQUEST, errorMessage); @@ -141,17 +143,19 @@ } else { when(requestMock.getPathInfo()).thenReturn("/" + pluginName + "." + cacheName); } - when(requestMock.getReader()).thenReturn(readerMock); + when(requestMock.getContentType()).thenReturn(MediaType.JSON_UTF_8.toString()); + StringReader reader; if (Constants.PROJECTS.equals(cacheName)) { - when(readerMock.readLine()).thenReturn("abc"); + reader = new StringReader("abc"); } else if (Constants.GROUPS_BYINCLUDE.equals(cacheName) || Constants.GROUPS_MEMBERS.equals(cacheName)) { - when(readerMock.readLine()).thenReturn("{\"uuid\":\"abcd1234\"}"); + reader = new StringReader("{\"uuid\":\"abcd1234\"}"); } else if (Constants.TOKENS.equals(cacheName)) { - when(readerMock.readLine()).thenReturn("{\"id\":\"1234\"}"); + reader = new StringReader("{\"id\":\"1234\"}"); } else { - when(readerMock.readLine()).thenReturn("{}"); + reader = new StringReader("{}"); } + when(requestMock.getReader()).thenReturn(new BufferedReader(reader)); } }