Add metric to count the number of code owner suggestions

In particular we are interested in how often resolveAllUsers=true is
set, as we want to understand whether we could remove this option.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I63f4822627a1aa062aa911cdd7d75f67a45f25fa
diff --git a/java/com/google/gerrit/plugins/codeowners/metrics/CodeOwnerMetrics.java b/java/com/google/gerrit/plugins/codeowners/metrics/CodeOwnerMetrics.java
index 4e952be..06664ba 100644
--- a/java/com/google/gerrit/plugins/codeowners/metrics/CodeOwnerMetrics.java
+++ b/java/com/google/gerrit/plugins/codeowners/metrics/CodeOwnerMetrics.java
@@ -61,6 +61,7 @@
   public final Counter0 countCodeOwnerConfigCacheReads;
   public final Counter1<String> countCodeOwnerSubmitRuleErrors;
   public final Counter0 countCodeOwnerSubmitRuleRuns;
+  public final Counter1<Boolean> countCodeOwnerSuggestions;
   public final Counter3<String, String, String> countInvalidCodeOwnerConfigFiles;
 
   private final MetricMaker metricMaker;
@@ -168,6 +169,15 @@
     this.countCodeOwnerSubmitRuleRuns =
         createCounter(
             "count_code_owner_submit_rule_runs", "Total number of code owner submit rule runs");
+    this.countCodeOwnerSuggestions =
+        createCounter1(
+            "count_code_owner_suggestions",
+            "Total number of code owner suggestions",
+            Field.ofBoolean("resolve_all_users", (metadataBuilder, resolveAllUsers) -> {})
+                .description(
+                    "Whether code ownerships that are assigned to all users are resolved to random"
+                        + " users.")
+                .build());
     this.countInvalidCodeOwnerConfigFiles =
         createCounter3(
             "count_invalid_code_owner_config_files",
diff --git a/java/com/google/gerrit/plugins/codeowners/restapi/AbstractGetCodeOwnersForPath.java b/java/com/google/gerrit/plugins/codeowners/restapi/AbstractGetCodeOwnersForPath.java
index 3ad00e8..85db8e3 100644
--- a/java/com/google/gerrit/plugins/codeowners/restapi/AbstractGetCodeOwnersForPath.java
+++ b/java/com/google/gerrit/plugins/codeowners/restapi/AbstractGetCodeOwnersForPath.java
@@ -42,6 +42,7 @@
 import com.google.gerrit.plugins.codeowners.backend.CodeOwnerScorings;
 import com.google.gerrit.plugins.codeowners.backend.CodeOwnersInternalServerErrorException;
 import com.google.gerrit.plugins.codeowners.backend.config.CodeOwnersPluginConfiguration;
+import com.google.gerrit.plugins.codeowners.metrics.CodeOwnerMetrics;
 import com.google.gerrit.server.account.AccountControl;
 import com.google.gerrit.server.account.AccountDirectory.FillOptions;
 import com.google.gerrit.server.account.AccountLoader;
@@ -80,6 +81,7 @@
   private final AccountControl.Factory accountControlFactory;
   private final PermissionBackend permissionBackend;
   private final CheckCodeOwnerCapability checkCodeOwnerCapability;
+  private final CodeOwnerMetrics codeOwnerMetrics;
   private final CodeOwnersPluginConfiguration codeOwnersPluginConfiguration;
   private final CodeOwnerConfigHierarchy codeOwnerConfigHierarchy;
   private final Provider<CodeOwnerResolver> codeOwnerResolver;
@@ -155,6 +157,7 @@
       AccountControl.Factory accountControlFactory,
       PermissionBackend permissionBackend,
       CheckCodeOwnerCapability checkCodeOwnerCapability,
+      CodeOwnerMetrics codeOwnerMetrics,
       CodeOwnersPluginConfiguration codeOwnersPluginConfiguration,
       CodeOwnerConfigHierarchy codeOwnerConfigHierarchy,
       Provider<CodeOwnerResolver> codeOwnerResolver,
@@ -164,6 +167,7 @@
     this.accountControlFactory = accountControlFactory;
     this.permissionBackend = permissionBackend;
     this.checkCodeOwnerCapability = checkCodeOwnerCapability;
+    this.codeOwnerMetrics = codeOwnerMetrics;
     this.codeOwnersPluginConfiguration = codeOwnersPluginConfiguration;
     this.codeOwnerConfigHierarchy = codeOwnerConfigHierarchy;
     this.codeOwnerResolver = codeOwnerResolver;
@@ -185,6 +189,8 @@
       seed = getDefaultSeed(rsrc);
     }
 
+    codeOwnerMetrics.countCodeOwnerSuggestions.increment(resolveAllUsers);
+
     // The distance that applies to code owners that are defined in the root code owner
     // configuration.
     int rootDistance = rsrc.getPath().getNameCount();
diff --git a/java/com/google/gerrit/plugins/codeowners/restapi/BUILD b/java/com/google/gerrit/plugins/codeowners/restapi/BUILD
index e938a4c..298b46b 100644
--- a/java/com/google/gerrit/plugins/codeowners/restapi/BUILD
+++ b/java/com/google/gerrit/plugins/codeowners/restapi/BUILD
@@ -9,6 +9,7 @@
         "//plugins/code-owners/java/com/google/gerrit/plugins/codeowners/api",
         "//plugins/code-owners/java/com/google/gerrit/plugins/codeowners/backend",
         "//plugins/code-owners/java/com/google/gerrit/plugins/codeowners/common",
+        "//plugins/code-owners/java/com/google/gerrit/plugins/codeowners/metrics",
         "//plugins/code-owners/java/com/google/gerrit/plugins/codeowners/util",
         "//plugins/code-owners/java/com/google/gerrit/plugins/codeowners/validation",
         "//plugins/code-owners/proto:owners_metadata_java_proto",
diff --git a/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInBranch.java b/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInBranch.java
index 8a9b85c..446b91e 100644
--- a/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInBranch.java
+++ b/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInBranch.java
@@ -26,6 +26,7 @@
 import com.google.gerrit.plugins.codeowners.backend.CodeOwnerConfigHierarchy;
 import com.google.gerrit.plugins.codeowners.backend.CodeOwnerResolver;
 import com.google.gerrit.plugins.codeowners.backend.config.CodeOwnersPluginConfiguration;
+import com.google.gerrit.plugins.codeowners.metrics.CodeOwnerMetrics;
 import com.google.gerrit.server.account.AccountControl;
 import com.google.gerrit.server.account.Accounts;
 import com.google.gerrit.server.change.IncludedInResolver;
@@ -75,6 +76,7 @@
       AccountControl.Factory accountControlFactory,
       PermissionBackend permissionBackend,
       CheckCodeOwnerCapability checkCodeOwnerCapability,
+      CodeOwnerMetrics codeOwnerMetrics,
       CodeOwnersPluginConfiguration codeOwnersPluginConfiguration,
       CodeOwnerConfigHierarchy codeOwnerConfigHierarchy,
       Provider<CodeOwnerResolver> codeOwnerResolver,
@@ -86,6 +88,7 @@
         accountControlFactory,
         permissionBackend,
         checkCodeOwnerCapability,
+        codeOwnerMetrics,
         codeOwnersPluginConfiguration,
         codeOwnerConfigHierarchy,
         codeOwnerResolver,
diff --git a/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInChange.java b/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInChange.java
index 3180fd1..e2aac82 100644
--- a/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInChange.java
+++ b/java/com/google/gerrit/plugins/codeowners/restapi/GetCodeOwnersForPathInChange.java
@@ -30,6 +30,7 @@
 import com.google.gerrit.plugins.codeowners.backend.CodeOwnerScore;
 import com.google.gerrit.plugins.codeowners.backend.CodeOwnerScoring;
 import com.google.gerrit.plugins.codeowners.backend.config.CodeOwnersPluginConfiguration;
+import com.google.gerrit.plugins.codeowners.metrics.CodeOwnerMetrics;
 import com.google.gerrit.server.account.AccountControl;
 import com.google.gerrit.server.account.Accounts;
 import com.google.gerrit.server.account.ServiceUserClassifier;
@@ -63,6 +64,7 @@
       AccountControl.Factory accountControlFactory,
       PermissionBackend permissionBackend,
       CheckCodeOwnerCapability checkCodeOwnerCapability,
+      CodeOwnerMetrics codeOwnerMetrics,
       CodeOwnersPluginConfiguration codeOwnersPluginConfiguration,
       CodeOwnerConfigHierarchy codeOwnerConfigHierarchy,
       Provider<CodeOwnerResolver> codeOwnerResolver,
@@ -74,6 +76,7 @@
         accountControlFactory,
         permissionBackend,
         checkCodeOwnerCapability,
+        codeOwnerMetrics,
         codeOwnersPluginConfiguration,
         codeOwnerConfigHierarchy,
         codeOwnerResolver,
diff --git a/resources/Documentation/metrics.md b/resources/Documentation/metrics.md
index 00659f7..d06a9f3 100644
--- a/resources/Documentation/metrics.md
+++ b/resources/Documentation/metrics.md
@@ -69,6 +69,11 @@
       The cause of the submit rule error.
 * `count_code_owner_submit_rule_runs`:
   Total number of code owner submit rule runs.
+* `count_code_owner_suggestions`:
+  Total number of code owner suggestions.
+    * `resolve_all_users`:
+      Whether code ownerships that are assigned to all users are resolved to
+      random users.
 * `count_invalid_code_owner_config_files`:
   Total number of failed requests caused by an invalid / non-parsable code owner
   config file.