Skip to content

Commit

Permalink
Adding configuration option to exclude jobs from Prometheus metrics v…
Browse files Browse the repository at this point in the history
…ia Regex (#699)

* Adding configuration option to exclude jobs from Prometheus metrics

* Removing unused parameter

---------

Co-authored-by: Waschndolos <[email protected]>
  • Loading branch information
Waschndolos and Waschndolos authored Sep 22, 2024
1 parent 7425fa1 commit 52e8f47
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;
import org.jenkinsci.plugins.prometheus.collectors.MetricCollector;
import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration;
import org.jenkinsci.plugins.prometheus.config.disabledmetrics.MetricStatusChecker;
import org.jenkinsci.plugins.prometheus.util.Jobs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -80,6 +81,10 @@ private List<MetricFamilySamples> collectCoverageMetricForJob(List<MetricCollect
if (lastBuild == null || lastBuild.isBuilding()) {
return;
}
if (!MetricStatusChecker.isJobEnabled(job.getFullName())) {

Check warning on line 84 in src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 84 is only partially covered, one branch is missing
LOGGER.debug("Job '{}' is excluded by configuration", job.getFullName());
return;

Check warning on line 86 in src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 85-86 are not covered by tests
}

CoverageBuildAction coverageBuildAction = lastBuild.getAction(CoverageBuildAction.class);
if (coverageBuildAction == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.jenkinsci.plugins.prometheus;

import com.cloudbees.simplediskusage.DiskItem;
import com.cloudbees.simplediskusage.JobDiskItem;
import edu.umd.cs.findbugs.annotations.NonNull;
import com.cloudbees.simplediskusage.JobDiskItem;
import edu.umd.cs.findbugs.annotations.NonNull;
import io.prometheus.client.Collector;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.prometheus.collectors.CollectorFactory;
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;
import org.jenkinsci.plugins.prometheus.collectors.MetricCollector;
import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration;
import org.jenkinsci.plugins.prometheus.config.disabledmetrics.MetricStatusChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -18,8 +19,8 @@
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import java.util.stream.Stream;

public class DiskUsageCollector extends Collector {

private static final Logger LOGGER = LoggerFactory.getLogger(DiskUsageCollector.class);
Expand Down Expand Up @@ -68,6 +69,9 @@ private static List<MetricFamilySamples> collectDiskUsage() throws IOException {
jobDiskItemCollectors.add(factory.createJobDiskItemCollector(CollectorType.JOB_USAGE_BYTES_GAUGE, new String[]{"file_store", "jobName", "url"}));

diskUsagePlugin.getJobsUsages().forEach(i -> {
if (!MetricStatusChecker.isJobEnabled(i.getFullName())) {

Check warning on line 72 in src/main/java/org/jenkinsci/plugins/prometheus/DiskUsageCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 72 is only partially covered, one branch is missing
return;

Check warning on line 73 in src/main/java/org/jenkinsci/plugins/prometheus/DiskUsageCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 73 is not covered by tests
}
final Optional<FileStore> fileStore = getFileStore(i.getPath());
fileStore.ifPresent(usedFileStores::add);
jobDiskItemCollectors.forEach(c -> c.calculateMetric(i, new String[]{toLabelValue(fileStore), i.getFullName(), i.getUrl()}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jenkinsci.plugins.prometheus.collectors.builds.CounterManager;
import org.jenkinsci.plugins.prometheus.collectors.builds.JobLabel;
import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration;
import org.jenkinsci.plugins.prometheus.config.disabledmetrics.MetricStatusChecker;
import org.jenkinsci.plugins.prometheus.util.Jobs;
import org.jenkinsci.plugins.prometheus.util.Runs;
import org.slf4j.Logger;
Expand Down Expand Up @@ -158,6 +159,10 @@ public List<MetricFamilySamples> collect() {
LOGGER.debug("job [{}] is disabled", job.getFullName());
return;
}
if (!MetricStatusChecker.isJobEnabled(job.getFullName())) {
LOGGER.debug("Job [{}] is excluded by configuration", job.getFullName());
return;

Check warning on line 164 in src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 162-164 are not covered by tests
}
LOGGER.debug("Collecting metrics for job [{}]", job.getFullName());
appendJobMetrics(job);
} catch (IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jenkinsci.plugins.prometheus.config.disabledmetrics;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Descriptor;
import org.kohsuke.stapler.DataBoundConstructor;

public class JobRegexDisabledMetric extends Entry {

private final String regex;

@DataBoundConstructor
public JobRegexDisabledMetric(String regex) {
this.regex = regex;
}

public String getRegex() {
return regex;
}

@Override
public Descriptor<Entry> getDescriptor() {
return new DescriptorImpl();

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 13-23 are not covered by tests
}

@Extension
public static class DescriptorImpl extends Descriptor<Entry> {
@Override
@NonNull
public String getDisplayName() {
return "Job Regex Entry";
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,7 @@ public class MetricStatusChecker {

public static boolean isEnabled(String metricName) {

PrometheusConfiguration configuration = PrometheusConfiguration.get();
if (configuration == null) {
LOGGER.warn("Cannot check if metric is enabled. Unable to get PrometheusConfiguration");
return true;
}

DisabledMetricConfig disabledMetricConfig = configuration.getDisabledMetricConfig();
if (disabledMetricConfig == null) {
LOGGER.debug("Cannot check if metric is enabled. No DisabledMetricConfig.");
return true;
}

List<Entry> entries = disabledMetricConfig.getEntries();
if (entries == null || entries.isEmpty()) {
LOGGER.debug("Cannot check if metric is enabled. No entries specified in DisabledMetricConfig.");
return true;
}
List<Entry> entries = getEntries();

for (Entry entry : entries) {
if (entry instanceof RegexDisabledMetric) {
Expand All @@ -55,10 +39,47 @@ public static boolean isEnabled(String metricName) {
return true;
}

public static boolean isJobEnabled(String jobName) {

List<Entry> entries = getEntries();

for (Entry entry : entries) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 46 is only partially covered, one branch is missing
if (entry instanceof JobRegexDisabledMetric) {
Pattern pattern = Pattern.compile(((JobRegexDisabledMetric) entry).getRegex());
Matcher matcher = pattern.matcher(jobName);
if (matcher.matches()) {
LOGGER.debug("Job named '{}' is disabled via Jenkins Prometheus Plugin configuration. Reason: JobRegexDisabledMetric", jobName);
return false;
}
}
}

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 47-55 are not covered by tests
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());
}

private static List<Entry> getEntries() {
PrometheusConfiguration configuration = PrometheusConfiguration.get();
if (configuration == null) {
LOGGER.warn("Cannot check if job is enabled. No PrometheusConfiguration");
return List.of();
}
DisabledMetricConfig disabledMetricConfig = configuration.getDisabledMetricConfig();
if (disabledMetricConfig == null) {
LOGGER.debug("Cannot check if metric is enabled. No DisabledMetricConfig.");
return List.of();
}

List<Entry> entries = disabledMetricConfig.getEntries();
if (entries == null || entries.isEmpty()) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 79 is only partially covered, one branch is missing
LOGGER.debug("Cannot check if metric is enabled. No entries specified in DisabledMetricConfig.");
return List.of();
}
return entries;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry field="regex" title="Regex matching metric for Jobs" description="Enter a regex matching job full names you want to exclude from calculation">
<f:textbox/>
</f:entry>
</j:jelly>

0 comments on commit 52e8f47

Please sign in to comment.