From 52e8f47488fc5d8d2a3da166cc11da24d76d30e0 Mon Sep 17 00:00:00 2001 From: Waschndolos Date: Sun, 22 Sep 2024 08:19:04 +0200 Subject: [PATCH] Adding configuration option to exclude jobs from Prometheus metrics via Regex (#699) * Adding configuration option to exclude jobs from Prometheus metrics * Removing unused parameter --------- Co-authored-by: Waschndolos --- .../prometheus/CodeCoverageCollector.java | 5 ++ .../prometheus/DiskUsageCollector.java | 12 ++-- .../plugins/prometheus/JobCollector.java | 5 ++ .../JobRegexDisabledMetric.java | 35 ++++++++++++ .../disabledmetrics/MetricStatusChecker.java | 55 +++++++++++++------ .../JobRegexDisabledMetric/config.jelly | 6 ++ 6 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/JobRegexDisabledMetric.java create mode 100644 src/main/resources/org/jenkinsci/plugins/prometheus/config/disabledmetrics/JobRegexDisabledMetric/config.jelly diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java b/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java index 19bc71429..94d812721 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java @@ -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; @@ -80,6 +81,10 @@ private List collectCoverageMetricForJob(List 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())) { + return; + } final Optional fileStore = getFileStore(i.getPath()); fileStore.ifPresent(usedFileStores::add); jobDiskItemCollectors.forEach(c -> c.calculateMetric(i, new String[]{toLabelValue(fileStore), i.getFullName(), i.getUrl()})); diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java b/src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java index 57d66224e..8ba02f908 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java @@ -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; @@ -158,6 +159,10 @@ public List collect() { LOGGER.debug("job [{}] is disabled", job.getFullName()); return; } + if (!MetricStatusChecker.isJobEnabled(job.getFullName())) { + LOGGER.debug("Job [{}] is excluded by configuration", job.getFullName()); + return; + } LOGGER.debug("Collecting metrics for job [{}]", job.getFullName()); appendJobMetrics(job); } catch (IllegalArgumentException e) { diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/JobRegexDisabledMetric.java b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/JobRegexDisabledMetric.java new file mode 100644 index 000000000..25cfe6586 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/JobRegexDisabledMetric.java @@ -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 getDescriptor() { + return new DescriptorImpl(); + } + + @Extension + public static class DescriptorImpl extends Descriptor { + @Override + @NonNull + public String getDisplayName() { + return "Job Regex Entry"; + } + + } +} diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusChecker.java b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusChecker.java index 4dcd870e8..e7a4745c4 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusChecker.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusChecker.java @@ -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 entries = disabledMetricConfig.getEntries(); - if (entries == null || entries.isEmpty()) { - LOGGER.debug("Cannot check if metric is enabled. No entries specified in DisabledMetricConfig."); - return true; - } + List entries = getEntries(); for (Entry entry : entries) { if (entry instanceof RegexDisabledMetric) { @@ -55,10 +39,47 @@ public static boolean isEnabled(String metricName) { return true; } + public static boolean isJobEnabled(String jobName) { + + List entries = getEntries(); + + for (Entry entry : entries) { + 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; + } + } + } + return true; + } + public static Set filter(List allMetricNames) { if (allMetricNames == null) { return new HashSet<>(); } return allMetricNames.stream().filter(MetricStatusChecker::isEnabled).collect(Collectors.toSet()); } + + private static List 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 entries = disabledMetricConfig.getEntries(); + if (entries == null || entries.isEmpty()) { + LOGGER.debug("Cannot check if metric is enabled. No entries specified in DisabledMetricConfig."); + return List.of(); + } + return entries; + } } diff --git a/src/main/resources/org/jenkinsci/plugins/prometheus/config/disabledmetrics/JobRegexDisabledMetric/config.jelly b/src/main/resources/org/jenkinsci/plugins/prometheus/config/disabledmetrics/JobRegexDisabledMetric/config.jelly new file mode 100644 index 000000000..8cc48f7f4 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/prometheus/config/disabledmetrics/JobRegexDisabledMetric/config.jelly @@ -0,0 +1,6 @@ + + + + + +