Skip to content

Commit

Permalink
Fixing filtering of external metrics (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
Waschndolos authored Sep 18, 2023
1 parent 7a4429f commit bc33d87
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Check warning on line 39 in src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumeration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 39 is not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -51,23 +50,10 @@ public String getMetrics() {
@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()));

Check warning on line 53 in src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 53 is not covered by tests
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;
}
}
Original file line number Diff line number Diff line change
@@ -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);

}
}
}

0 comments on commit bc33d87

Please sign in to comment.