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.
Creating Build Metrics for Log is updated and build is likely stuck (#…
…592)
- Loading branch information
Waschndolos
authored
Nov 26, 2023
1 parent
7e3f6e6
commit 4f5e028
Showing
10 changed files
with
279 additions
and
12 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
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
44 changes: 44 additions & 0 deletions
44
src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/BuildLikelyStuckGauge.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,44 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.builds; | ||
|
||
import hudson.model.Executor; | ||
import hudson.model.Run; | ||
import io.prometheus.client.Gauge; | ||
import io.prometheus.client.SimpleCollector; | ||
import org.jenkinsci.plugins.prometheus.collectors.CollectorType; | ||
|
||
public class BuildLikelyStuckGauge extends BuildsMetricCollector<Run<?, ?>, Gauge> { | ||
|
||
protected BuildLikelyStuckGauge(String[] labelNames, String namespace, String subsystem, String prefix) { | ||
super(labelNames, namespace, subsystem, prefix); | ||
} | ||
|
||
@Override | ||
protected CollectorType getCollectorType() { | ||
return CollectorType.BUILD_LIKELY_STUCK_GAUGE; | ||
} | ||
|
||
@Override | ||
protected String getHelpText() { | ||
return "Provides a hint if a build is likely stuck. Uses Jenkins function Executor#isLikelyStuck."; | ||
} | ||
|
||
@Override | ||
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() { | ||
return Gauge.build(); | ||
} | ||
|
||
@Override | ||
public void calculateMetric(Run<?, ?> run, String[] labelValues) { | ||
if (run == null || !run.isBuilding()) { | ||
return; | ||
} | ||
|
||
Executor executor = run.getExecutor(); | ||
if (executor == null) { | ||
return; | ||
} | ||
|
||
double likelyStuckDoubleValue = executor.isLikelyStuck() ? 1.0 : 0.0; | ||
this.collector.labels(labelValues).set(likelyStuckDoubleValue); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/main/java/org/jenkinsci/plugins/prometheus/collectors/jobs/LogUpdatedGauge.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,38 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.jobs; | ||
|
||
import hudson.model.Job; | ||
import io.prometheus.client.Gauge; | ||
import io.prometheus.client.SimpleCollector; | ||
import org.jenkinsci.plugins.prometheus.collectors.CollectorType; | ||
import org.jenkinsci.plugins.prometheus.collectors.builds.BuildsMetricCollector; | ||
|
||
public class LogUpdatedGauge extends BuildsMetricCollector<Job<?, ?>, Gauge> { | ||
|
||
protected LogUpdatedGauge(String[] labelNames, String namespace, String subsystem) { | ||
super(labelNames, namespace, subsystem); | ||
} | ||
|
||
@Override | ||
protected CollectorType getCollectorType() { | ||
return CollectorType.JOB_LOG_UPDATED_GAUGE; | ||
} | ||
|
||
@Override | ||
protected String getHelpText() { | ||
return "Provides a hint if a job is still logging. Uses Jenkins function Job#isLogUpdated"; | ||
} | ||
|
||
@Override | ||
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() { | ||
return Gauge.build(); | ||
} | ||
|
||
@Override | ||
public void calculateMetric(Job<?, ?> jenkinsObject, String[] labelValues) { | ||
|
||
if (jenkinsObject != null && jenkinsObject.isBuilding()) { | ||
boolean logUpdated = jenkinsObject.isLogUpdated(); | ||
this.collector.labels(labelValues).set(logUpdated ? 1.0 : 0.0); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...st/java/org/jenkinsci/plugins/prometheus/collectors/builds/BuildLikelyStuckGaugeTest.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,96 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.builds; | ||
|
||
import hudson.model.Executor; | ||
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.mockito.Mockito; | ||
|
||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
|
||
public class BuildLikelyStuckGaugeTest extends MockedRunCollectorTest { | ||
|
||
|
||
@Test | ||
public void testNothingCalculatedWhenRunIsNull() { | ||
BuildLikelyStuckGauge sut = new BuildLikelyStuckGauge(getLabelNames(), getNamespace(), getSubSystem(), ""); | ||
|
||
sut.calculateMetric(null, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
Assertions.assertEquals(1, collect.size()); | ||
Assertions.assertEquals(0, collect.get(0).samples.size()); | ||
|
||
} | ||
|
||
@Test | ||
public void testNothingCalculatedWhenJobIsNotBuilding() { | ||
Mockito.when(mock.isBuilding()).thenReturn(false); | ||
|
||
BuildLikelyStuckGauge sut = new BuildLikelyStuckGauge(getLabelNames(), getNamespace(), getSubSystem(), ""); | ||
|
||
sut.calculateMetric(mock, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
Assertions.assertEquals(1, collect.size()); | ||
Assertions.assertEquals(0, collect.get(0).samples.size()); | ||
|
||
} | ||
|
||
@Test | ||
public void testNothingCalculatedWhenNoExecutorFound() { | ||
Mockito.when(mock.isBuilding()).thenReturn(true); | ||
Mockito.when(mock.getExecutor()).thenReturn(null); | ||
|
||
BuildLikelyStuckGauge sut = new BuildLikelyStuckGauge(getLabelNames(), getNamespace(), getSubSystem(), ""); | ||
|
||
sut.calculateMetric(mock, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
Assertions.assertEquals(1, collect.size()); | ||
Assertions.assertEquals(0, collect.get(0).samples.size()); | ||
} | ||
|
||
@Test | ||
public void testBuildIsLikelyStuck() { | ||
Mockito.when(mock.isBuilding()).thenReturn(true); | ||
Executor mockedExecutor = mock(Executor.class); | ||
when(mockedExecutor.isLikelyStuck()).thenReturn(true); | ||
Mockito.when(mock.getExecutor()).thenReturn(mockedExecutor); | ||
|
||
BuildLikelyStuckGauge sut = new BuildLikelyStuckGauge(getLabelNames(), getNamespace(), getSubSystem(), ""); | ||
|
||
sut.calculateMetric(mock, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
Assertions.assertEquals(1, collect.size()); | ||
Assertions.assertEquals(1, collect.get(0).samples.size()); | ||
Assertions.assertEquals(1.0, collect.get(0).samples.get(0).value); | ||
} | ||
|
||
@Test | ||
public void testBuildIsNotLikelyStuck() { | ||
Mockito.when(mock.isBuilding()).thenReturn(true); | ||
Executor mockedExecutor = mock(Executor.class); | ||
when(mockedExecutor.isLikelyStuck()).thenReturn(false); | ||
Mockito.when(mock.getExecutor()).thenReturn(mockedExecutor); | ||
|
||
BuildLikelyStuckGauge sut = new BuildLikelyStuckGauge(getLabelNames(), getNamespace(), getSubSystem(), ""); | ||
|
||
sut.calculateMetric(mock, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
Assertions.assertEquals(1, collect.size()); | ||
Assertions.assertEquals(1, collect.get(0).samples.size()); | ||
Assertions.assertEquals(0.0, collect.get(0).samples.get(0).value); | ||
} | ||
} |
Oops, something went wrong.