forked from lsjostro/prometheus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New metrics which shows the build log size in bytes (#668)
* Removing System.out.println * Creating new Gauge which shows the buildlog size in bytes * Fixing codacy issues
- Loading branch information
Waschndolos
authored
May 21, 2024
1 parent
304ce25
commit d7d9acf
Showing
7 changed files
with
104 additions
and
3 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
39 changes: 39 additions & 0 deletions
39
src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/BuildLogFileSizeGauge.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,39 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.builds; | ||
|
||
import hudson.console.AnnotatedLargeText; | ||
import hudson.model.Run; | ||
import io.prometheus.client.Gauge; | ||
import io.prometheus.client.SimpleCollector; | ||
import org.jenkinsci.plugins.prometheus.collectors.CollectorType; | ||
|
||
public class BuildLogFileSizeGauge extends BuildsMetricCollector<Run<?, ?>, Gauge> { | ||
|
||
protected BuildLogFileSizeGauge(String[] labelNames, String namespace, String subsystem, String namePrefix) { | ||
super(labelNames, namespace, subsystem, namePrefix); | ||
} | ||
|
||
@Override | ||
protected CollectorType getCollectorType() { | ||
return CollectorType.BUILD_LOGFILE_SIZE_GAUGE; | ||
} | ||
|
||
@Override | ||
protected String getHelpText() { | ||
return "Build logfile size in bytes"; | ||
} | ||
|
||
@Override | ||
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() { | ||
return Gauge.build(); | ||
} | ||
|
||
@Override | ||
public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) { | ||
if (!jenkinsObject.isBuilding()) { | ||
AnnotatedLargeText logText = jenkinsObject.getLogText(); | ||
long logFileSize = logText.length(); | ||
|
||
collector.labels(labelValues).set(logFileSize); | ||
} | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
...st/java/org/jenkinsci/plugins/prometheus/collectors/builds/BuildLogFileSizeGaugeTest.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,54 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.builds; | ||
|
||
import hudson.console.AnnotatedLargeText; | ||
import io.prometheus.client.Collector; | ||
import org.jenkinsci.plugins.prometheus.collectors.testutils.MockedRunCollectorTest; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class BuildLogFileSizeGaugeTest extends MockedRunCollectorTest { | ||
|
||
@Test | ||
public void testNothingCalculatedWhenRunIsBuilding() { | ||
|
||
when(mock.isBuilding()).thenReturn(true); | ||
|
||
BuildLogFileSizeGauge sut = new BuildLogFileSizeGauge(getLabelNames(), getNamespace(), getSubSystem(), "default"); | ||
|
||
sut.calculateMetric(mock, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
assertEquals(1, collect.size()); | ||
assertEquals(0, collect.get(0).samples.size(), "Would expect no sample created when run is running"); | ||
} | ||
|
||
@Test | ||
public void testCollectResult() { | ||
|
||
when(mock.isBuilding()).thenReturn(false); | ||
AnnotatedLargeText annotatedLargeText = Mockito.mock(AnnotatedLargeText.class); | ||
when(annotatedLargeText.length()).thenReturn(3000L); | ||
|
||
when(mock.getLogText()).thenReturn(annotatedLargeText); | ||
|
||
BuildLogFileSizeGauge sut = new BuildLogFileSizeGauge(getLabelNames(), getNamespace(), getSubSystem(), "default"); | ||
|
||
sut.calculateMetric(mock, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
assertEquals(1, collect.size()); | ||
assertEquals(3000.0, collect.get(0).samples.get(0).value); | ||
|
||
} | ||
} |