Make cache disk stat metric computation optional

Computing the disc stat metrics for persistent caches is very expensive
and can block a thread for several minutes on larger installations.

This change adds a configuration option to enable the computation of
the disk stats. This option is set to false by default. This makes the
change a breaking change, which will affect users that use the
caches_disk_.* metrics. However, since computing this metric can block
multiple threads depending on metric scrape rate and thus even cause
outages, it makes sense to not enable this by default.

Change-Id: I41ee2d9a368c312b7b2729d17d6c19bee0d90922
diff --git a/Documentation/config-gerrit.txt b/Documentation/config-gerrit.txt
index c96e685..03c4beb 100644
--- a/Documentation/config-gerrit.txt
+++ b/Documentation/config-gerrit.txt
@@ -688,6 +688,13 @@
 +
 Default is unset, no disk cache.
 
+[[cache.enableDiskStatMetrics]]cache.enableDiskStatMetrics::
++
+Whether to enable the computation of disk statistics of persistent caches.
+This computation is expensive and requires a long time on larger installations.
++
+By default, false.
+
 [[cache.h2CacheSize]]cache.h2CacheSize::
 +
 The size of the in-memory cache for each opened H2 cache database, in bytes.
diff --git a/java/com/google/gerrit/server/cache/CacheMetrics.java b/java/com/google/gerrit/server/cache/CacheMetrics.java
index 5c8cce6..60dd62f 100644
--- a/java/com/google/gerrit/server/cache/CacheMetrics.java
+++ b/java/com/google/gerrit/server/cache/CacheMetrics.java
@@ -25,16 +25,19 @@
 import com.google.gerrit.metrics.Description;
 import com.google.gerrit.metrics.Field;
 import com.google.gerrit.metrics.MetricMaker;
+import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
 import java.util.Set;
+import org.eclipse.jgit.lib.Config;
 
 @Singleton
 public class CacheMetrics {
   private static final Field<String> F_NAME = Field.ofString("cache_name");
 
   @Inject
-  public CacheMetrics(MetricMaker metrics, DynamicMap<Cache<?, ?>> cacheMap) {
+  public CacheMetrics(
+      MetricMaker metrics, DynamicMap<Cache<?, ?>> cacheMap, @GerritServerConfig Config config) {
     CallbackMetric1<String, Long> memEnt =
         metrics.newCallbackMetric(
             "caches/memory_cached",
@@ -79,7 +82,8 @@
             memEnt.set(name, c.size());
             memHit.set(name, cstats.hitRate() * 100);
             memEvict.set(name, cstats.evictionCount());
-            if (c instanceof PersistentCache) {
+            if (c instanceof PersistentCache
+                && config.getBoolean("cache", "enableDiskStatMetrics", false)) {
               PersistentCache.DiskStats d = ((PersistentCache) c).diskStats();
               perDiskEnt.set(name, d.size());
               perDiskHit.set(name, hitRatio(d));