Make the reporting rate configurable

Allow to configure graphite.rate in the plugin's configuration file.

The default is 60 seconds, which is the same as the previously hard
coded value of 1 minute.

The minimum supported rate is 1 second.

Change-Id: I643a74825e941211803b7a812f3c22e4aa490d22
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritGraphiteReporter.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritGraphiteReporter.java
index 7ed8ed4..f8d1147 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritGraphiteReporter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritGraphiteReporter.java
@@ -47,10 +47,15 @@
   private static final String KEY_HOST = "host";
   private static final String KEY_PORT = "port";
   private static final String KEY_PREFIX = "prefix";
+  private static final String KEY_RATE = "rate";
   private static final String DEFAULT_HOST = "localhost";
   private static final int DEFAULT_PORT = 2003;
   private static final String DEFAULT_PREFIX = "gerrit";
+  private static final TimeUnit DEFAULT_RATE_UNIT = TimeUnit.SECONDS;
+  private static final int DEFAULT_RATE = 60;
+
   private final GraphiteReporter graphiteReporter;
+  private final int rate;
 
   @Inject
   public GerritGraphiteReporter(
@@ -70,9 +75,27 @@
         throw new RuntimeException(e);
       }
     }
-    log.info(
-        String.format("Reporting to Graphite at %s:%d with prefix %s",
-        host, port, prefix));
+
+    long configRate;
+    try {
+      configRate = config.getTimeUnit(
+          SECTION_GRAPHITE, null, KEY_RATE, DEFAULT_RATE, DEFAULT_RATE_UNIT);
+    } catch (IllegalArgumentException e) {
+      log.warn(String.format(
+          "Invalid rate value; default to %ds", DEFAULT_RATE));
+      configRate = DEFAULT_RATE;
+    }
+    if (configRate > 0) {
+      rate = (int) configRate;
+    } else {
+      log.warn(String.format(
+          "Rate value must be positive; default to %ds", DEFAULT_RATE));
+      rate = DEFAULT_RATE;
+    }
+
+    log.info(String.format(
+        "Reporting to Graphite at %s:%d with prefix %s at rate %ds",
+        host, port, prefix, rate));
 
     graphiteReporter = GraphiteReporter.forRegistry(registry)
         .convertRatesTo(TimeUnit.MINUTES)
@@ -84,7 +107,7 @@
 
   @Override
   public void start() {
-    graphiteReporter.start(1, TimeUnit.MINUTES);
+    graphiteReporter.start(rate, DEFAULT_RATE_UNIT);
   }
 
   @Override
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index cbe5f14..3335582 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -16,3 +16,9 @@
 graphite.prefix
 :	Prefix to use when reporting metrics. Defaults to `gerrit.`
 	suffixed with the hostname of `localhost`.
+
+graphite.rate
+:	Reporting rate in seconds. May be specified in common time
+	units such as 'm', 's', 'ms', etc, but will be converted
+	to seconds. The lowest supported rate is `1 s`.
+	Defaults to `60 s`.