diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/metrics/jobs/CurrentRunDurationGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/metrics/jobs/CurrentRunDurationGauge.java index cf56a7cbe..661da88d5 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/metrics/jobs/CurrentRunDurationGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/metrics/jobs/CurrentRunDurationGauge.java @@ -3,7 +3,6 @@ import hudson.model.Job; import hudson.model.Run; import io.prometheus.client.Gauge; -import org.jenkinsci.plugins.prometheus.util.ArrayUtils; import java.time.Clock; @@ -19,28 +18,21 @@ protected Gauge initCollector() { .name(calculateName("running_build_duration_milliseconds")) .subsystem(subsystem) .namespace(namespace) - .labelNames(ArrayUtils.appendToArray(labelNames, "building")) + .labelNames(labelNames) .help("Indicates the runtime of the run currently building if there is a run currently building") .create(); } @Override public void calculateMetric(Job jenkinsObject, String[] labelValues) { - boolean isBuilding = jenkinsObject.isBuilding(); - String[] labels = ArrayUtils.appendToArray(labelValues, String.valueOf(isBuilding)); - if (isBuilding) { - Run runningBuild = jenkinsObject.getLastBuild(); - if (runningBuild != null) { - long start = runningBuild.getStartTimeInMillis(); - // Using Clock to be able to mock in test - long end = Clock.systemUTC().millis(); - long duration = Math.max(end - start, 0); - this.collector.labels(labels).set(duration); - return; - } + Run runningBuild = jenkinsObject.getLastBuild(); + if (runningBuild != null && runningBuild.isBuilding()) { + long start = runningBuild.getStartTimeInMillis(); + // Using Clock to be able to mock in test + long end = Clock.systemUTC().millis(); + long duration = Math.max(end - start, 0); + this.collector.labels(labelValues).set(duration); } - - this.collector.labels(labels).set(0.0); } } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/metrics/jobs/NbBuildsGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/metrics/jobs/NbBuildsGauge.java index 002e93e83..b40556d42 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/metrics/jobs/NbBuildsGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/metrics/jobs/NbBuildsGauge.java @@ -3,6 +3,7 @@ import hudson.model.Job; import io.prometheus.client.Gauge; + public class NbBuildsGauge extends BaseJobMetricCollector { public NbBuildsGauge(String[] labelNames, String namespace, String subsystem) { diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/metrics/jobs/CurrentRunDurationGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/metrics/jobs/CurrentRunDurationGaugeTest.java index 84540ab34..44b3e4640 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/metrics/jobs/CurrentRunDurationGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/metrics/jobs/CurrentRunDurationGaugeTest.java @@ -22,7 +22,7 @@ public class CurrentRunDurationGaugeTest extends JobCollectorTest { @Override @Test public void testCollectResult() { - when(job.isBuilding()).thenReturn(true); + when(currentRun.isBuilding()).thenReturn(true); when(currentRun.getStartTimeInMillis()).thenReturn(1000L); when(job.getLastBuild()).thenReturn(currentRun); @@ -48,7 +48,7 @@ public void testCollectResult() { @Test public void testCurrentlyRunningLabelValue() { - when(job.isBuilding()).thenReturn(true); + when(currentRun.isBuilding()).thenReturn(true); when(currentRun.getStartTimeInMillis()).thenReturn(1000L); when(job.getLastBuild()).thenReturn(currentRun); @@ -59,11 +59,8 @@ public void testCurrentlyRunningLabelValue() { List labelNames = sut.collect().get(0).samples.get(0).labelNames; List labelValues = sut.collect().get(0).samples.get(0).labelValues; - Assertions.assertEquals(3, labelNames.size()); - Assertions.assertEquals(3, labelValues.size()); - - Assertions.assertEquals("building", labelNames.get(2)); - Assertions.assertEquals("true", labelValues.get(2)); + Assertions.assertEquals(2, labelNames.size()); + Assertions.assertEquals(2, labelValues.size()); double value = sut.collect().get(0).samples.get(0).value; @@ -71,25 +68,15 @@ public void testCurrentlyRunningLabelValue() { } @Test - public void testCurrentlyNotRunningLabelValue() { - when(job.isBuilding()).thenReturn(false); + public void testCurrentlyNotRunning() { + when(currentRun.isBuilding()).thenReturn(false); + when(job.getLastBuild()).thenReturn(currentRun); CurrentRunDurationGauge sut = new CurrentRunDurationGauge(new String[]{"jenkins_job", "repo"}, "default", "jenkins"); sut.calculateMetric(job, new String[]{"job1", "NA"}); - List labelNames = sut.collect().get(0).samples.get(0).labelNames; - List labelValues = sut.collect().get(0).samples.get(0).labelValues; - - Assertions.assertEquals(3, labelNames.size()); - Assertions.assertEquals(3, labelValues.size()); - - Assertions.assertEquals("building", labelNames.get(2)); - Assertions.assertEquals("false", labelValues.get(2)); - - double value = sut.collect().get(0).samples.get(0).value; - - Assertions.assertEquals(0.0, value); + Assertions.assertEquals(1, sut.collect().size(), "Expected not to be calculated"); }