RestApiServlet: Do not count non-error-exceptions as ISE if response is already committed

If an exception occurs we return '500 Internal Server Error' by default.
However ExceptionHook implementations can decide to return a different
response status, e.g. if an exception is caused by a client that
disconnects, an exception hook could return '499 Client Closed Request',
which is not an internal server error. If this happens, we increase the
http/server/rest_api/error_count metric for the status code that was
returned by the exception hook, however this only worked if the response
was not committed yet. If the response was already committed we counted
all exceptions as '500 Internal Server Error', although an exception
would have returned a different response code. This is bad because it
can eat up the SLO budget.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: If6fd0b543a3d385a2d354f8604202f1663b89242
diff --git a/java/com/google/gerrit/httpd/restapi/RestApiServlet.java b/java/com/google/gerrit/httpd/restapi/RestApiServlet.java
index 4db9217..cca4f69 100644
--- a/java/com/google/gerrit/httpd/restapi/RestApiServlet.java
+++ b/java/com/google/gerrit/httpd/restapi/RestApiServlet.java
@@ -678,17 +678,25 @@
         cause = Optional.of(e);
         statusCode = SC_INTERNAL_SERVER_ERROR;
 
+        Optional<ExceptionHook.Status> status = getStatus(e);
+        statusCode = status.map(ExceptionHook.Status::statusCode).orElse(SC_INTERNAL_SERVER_ERROR);
+
         if (res.isCommitted()) {
-          logger.atSevere().withCause(e).log(
-              "Error in %s %s, response already committed", req.getMethod(), uriForLogging(req));
           responseBytes = 0;
+          if (statusCode == SC_INTERNAL_SERVER_ERROR) {
+            logger.atSevere().withCause(e).log(
+                "Error in %s %s, response already committed with status %d",
+                req.getMethod(), uriForLogging(req), res.getStatus());
+          } else {
+            logger.atWarning().log(
+                "Response for %s %s already committed with status %d, wanted to set status %d",
+                req.getMethod(), uriForLogging(req), res.getStatus(), statusCode);
+          }
         } else {
           res.reset();
           traceContext.getTraceId().ifPresent(traceId -> res.addHeader(X_GERRIT_TRACE, traceId));
 
-          Optional<ExceptionHook.Status> status = getStatus(e);
           if (status.isPresent()) {
-            statusCode = status.get().statusCode();
             responseBytes = reply(req, res, e, status.get(), getUserMessages(traceContext, e));
           }
           responseBytes = replyInternalServerError(req, res, e, getUserMessages(traceContext, e));