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.
Implementing new Metric which displays the file count within the root…
… 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
Showing
6 changed files
with
96 additions
and
4 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
37 changes: 37 additions & 0 deletions
37
src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGauge.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.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()); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
...st/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGaugeTest.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,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); | ||
} | ||
} |