Skip to content

Commit

Permalink
Adding metric which shows if job log is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
waschndolos committed Oct 25, 2023
1 parent 3dc4362 commit 2e350ec
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class JobCollector extends Collector {
private MetricCollector<Job<?, ?>, ? extends Collector> nbBuildsGauge;
private MetricCollector<Job<?, ?>, ? extends Collector> buildDiscardGauge;
private MetricCollector<Job<?, ?>, ? extends Collector> currentRunDurationGauge;
private MetricCollector<Job<?,?>, ? extends Collector> logUpdatedGauge;

private static class BuildMetrics {

Expand Down Expand Up @@ -129,6 +130,8 @@ public List<MetricFamilySamples> collect() {

currentRunDurationGauge = factory.createJobCollector(CollectorType.CURRENT_RUN_DURATION_GAUGE, labelBaseNameArray);

logUpdatedGauge = factory.createJobCollector(CollectorType.JOB_LOG_UPDATED_GAUGE, labelBaseNameArray);

if (PrometheusConfiguration.get().isPerBuildMetrics()) {
labelNameArray = Arrays.copyOf(labelNameArray, labelNameArray.length + 1);
labelNameArray[labelNameArray.length - 1] = "number";
Expand Down Expand Up @@ -164,6 +167,7 @@ public List<MetricFamilySamples> collect() {
addSamples(samples, nbBuildsGauge.collect(), "Adding [{}] samples from gauge ({})");
addSamples(samples, buildDiscardGauge.collect(), "Adding [{}] samples from gauge ({})");
addSamples(samples, currentRunDurationGauge.collect(), "Adding [{}] samples from gauge ({})");
addSamples(samples, logUpdatedGauge.collect(), "Adding [{}] samples from gauge ({})");
addSamples(samples, lastBuildMetrics);
if (PrometheusConfiguration.get().isPerBuildMetrics()) {
addSamples(samples, perBuildMetrics);
Expand Down Expand Up @@ -217,6 +221,8 @@ protected void appendJobMetrics(Job<?, ?> job) {
jobHealthScoreGauge.calculateMetric(job, baseLabelValueArray);
buildDiscardGauge.calculateMetric(job, baseLabelValueArray);
currentRunDurationGauge.calculateMetric(job, baseLabelValueArray);
logUpdatedGauge.calculateMetric(job, baseLabelValueArray);

processRun(job, lastBuild, baseLabelValueArray, lastBuildMetrics);

Run<?, ?> run = lastBuild;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public enum CollectorType {
COVERAGE_FILE_MISSED("coverage_file_missed"),
COVERAGE_FILE_TOTAL("coverage_file_total"),



;
JOB_LOG_UPDATED_GAUGE("job_log_updated");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public JobCollectorFactory() {
return saveBuildCollector(new BuildDiscardGauge(labelNames, namespace, subsystem));
case CURRENT_RUN_DURATION_GAUGE:
return saveBuildCollector(new CurrentRunDurationGauge(labelNames, namespace, subsystem));
case JOB_LOG_UPDATED_GAUGE:
return saveBuildCollector(new LogUpdatedGauge(labelNames, namespace, subsystem));
default:
return new NoOpMetricCollector<>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jenkinsci.plugins.prometheus.collectors.jobs;

import hudson.model.Job;
import io.prometheus.client.Gauge;
import io.prometheus.client.SimpleCollector;
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;
import org.jenkinsci.plugins.prometheus.collectors.builds.BuildsMetricCollector;

public class LogUpdatedGauge extends BuildsMetricCollector<Job<?, ?>, Gauge> {

protected LogUpdatedGauge(String[] labelNames, String namespace, String subsystem) {
super(labelNames, namespace, subsystem);
}

@Override
protected CollectorType getCollectorType() {
return CollectorType.JOB_LOG_UPDATED_GAUGE;
}

@Override
protected String getHelpText() {
return "Provides a hint if a job is still logging. Maybe not 100% accurate - but a good hint.";
}

@Override
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() {
return Gauge.build();
}

@Override
public void calculateMetric(Job<?, ?> jenkinsObject, String[] labelValues) {

if (jenkinsObject != null) {
boolean logUpdated = jenkinsObject.isLogUpdated();
this.collector.labels(labelValues).set(logUpdated ? 1.0 : 0.0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class BuildDiscardGaugeTest extends JobCollectorTest {

@Override
@Test
void testCollectResult() {
when(job.getBuildDiscarder()).thenReturn(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class CurrentRunDurationGaugeTest extends JobCollectorTest {
@Mock
Run currentRun;

@Override
@Test
public void testCollectResult() {
when(currentRun.isBuilding()).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

public class HealthScoreGaugeTest extends JobCollectorTest {

@Override
@Test
public void testCollectResult() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ public abstract class JobCollectorTest extends CollectorTest {
protected Job job;


abstract void testCollectResult();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.jenkinsci.plugins.prometheus.collectors.jobs;

import io.prometheus.client.Collector;
import org.junit.Test;

import java.util.List;

import static org.mockito.Mockito.when;

public class LogUpdatedGaugeTest extends JobCollectorTest {


@Test
public void testBasicAttributes() {
when(job.isLogUpdated()).thenReturn(true);

LogUpdatedGauge sut = new LogUpdatedGauge(new String[]{"jenkins_job", "repo"}, "default", "jenkins");

sut.calculateMetric(job, new String[]{"job1", "NA"});
List<Collector.MetricFamilySamples> collect = sut.collect();

validateMetricFamilySampleListSize(collect, 1);

Collector.MetricFamilySamples samples = collect.get(0);
validateNames(samples, new String[]{"default_jenkins_builds_job_log_updated"});
validateMetricFamilySampleSize(samples, 1);

}

@Test
public void testLogIsUpdatedReturnsOne() {

when(job.isLogUpdated()).thenReturn(true);

LogUpdatedGauge sut = new LogUpdatedGauge(new String[]{"jenkins_job", "repo"}, "default", "jenkins");

sut.calculateMetric(job, new String[]{"job1", "NA"});
List<Collector.MetricFamilySamples> collect = sut.collect();
Collector.MetricFamilySamples samples = collect.get(0);
validateValue(samples.samples.get(0), 1.0);
}

@Test
public void testLogIsNotUpdatedReturnsZero() {

when(job.isLogUpdated()).thenReturn(false);

LogUpdatedGauge sut = new LogUpdatedGauge(new String[]{"jenkins_job", "repo"}, "default", "jenkins");

sut.calculateMetric(job, new String[]{"job1", "NA"});
List<Collector.MetricFamilySamples> collect = sut.collect();
Collector.MetricFamilySamples samples = collect.get(0);
validateValue(samples.samples.get(0), 0.0);
}
}

0 comments on commit 2e350ec

Please sign in to comment.