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.
New metric indicating whether Jenkins is in quietdown mode
This metric exposes the Jenkins isQuietingDown status as a gauge. When Jenkins is in quiet mode, the new metric will represent this as a '1', otherwise the value will be '0' Exposing this metric allows Prometheus to determine whether Jenkins is in quiet mode. This can be useful in cases where an alert should be triggered if quiet mode has been enabled for too long or falls outside of a maintenance window. Fixes issue #686
- Loading branch information
Showing
6 changed files
with
113 additions
and
0 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
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
37 changes: 37 additions & 0 deletions
37
src/main/java/org/jenkinsci/plugins/prometheus/collectors/jenkins/JenkinsQuietDownGauge.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,37 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.jenkins; | ||
|
||
import io.prometheus.client.Gauge; | ||
import io.prometheus.client.SimpleCollector; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.prometheus.collectors.BaseMetricCollector; | ||
import org.jenkinsci.plugins.prometheus.collectors.CollectorType; | ||
|
||
public class JenkinsQuietDownGauge extends BaseMetricCollector<Jenkins, Gauge> { | ||
|
||
JenkinsQuietDownGauge(String[] labelNames, String namespace, String subsystem) { | ||
super(labelNames, namespace, subsystem); | ||
} | ||
|
||
@Override | ||
protected CollectorType getCollectorType() { | ||
return CollectorType.JENKINS_QUIETDOWN_GAUGE; | ||
} | ||
|
||
@Override | ||
protected String getHelpText() { | ||
return "Is Jenkins in quiet mode"; | ||
} | ||
|
||
@Override | ||
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() { | ||
return Gauge.build(); | ||
} | ||
|
||
@Override | ||
public void calculateMetric(Jenkins jenkinsObject, String[] labelValues) { | ||
if (jenkinsObject == null) { | ||
return; | ||
} | ||
this.collector.set(jenkinsObject.isQuietingDown() ? 1 : 0); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...t/java/org/jenkinsci/plugins/prometheus/collectors/jenkins/JenkinsQuietDownGaugeTest.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,71 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.jenkins; | ||
|
||
import io.prometheus.client.Collector; | ||
import org.jenkinsci.plugins.prometheus.collectors.testutils.MockedJenkinsTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
public class JenkinsQuietDownGaugeTest extends MockedJenkinsTest { | ||
|
||
|
||
@Test | ||
public void testCollectResultForJenkinsQuietModeEnabled() { | ||
Check notice Code scanning / Pmd (reported by Codacy) JUnit tests should include assert() or fail() Note test
JUnit tests should include assert() or fail()
|
||
|
||
when(mock.isQuietingDown()).thenReturn(true); | ||
|
||
JenkinsQuietDownGauge sut = new JenkinsQuietDownGauge(new String[]{}, getNamespace(), getSubSystem()); | ||
sut.calculateMetric(mock, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
validateMetricFamilySampleListSize(collect, 1); | ||
|
||
Collector.MetricFamilySamples samples = collect.get(0); | ||
|
||
validateNames(samples, new String[]{"default_jenkins_quietdown"}); | ||
validateMetricFamilySampleSize(samples, 1); | ||
validateHelp(samples, "Is Jenkins in quiet mode"); | ||
validateValue(samples, 0, 1.0); | ||
} | ||
|
||
|
||
@Test | ||
public void testCollectResultForJenkinsQuietModeDisabled() { | ||
Check notice Code scanning / Pmd (reported by Codacy) JUnit tests should include assert() or fail() Note test
JUnit tests should include assert() or fail()
|
||
|
||
when(mock.isQuietingDown()).thenReturn(false); | ||
|
||
JenkinsQuietDownGauge sut = new JenkinsQuietDownGauge(new String[]{}, getNamespace(), getSubSystem()); | ||
sut.calculateMetric(mock, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
validateMetricFamilySampleListSize(collect, 1); | ||
|
||
Collector.MetricFamilySamples samples = collect.get(0); | ||
|
||
validateNames(samples, new String[]{"default_jenkins_quietdown"}); | ||
validateMetricFamilySampleSize(samples, 1); | ||
validateHelp(samples, "Is Jenkins in quiet mode"); | ||
validateValue(samples, 0, 0.0); | ||
} | ||
|
||
@Test | ||
public void testJenkinsIsNull() { | ||
Check notice Code scanning / Pmd (reported by Codacy) JUnit tests should include assert() or fail() Note test
JUnit tests should include assert() or fail()
|
||
JenkinsQuietDownGauge sut = new JenkinsQuietDownGauge(new String[]{}, getNamespace(), getSubSystem()); | ||
sut.calculateMetric(null, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
validateMetricFamilySampleListSize(collect, 1); | ||
|
||
Collector.MetricFamilySamples samples = collect.get(0); | ||
|
||
validateNames(samples, new String[]{"default_jenkins_quietdown"}); | ||
validateMetricFamilySampleSize(samples, 1); | ||
validateHelp(samples, "Is Jenkins in quiet mode"); | ||
validateValue(samples, 0, 0.0); | ||
} | ||
} |