Set HTTP thread name to describe the request it is processing

The HTTP worker threads had generic names: HTTP-100, HTTP-101, ...
While we could conclude from the stack trace what this thread was doing
we missed some important information like repository name, user name,
etc...

With this change the HTTP threads get descriptive names. For example, if
there is an ongoing git-fetch operation we would see a thread named
like:

"HTTP POST /a/myProject/git-upload-pack (johndoe from 10.87.75.169)"

This can be of a great help for Gerrit admins trying to figure out who
is executing that many git-fetch requests and affecting the overall
performance.  Note that we cannot reliably tell that from the httpd_log
as requests are logged only once they are finished. The show-queue
command also doesn't show tasks being processing in the HTTP thread
pool.

As additional justification, SSH thread names are already named with a
similar pattern i.e:

"SSH git-upload-pack /myProject (johndoe)"

Change-Id: I98cf112018732e5d4075568e7da8614ffe0495a4
diff --git a/java/com/google/gerrit/httpd/SetThreadNameFilter.java b/java/com/google/gerrit/httpd/SetThreadNameFilter.java
new file mode 100644
index 0000000..c7d977d
--- /dev/null
+++ b/java/com/google/gerrit/httpd/SetThreadNameFilter.java
@@ -0,0 +1,91 @@
+// Copyright (C) 2020 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.google.gerrit.httpd;
+
+import com.google.gerrit.server.CurrentUser;
+import com.google.inject.Inject;
+import com.google.inject.Module;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+import com.google.inject.servlet.ServletModule;
+import java.io.IOException;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
+@Singleton
+public class SetThreadNameFilter implements Filter {
+  private static final int MAX_PATH_LENGTH = 120;
+
+  public static Module module() {
+    return new ServletModule() {
+      @Override
+      protected void configureServlets() {
+        filter("/*").through(SetThreadNameFilter.class);
+      }
+    };
+  }
+
+  private final Provider<CurrentUser> user;
+
+  @Inject
+  public SetThreadNameFilter(Provider<CurrentUser> user) {
+    this.user = user;
+  }
+
+  @Override
+  public void init(FilterConfig filterConfig) {}
+
+  @Override
+  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+      throws IOException, ServletException {
+    Thread current = Thread.currentThread();
+    String old = current.getName();
+    try {
+      current.setName(computeName((HttpServletRequest) request));
+      chain.doFilter(request, response);
+    } finally {
+      current.setName(old);
+    }
+  }
+
+  private String computeName(HttpServletRequest req) {
+    StringBuilder s = new StringBuilder();
+    s.append("HTTP ");
+    s.append(req.getMethod());
+    s.append(" ");
+    s.append(req.getRequestURI());
+    String query = req.getQueryString();
+    if (query != null) {
+      s.append("?").append(query);
+    }
+    if (s.length() > MAX_PATH_LENGTH) {
+      s.delete(MAX_PATH_LENGTH, s.length());
+    }
+    s.append(" (");
+    s.append(user.get().getUserName().orElse("N/A"));
+    s.append(" from ");
+    s.append(req.getRemoteAddr());
+    s.append(")");
+    return s.toString();
+  }
+
+  @Override
+  public void destroy() {}
+}
diff --git a/java/com/google/gerrit/httpd/init/WebAppInitializer.java b/java/com/google/gerrit/httpd/init/WebAppInitializer.java
index c9cf2f4..c2d4a14 100644
--- a/java/com/google/gerrit/httpd/init/WebAppInitializer.java
+++ b/java/com/google/gerrit/httpd/init/WebAppInitializer.java
@@ -32,6 +32,7 @@
 import com.google.gerrit.httpd.RequestContextFilter;
 import com.google.gerrit.httpd.RequestMetricsFilter;
 import com.google.gerrit.httpd.RequireSslFilter;
+import com.google.gerrit.httpd.SetThreadNameFilter;
 import com.google.gerrit.httpd.WebModule;
 import com.google.gerrit.httpd.WebSshGlueModule;
 import com.google.gerrit.httpd.auth.oauth.OAuthModule;
@@ -418,6 +419,7 @@
     modules.add(sysInjector.getInstance(GerritAuthModule.class));
     modules.add(sysInjector.getInstance(GitOverHttpModule.class));
     modules.add(RequestCleanupFilter.module());
+    modules.add(SetThreadNameFilter.module());
     modules.add(AllRequestFilter.module());
     modules.add(sysInjector.getInstance(WebModule.class));
     modules.add(sysInjector.getInstance(RequireSslFilter.Module.class));
diff --git a/java/com/google/gerrit/pgm/Daemon.java b/java/com/google/gerrit/pgm/Daemon.java
index 8fb208b..65707fa 100644
--- a/java/com/google/gerrit/pgm/Daemon.java
+++ b/java/com/google/gerrit/pgm/Daemon.java
@@ -37,6 +37,7 @@
 import com.google.gerrit.httpd.RequestContextFilter;
 import com.google.gerrit.httpd.RequestMetricsFilter;
 import com.google.gerrit.httpd.RequireSslFilter;
+import com.google.gerrit.httpd.SetThreadNameFilter;
 import com.google.gerrit.httpd.WebModule;
 import com.google.gerrit.httpd.WebSshGlueModule;
 import com.google.gerrit.httpd.auth.oauth.OAuthModule;
@@ -590,6 +591,7 @@
     }
     modules.add(RequestCleanupFilter.module());
     modules.add(AllRequestFilter.module());
+    modules.add(SetThreadNameFilter.module());
     modules.add(sysInjector.getInstance(WebModule.class));
     modules.add(sysInjector.getInstance(RequireSslFilter.Module.class));
     modules.add(new HttpPluginModule());