diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/DiskUsageCollector.java b/src/main/java/org/jenkinsci/plugins/prometheus/DiskUsageCollector.java index 992cb6291..ee8c70048 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/DiskUsageCollector.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/DiskUsageCollector.java @@ -55,6 +55,7 @@ private static List collectDiskUsage() throws IOException { final Set usedFileStores = new HashSet<>(); List> diskItemCollectors = new ArrayList<>(); diskItemCollectors.add(factory.createDiskItemCollector(CollectorType.DISK_USAGE_BYTES_GAUGE, new String[]{"file_store", "directory"})); + diskItemCollectors.add(factory.createDiskItemCollector(CollectorType.DISK_USAGE_FILE_COUNT_GAUGE, new String[]{"file_store", "directory"})); diskUsagePlugin.getDirectoriesUsages().forEach(i -> { final Optional fileStore = getFileStore(i.getPath()); diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/CollectorType.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/CollectorType.java index fd19083e3..dfafd8da0 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/CollectorType.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/CollectorType.java @@ -30,6 +30,7 @@ public enum CollectorType { EXECUTORS_QUEUE_LENGTH_GAUGE("queue_length"), DISK_USAGE_BYTES_GAUGE("disk_usage_bytes"), + DISK_USAGE_FILE_COUNT_GAUGE("disk_usage_file_count"), FILE_STORE_AVAILABLE_GAUGE("file_store_available_bytes"), FILE_STORE_CAPACITY_GAUGE("file_store_capacity_bytes"), JOB_USAGE_BYTES_GAUGE("job_usage_bytes"), diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskCollectorFactory.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskCollectorFactory.java index 09b261c71..947b1b366 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskCollectorFactory.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskCollectorFactory.java @@ -23,6 +23,9 @@ public DiskCollectorFactory() { if (Objects.requireNonNull(type) == DISK_USAGE_BYTES_GAUGE) { return saveBuildCollector(new DiskUsageBytesGauge(labelNames, namespace, subsystem)); } + if (Objects.requireNonNull(type) == DISK_USAGE_FILE_COUNT_GAUGE) { + return saveBuildCollector(new DiskUsageFileCountGauge(labelNames, namespace, subsystem)); + } return new NoOpMetricCollector<>(); } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGauge.java new file mode 100644 index 000000000..35d66c875 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGauge.java @@ -0,0 +1,37 @@ +package org.jenkinsci.plugins.prometheus.collectors.disk; + +import com.cloudbees.simplediskusage.DiskItem; +import io.prometheus.client.Gauge; +import io.prometheus.client.SimpleCollector; +import org.jenkinsci.plugins.prometheus.collectors.BaseMetricCollector; +import org.jenkinsci.plugins.prometheus.collectors.CollectorType; + +public class DiskUsageFileCountGauge extends BaseMetricCollector { + + protected DiskUsageFileCountGauge(String[] labelNames, String namespace, String subsystem) { + super(labelNames, namespace, subsystem); + } + + @Override + protected CollectorType getCollectorType() { + return CollectorType.DISK_USAGE_FILE_COUNT_GAUGE; + } + + @Override + protected String getHelpText() { + return "Disk usage file count of the first level folder in JENKINS_HOME"; + } + + @Override + protected SimpleCollector.Builder getCollectorBuilder() { + return Gauge.build(); + } + + @Override + public void calculateMetric(DiskItem jenkinsObject, String[] labelValues) { + if (jenkinsObject == null) { + return; + } + this.collector.labels(labelValues).set(jenkinsObject.getCount()); + } +} diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/DiskUsageCollectorTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/DiskUsageCollectorTest.java index 83bf53efa..6a55fc521 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/DiskUsageCollectorTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/DiskUsageCollectorTest.java @@ -92,14 +92,13 @@ public void shouldProduceMetrics() throws IOException { DiskUsageCollector underTest = new DiskUsageCollector(); final List samples = underTest.collect(); - System.out.println("Size: " + samples.size()); - for (MetricFamilySamples f :samples) { - System.out.println(f); - } assertThat(samples, containsInAnyOrder( gauges("foo_bar_disk_usage_bytes", containsInAnyOrder( sample(ImmutableMap.of("file_store", "the file store", "directory", "dir"), equalTo(11. * 1024)) )), + gauges("foo_bar_disk_usage_file_count", containsInAnyOrder( + sample(ImmutableMap.of("file_store", "the file store", "directory", "dir"), equalTo(0.0)) + )), gauges("foo_bar_job_usage_bytes", containsInAnyOrder( sample(ImmutableMap.of("file_store", "the file store", "jobName", "job name", "url", "/job"), equalTo(7. * 1024)) )), diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGaugeTest.java new file mode 100644 index 000000000..d395c5fa7 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGaugeTest.java @@ -0,0 +1,51 @@ +package org.jenkinsci.plugins.prometheus.collectors.disk; + +import com.cloudbees.simplediskusage.DiskItem; +import io.prometheus.client.Collector; +import org.jenkinsci.plugins.prometheus.collectors.testutils.CollectorTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class DiskUsageFileCountGaugeTest extends CollectorTest { + + @Mock + DiskItem mock; + + @Test + public void testCollectResult() { + + when(mock.getCount()).thenReturn(10L); + + DiskUsageFileCountGauge sut = new DiskUsageFileCountGauge(getLabelNames(), getNamespace(), getSubSystem()); + sut.calculateMetric(mock, getLabelValues()); + + List collect = sut.collect(); + + validateMetricFamilySampleListSize(collect, 1); + + Collector.MetricFamilySamples samples = collect.get(0); + + validateNames(samples, new String[]{"default_jenkins_disk_usage_file_count"}); + validateMetricFamilySampleSize(samples, 1); + validateHelp(samples, "Disk usage file count of the first level folder in JENKINS_HOME"); + validateValue(samples, 0, 10.0); + } + + @Test + public void testDiskItemIsNull() { + DiskUsageFileCountGauge sut = new DiskUsageFileCountGauge(getLabelNames(), getNamespace(), getSubSystem()); + sut.calculateMetric(null, getLabelValues()); + + List collect = sut.collect(); + + validateMetricFamilySampleListSize(collect, 1); + validateMetricFamilySampleSize(collect.get(0), 0); + } +} \ No newline at end of file