forked from lsjostro/prometheus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding metric which shows if job log is updated
- Loading branch information
waschndolos
committed
Oct 25, 2023
1 parent
3dc4362
commit 2e350ec
Showing
9 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/org/jenkinsci/plugins/prometheus/collectors/jobs/LogUpdatedGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/java/org/jenkinsci/plugins/prometheus/collectors/jobs/LogUpdatedGaugeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |