Skip to content

Commit

Permalink
Bugfix/515 (#517)
Browse files Browse the repository at this point in the history
* Removing running label. Only calculating metric if there is a running build
  • Loading branch information
Waschndolos authored Apr 18, 2023
1 parent 7a8f517 commit 8e410b1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hudson.model.Job;
import io.prometheus.client.Gauge;


public class NbBuildsGauge extends BaseJobMetricCollector<Job, Gauge> {

public NbBuildsGauge(String[] labelNames, String namespace, String subsystem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -59,37 +59,24 @@ public void testCurrentlyRunningLabelValue() {
List<String> labelNames = sut.collect().get(0).samples.get(0).labelNames;
List<String> 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;

Assertions.assertNotEquals(0.0, value);
}

@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<String> labelNames = sut.collect().get(0).samples.get(0).labelNames;
List<String> 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");
}


Expand Down

0 comments on commit 8e410b1

Please sign in to comment.