Add new metrics for account, change, group and project Lucene indexes

Introduce four new Gauge metrics to track the number of documents
in the respective search indexes:

* index/lucene/accounts
* index/lucene/changes
* index/lucene/groups
* index/lucene/projects

These metrics are particularly useful in multi-site scenarios, as they
provide visibility into the number of documents across different
indexes, aiding in the diagnosis of potential indexing issues.

Release-Notes: Provide index metric for accounts/changes/groups/projects
Bug: Issue 381216361
Change-Id: I92d5d62614f1bbf367e519e2aa2192fc5383d2fe
diff --git a/Documentation/metrics.txt b/Documentation/metrics.txt
index 903c635..18455cf 100644
--- a/Documentation/metrics.txt
+++ b/Documentation/metrics.txt
@@ -550,6 +550,13 @@
 
 * `license/cla_check_count`: Total number of CLA check requests.
 
+=== Lucene
+
+* `index/lucene/accounts`: Total number documents in account search index.
+* `index/lucene/changes`: Total number documents in change search index.
+* `index/lucene/groups`: Total number documents in group search index.
+* `index/lucene/projects`: Total number documents in project search index.
+
 GERRIT
 ------
 Part of link:index.html[Gerrit Code Review]
diff --git a/java/com/google/gerrit/lucene/BUILD b/java/com/google/gerrit/lucene/BUILD
index 6584bfd..a02289e 100644
--- a/java/com/google/gerrit/lucene/BUILD
+++ b/java/com/google/gerrit/lucene/BUILD
@@ -30,6 +30,7 @@
         "//java/com/google/gerrit/index",
         "//java/com/google/gerrit/index:query_exception",
         "//java/com/google/gerrit/index/project",
+        "//java/com/google/gerrit/metrics",
         "//java/com/google/gerrit/proto",
         "//java/com/google/gerrit/server",
         "//java/com/google/gerrit/server/logging",
diff --git a/java/com/google/gerrit/lucene/LuceneIndexMetrics.java b/java/com/google/gerrit/lucene/LuceneIndexMetrics.java
new file mode 100644
index 0000000..31c8fde
--- /dev/null
+++ b/java/com/google/gerrit/lucene/LuceneIndexMetrics.java
@@ -0,0 +1,45 @@
+// Copyright (C) 2025 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.lucene;
+
+import com.google.gerrit.index.IndexDefinition;
+import com.google.gerrit.metrics.Description;
+import com.google.gerrit.metrics.MetricMaker;
+import com.google.inject.Inject;
+import java.util.Collection;
+
+class LuceneIndexMetrics {
+
+  @Inject
+  LuceneIndexMetrics(MetricMaker metrics, Collection<IndexDefinition<?, ?, ?>> defs) {
+    for (IndexDefinition<?, ?, ?> def : defs) {
+      String indexName = def.getName();
+
+      metrics.newCallbackMetric(
+          String.format("index/lucene/%s", indexName),
+          Integer.class,
+          new Description(String.format("%s Lucene Index documents", indexName))
+              .setGauge()
+              .setUnit("documents"),
+          () -> {
+            if (def.getIndexCollection().getSearchIndex() == null) {
+              return -1;
+            } else {
+              return def.getIndexCollection().getSearchIndex().numDocs();
+            }
+          });
+    }
+  }
+}
diff --git a/java/com/google/gerrit/lucene/LuceneIndexModule.java b/java/com/google/gerrit/lucene/LuceneIndexModule.java
index 89b83a4..9a34aee 100644
--- a/java/com/google/gerrit/lucene/LuceneIndexModule.java
+++ b/java/com/google/gerrit/lucene/LuceneIndexModule.java
@@ -26,6 +26,7 @@
 import com.google.gerrit.server.index.change.ChangeIndex;
 import com.google.gerrit.server.index.group.GroupIndex;
 import com.google.gerrit.server.index.options.AutoFlush;
+import com.google.inject.Scopes;
 import org.apache.lucene.search.BooleanQuery;
 import org.eclipse.jgit.lib.Config;
 
@@ -70,6 +71,7 @@
   protected void configure() {
     super.configure();
     bind(AutoFlush.class).toInstance(autoFlush);
+    bind(LuceneIndexMetrics.class).in(Scopes.SINGLETON);
   }
 
   @Override
diff --git a/javatests/com/google/gerrit/acceptance/testsuite/index/LuceneIndexMetricsIT.java b/javatests/com/google/gerrit/acceptance/testsuite/index/LuceneIndexMetricsIT.java
new file mode 100644
index 0000000..6236492
--- /dev/null
+++ b/javatests/com/google/gerrit/acceptance/testsuite/index/LuceneIndexMetricsIT.java
@@ -0,0 +1,67 @@
+// Copyright (C) 2025 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.acceptance.testsuite.index;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.common.truth.TruthJUnit.assume;
+
+import com.google.gerrit.acceptance.AbstractDaemonTest;
+import com.google.gerrit.acceptance.TestMetricMaker;
+import com.google.gerrit.index.IndexType;
+import com.google.inject.Inject;
+import org.junit.Test;
+
+public class LuceneIndexMetricsIT extends AbstractDaemonTest {
+
+  @Inject protected TestMetricMaker testMetricMaker;
+  private boolean isLuceneIndex =
+      IndexType.fromEnvironment().map(IndexType::isLucene).orElse(false);
+
+  @Test
+  public void checkProjectsIndexMetric() throws Exception {
+    assume().that(isLuceneIndex).isTrue();
+    int numberProjects = luceneIndexMetricValueOf("projects");
+    gApi.projects().create("some_project");
+    assertThat(luceneIndexMetricValueOf("projects")).isEqualTo(numberProjects + 1);
+  }
+
+  @Test
+  public void checkChangesIndexMetric() throws Exception {
+    assume().that(isLuceneIndex).isTrue();
+    int numberChanges = luceneIndexMetricValueOf("changes");
+    createChange();
+    assertThat(luceneIndexMetricValueOf("changes")).isEqualTo(numberChanges + 1);
+  }
+
+  @Test
+  public void checkAccountsIndexMetric() throws Exception {
+    assume().that(isLuceneIndex).isTrue();
+    int numberAccounts = luceneIndexMetricValueOf("accounts");
+    gApi.accounts().create("some_account");
+    assertThat(luceneIndexMetricValueOf("accounts")).isEqualTo(numberAccounts + 1);
+  }
+
+  @Test
+  public void checkGroupsIndexMetric() throws Exception {
+    assume().that(isLuceneIndex).isTrue();
+    int numberGroups = luceneIndexMetricValueOf("groups");
+    gApi.groups().create("some_group");
+    assertThat(luceneIndexMetricValueOf("groups")).isEqualTo(numberGroups + 1);
+  }
+
+  private int luceneIndexMetricValueOf(String metric) {
+    return (int) testMetricMaker.getCallbackMetricValue(String.format("index/lucene/%s", metric));
+  }
+}