Add a metric to count the number of code owner config reads

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: Id2d9c0f2583031d65d6fb6a5555ee2833e65a628
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwners.java b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwners.java
index 843a054..e248570 100644
--- a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwners.java
+++ b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwners.java
@@ -18,6 +18,7 @@
 
 import com.google.common.base.Throwables;
 import com.google.gerrit.plugins.codeowners.backend.config.CodeOwnersPluginConfiguration;
+import com.google.gerrit.plugins.codeowners.metrics.CodeOwnerMetrics;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
 import java.nio.file.Path;
@@ -37,10 +38,14 @@
 @Singleton
 public class CodeOwners {
   private final CodeOwnersPluginConfiguration codeOwnersPluginConfiguration;
+  private final CodeOwnerMetrics codeOwnerMetrics;
 
   @Inject
-  CodeOwners(CodeOwnersPluginConfiguration codeOwnersPluginConfiguration) {
+  CodeOwners(
+      CodeOwnersPluginConfiguration codeOwnersPluginConfiguration,
+      CodeOwnerMetrics codeOwnerMetrics) {
     this.codeOwnersPluginConfiguration = codeOwnersPluginConfiguration;
+    this.codeOwnerMetrics = codeOwnerMetrics;
   }
 
   /**
@@ -54,6 +59,7 @@
   public Optional<CodeOwnerConfig> get(CodeOwnerConfig.Key codeOwnerConfigKey, ObjectId revision) {
     requireNonNull(codeOwnerConfigKey, "codeOwnerConfigKey");
     requireNonNull(revision, "revision");
+    codeOwnerMetrics.countCodeOwnerConfigReads.increment();
     CodeOwnerBackend codeOwnerBackend =
         codeOwnersPluginConfiguration.getBackend(codeOwnerConfigKey.branchNameKey());
     return codeOwnerBackend.getCodeOwnerConfig(codeOwnerConfigKey, revision);
@@ -68,6 +74,7 @@
    */
   public Optional<CodeOwnerConfig> getFromCurrentRevision(CodeOwnerConfig.Key codeOwnerConfigKey) {
     requireNonNull(codeOwnerConfigKey, "codeOwnerConfigKey");
+    codeOwnerMetrics.countCodeOwnerConfigReads.increment();
     CodeOwnerBackend codeOwnerBackend =
         codeOwnersPluginConfiguration.getBackend(codeOwnerConfigKey.branchNameKey());
     return codeOwnerBackend.getCodeOwnerConfig(codeOwnerConfigKey, /* revision= */ null);
diff --git a/java/com/google/gerrit/plugins/codeowners/metrics/CodeOwnerMetrics.java b/java/com/google/gerrit/plugins/codeowners/metrics/CodeOwnerMetrics.java
index be681e6..0b7661e 100644
--- a/java/com/google/gerrit/plugins/codeowners/metrics/CodeOwnerMetrics.java
+++ b/java/com/google/gerrit/plugins/codeowners/metrics/CodeOwnerMetrics.java
@@ -14,6 +14,7 @@
 
 package com.google.gerrit.plugins.codeowners.metrics;
 
+import com.google.gerrit.metrics.Counter0;
 import com.google.gerrit.metrics.Description;
 import com.google.gerrit.metrics.Description.Units;
 import com.google.gerrit.metrics.MetricMaker;
@@ -24,6 +25,7 @@
 /** Metrics of the code-owners plugin. */
 @Singleton
 public class CodeOwnerMetrics {
+  // latency metrics
   public final Timer0 computeChangedFiles;
   public final Timer0 computeFileStatus;
   public final Timer0 computeFileStatuses;
@@ -36,12 +38,16 @@
   public final Timer0 resolvePathCodeOwners;
   public final Timer0 runCodeOwnerSubmitRule;
 
+  // counter metrics
+  public final Counter0 countCodeOwnerConfigReads;
+
   private final MetricMaker metricMaker;
 
   @Inject
   CodeOwnerMetrics(MetricMaker metricMaker) {
     this.metricMaker = metricMaker;
 
+    // latency metrics
     this.computeChangedFiles =
         createLatencyTimer("compute_changed_files", "Latency for computing changed files");
     this.computeFileStatus =
@@ -79,6 +85,10 @@
     this.runCodeOwnerSubmitRule =
         createLatencyTimer(
             "run_code_owner_submit_rule", "Latency for running the code owner submit rule");
+
+    // counter metrics
+    this.countCodeOwnerConfigReads =
+        createCounter("count_code_owner_config_reads", "Total number of code owner config reads");
   }
 
   private Timer0 createLatencyTimer(String name, String description) {
@@ -86,4 +96,8 @@
         "code_owners/" + name,
         new Description(description).setCumulative().setUnit(Units.MILLISECONDS));
   }
+
+  private Counter0 createCounter(String name, String description) {
+    return metricMaker.newCounter("code_owners/" + name, new Description(description).setRate());
+  }
 }
diff --git a/resources/Documentation/metrics.md b/resources/Documentation/metrics.md
index 55bb736..eda1fbe 100644
--- a/resources/Documentation/metrics.md
+++ b/resources/Documentation/metrics.md
@@ -28,6 +28,11 @@
 * `run_code_owner_submit_rule`:
   Latency for running the code owner submit rule.
 
+## <a id="counterMetrics"> Counter Metrics
+
+* `count_code_owner_config_reads`:
+  Total number of code owner config reads.
+
 ---
 
 Back to [@PLUGIN@ documentation index](index.html)