Skip to content

Commit

Permalink
Implementing new Metric which displays the file count within the root…
Browse files Browse the repository at this point in the history
… level of the JENKINS_HOME directory (#591)

Co-authored-by: Sailer, Manuel <[email protected]>
  • Loading branch information
Waschndolos and Sailer, Manuel authored Nov 22, 2023
1 parent a2bb143 commit 8dab3b1
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private static List<MetricFamilySamples> collectDiskUsage() throws IOException {
final Set<FileStore> usedFileStores = new HashSet<>();
List<MetricCollector<DiskItem, ? extends Collector>> 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> fileStore = getFileStore(i.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 26 in src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskCollectorFactory.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 26 is only partially covered, one branch is missing
return saveBuildCollector(new DiskUsageFileCountGauge(labelNames, namespace, subsystem));
}
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.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<DiskItem, Gauge> {

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<?, Gauge> getCollectorBuilder() {
return Gauge.build();
}

@Override
public void calculateMetric(DiskItem jenkinsObject, String[] labelValues) {
if (jenkinsObject == null) {
return;
}
this.collector.labels(labelValues).set(jenkinsObject.getCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,13 @@ public void shouldProduceMetrics() throws IOException {
DiskUsageCollector underTest = new DiskUsageCollector();
final List<MetricFamilySamples> 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))
)),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Collector.MetricFamilySamples> 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<Collector.MetricFamilySamples> collect = sut.collect();

validateMetricFamilySampleListSize(collect, 1);
validateMetricFamilySampleSize(collect.get(0), 0);
}
}

0 comments on commit 8dab3b1

Please sign in to comment.