Skip to content

Commit

Permalink
Fixes #554. Apply Metric Filter on all external metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Waschndolos authored Sep 14, 2023
1 parent a85d274 commit 3affe77
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.jenkinsci.plugins.prometheus.config.disabledmetrics;

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class MetricStatusChecker {

Expand Down Expand Up @@ -51,4 +54,11 @@ public static boolean isEnabled(String metricName) {
}
return true;
}

public static Set<String> filter(List<String> allMetricNames) {
if (allMetricNames == null) {
return new HashSet<>();
}
return allMetricNames.stream().filter(MetricStatusChecker::isEnabled).collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
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.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 @@ -49,10 +51,23 @@ public String getMetrics() {
@Override
public void collectMetrics() {
try (StringWriter buffer = new StringWriter()) {
TextFormat.write004(buffer, collectorRegistry.metricFamilySamples());
Set<String> filteredMetrics = MetricStatusChecker.filter(getMetricNames());
TextFormat.write004(buffer, collectorRegistry.filteredMetricFamilySamples(filteredMetrics));
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
Expand Up @@ -9,6 +9,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -181,6 +182,32 @@ void testNamedDisabledMetricAndRegexAvailableMatches() {
}
}


@Test
void testFilterExternalMetricNames() {
Jenkins jenkins = mock(Jenkins.class);

PrometheusConfiguration mockedConfig = mock(PrometheusConfiguration.class);
List<Entry> entries = new ArrayList<>();
entries.add(new NamedDisabledMetric("other_metric"));
entries.add(new RegexDisabledMetric("j?vm.*"));
DisabledMetricConfig disabledMetricConfig = new DisabledMetricConfig(entries);

when(mockedConfig.getDisabledMetricConfig()).thenReturn(disabledMetricConfig);


try (MockedStatic<Jenkins> jenkinsStatic = mockStatic(Jenkins.class);
MockedStatic<PrometheusConfiguration> configStatic = mockStatic(PrometheusConfiguration.class)) {
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins);
configStatic.when(PrometheusConfiguration::get).thenReturn(mockedConfig);

List<String> allMetrics = List.of("some_metric", "jvm_xxx", "vm_xxx");
Set<String> filteredMetrics = MetricStatusChecker.filter(allMetrics);
Assertions.assertEquals(1, filteredMetrics.size());
Assertions.assertEquals("some_metric", filteredMetrics.stream().findFirst().get());
}
}

@Test
// shouldn't take more than 3 seconds
@Timeout(value = 3L)
Expand Down

0 comments on commit 3affe77

Please sign in to comment.