Repository: jenkinsci/prometheus-plugin (java)

Issue:
No all_jenkins_version_info metric in 2.3.2 version
### Jenkins and plugins versions report

<details>
  <summary>Environment</summary>

  <!-- Paste your environment details below -->
  ```text
  Paste the output here
  ```

</details>


### What Operating System are you using (both controller, and any agents involved in the problem)?

Jenkins: 2.414.1
OS: Linux - 3.10.0-1160.95.1.el7.x86_64
Java: 11.0.15 - Eclipse Adoptium (OpenJDK 64-Bit Server VM)

### Reproduction steps

Go to https://path.to.jenkins/prometheus to get all the metrics

### Expected Results

Expecting to see the:
all_jenkins_version_info{version="2.414.1",} 1.0

### Actual Results

There is no any info about the versions

### Anything else?

Have a couple of dozen jenkins instances, and the issue appears only after installing this version of plugin.

Pull request:
Fix filtering of external metrics
Fixes #557

### Checklist

- [x] Includes tests covering the new functionality?
- [x] Ready for review
- [x] Follows CONTRIBUTING rules

### Notify

@Waschndolos


Patch:
diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumeration.java b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumeration.java
new file mode 100644
index 0000000..d531b93
--- /dev/null
+++ b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumeration.java
@@ -0,0 +1,41 @@
+package org.jenkinsci.plugins.prometheus.config.disabledmetrics;
+
+import io.prometheus.client.Collector;
+
+import java.util.*;
+
+public class FilteredMetricEnumeration implements Enumeration<Collector.MetricFamilySamples> {
+
+    private final Iterator<Collector.MetricFamilySamples> filteredList;
+
+    public FilteredMetricEnumeration(Iterator<Collector.MetricFamilySamples> fullList) {
+        this.filteredList = filterList(fullList);
+    }
+
+    private Iterator<Collector.MetricFamilySamples> filterList(Iterator<Collector.MetricFamilySamples> fullList) {
+        List<Collector.MetricFamilySamples> filteredList = new ArrayList<>();
+        while (fullList.hasNext()) {
+            Collector.MetricFamilySamples familySamples = fullList.next();
+            if (MetricStatusChecker.isEnabled(familySamples.name)) {
+                filteredList.add(familySamples);
+            }
+        }
+        return filteredList.iterator();
+    }
+
+
+    @Override
+    public boolean hasMoreElements() {
+        return filteredList.hasNext();
+    }
+
+    @Override
+    public Collector.MetricFamilySamples nextElement() {
+        return filteredList.next();
+    }
+
+    @Override
+    public Iterator<Collector.MetricFamilySamples> asIterator() {
+        return filteredList;
+    }
+}
diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java b/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java
index 2b7935a..9024c9c 100644
--- a/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java
+++ b/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java
@@ -8,14 +8,13 @@ import io.prometheus.client.exporter.common.TextFormat;
 import io.prometheus.client.hotspot.DefaultExports;
 import jenkins.metrics.api.Metrics;
 import org.jenkinsci.plugins.prometheus.*;
-import org.jenkinsci.plugins.prometheus.config.disabledmetrics.MetricStatusChecker;
+import org.jenkinsci.plugins.prometheus.config.disabledmetrics.FilteredMetricEnumeration;
 import org.jenkinsci.plugins.prometheus.util.JenkinsNodeBuildsSampleBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.io.StringWriter;
-import java.util.*;
 import java.util.concurrent.atomic.AtomicReference;
 
 public class DefaultPrometheusMetrics implements PrometheusMetrics {
@@ -51,23 +50,10 @@ public class DefaultPrometheusMetrics implements PrometheusMetrics {
     @Override
     public void collectMetrics() {
         try (StringWriter buffer = new StringWriter()) {
-            Set<String> filteredMetrics = MetricStatusChecker.filter(getMetricNames());
-            TextFormat.write004(buffer, collectorRegistry.filteredMetricFamilySamples(filteredMetrics));
+            TextFormat.write004(buffer, new FilteredMetricEnumeration(collectorRegistry.metricFamilySamples().asIterator()));
             cachedMetrics.set(buffer.toString());
         } catch (IOException e) {
             logger.debug("Unable to collect metrics");
         }
     }
-
-    private List<String> getMetricNames() {
-        Enumeration<Collector.MetricFamilySamples> metricFamilySamplesEnumeration = collectorRegistry.metricFamilySamples();
-        List<String> allMetricNames = new ArrayList<>();
-        while (metricFamilySamplesEnumeration.hasMoreElements()) {
-            Collector.MetricFamilySamples familySamples = metricFamilySamplesEnumeration.nextElement();
-            if (familySamples != null && familySamples.name != null) {
-                allMetricNames.add(familySamples.name);
-            }
-        }
-        return allMetricNames;
-    }
 }


Test patch:
diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumerationTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumerationTest.java
new file mode 100644
index 0000000..8e5fa46
--- /dev/null
+++ b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumerationTest.java
@@ -0,0 +1,45 @@
+package org.jenkinsci.plugins.prometheus.config.disabledmetrics;
+
+import io.prometheus.client.Collector;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+
+import static org.mockito.Mockito.mockStatic;
+
+public class FilteredMetricEnumerationTest {
+
+    @Test
+    void testFilterMatches() {
+        try (MockedStatic<MetricStatusChecker> statusCheckerMockedStatic = mockStatic(MetricStatusChecker.class)) {
+
+            statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_1")).thenReturn(false);
+            statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_2")).thenReturn(true);
+
+            List<Collector.MetricFamilySamples> list = List.of(
+                    new Collector.MetricFamilySamples("metric_1", Collector.Type.GAUGE, "help1", List.of()),
+                    new Collector.MetricFamilySamples("metric_2", Collector.Type.GAUGE, "help2", List.of())
+            );
+
+            Iterator<Collector.MetricFamilySamples> iterator = list.iterator();
+
+
+            FilteredMetricEnumeration filteredMetricEnumeration = new FilteredMetricEnumeration(iterator);
+
+            int foundElements = 0;
+            String foundKey = "";
+            while (filteredMetricEnumeration.hasMoreElements()) {
+                Collector.MetricFamilySamples familySamples = filteredMetricEnumeration.nextElement();
+                foundKey = familySamples.name;
+                foundElements++;
+            }
+            Assertions.assertEquals(1, foundElements);
+            Assertions.assertEquals("metric_2", foundKey);
+
+         }
+    }
+}
\ No newline at end of file