From eaecaf6592eb2954aa33c935fed6a0e5e7234f0e Mon Sep 17 00:00:00 2001 From: Waschndolos Date: Fri, 23 Aug 2024 17:38:03 +0200 Subject: [PATCH] Do not create Collectors for each Job. (#692) --- .../prometheus/CodeCoverageCollector.java | 41 +++++++++-------- .../prometheus/CodeCoverageCollectorTest.java | 46 ------------------- 2 files changed, 22 insertions(+), 65 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java b/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java index b3a22a695..19bc71429 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java @@ -1,11 +1,9 @@ package org.jenkinsci.plugins.prometheus; -import hudson.model.Job; import hudson.model.Run; import io.jenkins.plugins.coverage.metrics.steps.CoverageBuildAction; import io.prometheus.client.Collector; import jenkins.model.Jenkins; -import org.apache.commons.collections.CollectionUtils; import org.jenkinsci.plugins.prometheus.collectors.CollectorFactory; import org.jenkinsci.plugins.prometheus.collectors.CollectorType; import org.jenkinsci.plugins.prometheus.collectors.MetricCollector; @@ -36,25 +34,12 @@ public List collect() { return Collections.emptyList(); } - List> samples = new ArrayList<>(); - Jobs.forEachJob(job -> CollectionUtils.addIgnoreNull(samples, collectCoverageMetricForJob(job))); + List, ? extends Collector>> collectors = createCollectors(); - - return samples.stream().flatMap(Collection::stream).collect(Collectors.toList()); + return collectCoverageMetricForJob(collectors); } - private List collectCoverageMetricForJob(Job job) { - - Run lastBuild = job.getLastBuild(); - if (lastBuild == null || lastBuild.isBuilding()) { - return Collections.emptyList(); - } - - CoverageBuildAction coverageBuildAction = lastBuild.getAction(CoverageBuildAction.class); - if (coverageBuildAction == null) { - return Collections.emptyList(); - } - + private List, ? extends Collector>> createCollectors() { CollectorFactory factory = new CollectorFactory(); List, ? extends Collector>> collectors = new ArrayList<>(); @@ -85,13 +70,31 @@ private List collectCoverageMetricForJob(Job job) { collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_TOTAL, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_PERCENT, new String[]{jobAttributeName})); - collectors.forEach(c -> c.calculateMetric(lastBuild, new String[]{job.getFullName()})); + return collectors; + } + + private List collectCoverageMetricForJob(List, ? extends Collector>> collectors) { + + Jobs.forEachJob(job -> { + Run lastBuild = job.getLastBuild(); + if (lastBuild == null || lastBuild.isBuilding()) { + return; + } + + CoverageBuildAction coverageBuildAction = lastBuild.getAction(CoverageBuildAction.class); + if (coverageBuildAction == null) { + return; + } + + collectors.forEach(c -> c.calculateMetric(lastBuild, new String[]{job.getFullName()})); + }); return collectors.stream() .map(MetricCollector::collect) .flatMap(Collection::stream) .collect(Collectors.toList()); } + private boolean isCoveragePluginLoaded() { return Jenkins.get().getPlugin("coverage") != null; } diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollectorTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollectorTest.java index 6cf55451d..041d7fed9 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollectorTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollectorTest.java @@ -59,52 +59,6 @@ void shouldNotProduceMetricsWhenItIsNotConfigured() { } } - @Test - void shouldNotProduceMetricsWhenJobIsBuilding() { - try ( - MockedStatic jenkinsStatic = mockStatic(Jenkins.class); - MockedStatic configurationStatic = mockStatic(PrometheusConfiguration.class); - ) { - jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); - configurationStatic.when(PrometheusConfiguration::get).thenReturn(config); - Job jobUnderTest = mock(Job.class); - Run lastBuild = mock(Run.class); - when(lastBuild.isBuilding()).thenReturn(true); - when(jobUnderTest.getLastBuild()).thenReturn(lastBuild); - when(jenkins.getAllItems(Job.class)).thenReturn(List.of(jobUnderTest)); - when(jenkins.getPlugin("coverage")).thenReturn(new Plugin.DummyImpl()); - when(config.isCollectCodeCoverage()).thenReturn(true); - - CodeCoverageCollector sut = new CodeCoverageCollector(); - - List collect = sut.collect(); - assertEquals(0, collect.size()); - } - } - - @Test - void shouldNotProduceMetricsWhenJobHasNoCoverageBuildAction() { - try ( - MockedStatic jenkinsStatic = mockStatic(Jenkins.class); - MockedStatic configurationStatic = mockStatic(PrometheusConfiguration.class); - ) { - jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); - configurationStatic.when(PrometheusConfiguration::get).thenReturn(config); - Job jobUnderTest = mock(Job.class); - Run lastBuild = mock(Run.class); - when(lastBuild.isBuilding()).thenReturn(false); - when(jobUnderTest.getLastBuild()).thenReturn(lastBuild); - when(jenkins.getAllItems(Job.class)).thenReturn(List.of(jobUnderTest)); - when(jenkins.getPlugin("coverage")).thenReturn(new Plugin.DummyImpl()); - when(config.isCollectCodeCoverage()).thenReturn(true); - - CodeCoverageCollector sut = new CodeCoverageCollector(); - - List collect = sut.collect(); - assertEquals(0, collect.size()); - } - } - @Test void shouldProduceMetricsWhenJobHasCoverageBuildAction() { try (