From 23530b08c91b59c3bfcaa02df20dee3b62d4efe9 Mon Sep 17 00:00:00 2001 From: waschndolos Date: Sun, 17 Sep 2023 09:08:56 +0200 Subject: [PATCH] Fixing filtering of external metrics --- .../FilteredMetricEnumeration.java | 41 +++++++++++++++++ .../service/DefaultPrometheusMetrics.java | 18 +------- .../FilteredMetricEnumerationTest.java | 45 +++++++++++++++++++ 3 files changed, 88 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumeration.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumerationTest.java 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 000000000..d531b9374 --- /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 { + + private final Iterator filteredList; + + public FilteredMetricEnumeration(Iterator fullList) { + this.filteredList = filterList(fullList); + } + + private Iterator filterList(Iterator fullList) { + List 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 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 2b7935a9b..9024c9c3d 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.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 String getMetrics() { @Override public void collectMetrics() { try (StringWriter buffer = new StringWriter()) { - Set 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 getMetricNames() { - Enumeration metricFamilySamplesEnumeration = collectorRegistry.metricFamilySamples(); - List allMetricNames = new ArrayList<>(); - while (metricFamilySamplesEnumeration.hasMoreElements()) { - Collector.MetricFamilySamples familySamples = metricFamilySamplesEnumeration.nextElement(); - if (familySamples != null && familySamples.name != null) { - allMetricNames.add(familySamples.name); - } - } - return allMetricNames; - } } 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 000000000..8e5fa46a2 --- /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 statusCheckerMockedStatic = mockStatic(MetricStatusChecker.class)) { + + statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_1")).thenReturn(false); + statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_2")).thenReturn(true); + + List 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 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