Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new metric for Quiet Mode status #687

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/metrics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
| default_jenkins_up | Shows if jenkins ready to receive requests | gauge |
| default_jenkins_uptime | Shows time since Jenkins was initialized | gauge |
| default_jenkins_nodes_online | Shows Nodes online status | gauge |
| default_jenkins_quietdown | Shows if jenkins is in quiet mode | gauge |

Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 80; Actual: 95 Warning documentation

Expected: 80; Actual: 95

## JobCollector

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public List<MetricFamilySamples> collect() {
collectors.add(factory.createJenkinsCollector(CollectorType.JENKINS_UP_GAUGE, new String[]{}));
collectors.add(factory.createJenkinsCollector(CollectorType.JENKINS_UPTIME_GAUGE, new String[]{}));
collectors.add(factory.createJenkinsCollector(CollectorType.NODES_ONLINE_GAUGE, new String[]{"node"}));
collectors.add(factory.createJenkinsCollector(CollectorType.JENKINS_QUIETDOWN_GAUGE, new String[]{}));

collectors.forEach(c -> c.calculateMetric(jenkins, new String[]{}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum CollectorType {
JENKINS_UP_GAUGE("up"),
JENKINS_UPTIME_GAUGE("uptime"),
JENKINS_VERSION_INFO_GAUGE("version"),
JENKINS_QUIETDOWN_GAUGE("quietdown"),
NODES_ONLINE_GAUGE("nodes_online"),
BUILD_DURATION_GAUGE("build_duration_milliseconds"),
BUILD_LOGFILE_SIZE_GAUGE("build_logfile_size_bytes"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class JenkinsCollectorFactory extends BaseCollectorFactory {
switch (type) {
case JENKINS_UP_GAUGE:
return saveBuildCollector(new JenkinsUpGauge(labelNames, namespace, subsystem));
case JENKINS_QUIETDOWN_GAUGE:
return saveBuildCollector(new JenkinsQuietDownGauge(labelNames, namespace, subsystem));
case NODES_ONLINE_GAUGE:
if (!isNodeOnlineGaugeEnabled()) {
return new NoOpMetricCollector<>();
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void shouldProduceNodeMetrics() {

PrometheusConfiguration mockedConfig = mock(PrometheusConfiguration.class);
String namespace = "TestNamespace";
int numberOfMetrics = 4;

when(mockedConfig.getDefaultNamespace()).thenReturn(namespace);
when(mockedConfig.isCollectNodeStatus()).thenReturn(false);
Expand All @@ -34,7 +35,7 @@ public void shouldProduceNodeMetrics() {
JenkinsStatusCollector jenkinsStatusCollector = new JenkinsStatusCollector();

List<MetricFamilySamples> samples = jenkinsStatusCollector.collect();
assertEquals(3, samples.size());
assertEquals(numberOfMetrics, samples.size());
}
}
}
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);
}
}
Loading