forked from lsjostro/prometheus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/583 offline nodes metrics #584
Closed
aleksei-scanbot
wants to merge
2
commits into
jenkinsci:master
from
aleksei-scanbot:feature/583-offline-nodes-metrics
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions
51
src/main/java/org/jenkinsci/plugins/prometheus/collectors/jenkins/NodesOfflineGauge.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.jenkins; | ||
|
||
import hudson.model.Computer; | ||
import hudson.model.Node; | ||
import io.prometheus.client.Gauge; | ||
import io.prometheus.client.SimpleCollector; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.prometheus.collectors.BaseMetricCollector; | ||
import org.jenkinsci.plugins.prometheus.collectors.CollectorType; | ||
|
||
public class NodesOfflineGauge extends BaseMetricCollector<Jenkins, Gauge> { | ||
|
||
NodesOfflineGauge(String[] labelNames, String namespace, String subsystem) { | ||
super(labelNames, namespace, subsystem); | ||
} | ||
|
||
@Override | ||
protected CollectorType getCollectorType() { | ||
return CollectorType.NODES_OFFLINE_GAUGE; | ||
} | ||
|
||
@Override | ||
protected String getHelpText() { | ||
return "Jenkins nodes offline status"; | ||
} | ||
|
||
@Override | ||
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() { | ||
return Gauge.build(); | ||
} | ||
|
||
@Override | ||
public void calculateMetric(Jenkins jenkinsObject, String[] labelValues) { | ||
if (jenkinsObject == null) { | ||
return; | ||
} | ||
for (Node node : jenkinsObject.getNodes()) { | ||
//Check whether the node is online or offline | ||
Computer comp = node.toComputer(); | ||
if (comp == null) { | ||
continue; | ||
} | ||
|
||
if (comp.isOffline()) { // https://javadoc.jenkins.io/hudson/model/Computer.html | ||
this.collector.labels(node.getNodeName()).set(1); | ||
} else { | ||
this.collector.labels(node.getNodeName()).set(0); | ||
} | ||
} | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
src/test/java/org/jenkinsci/plugins/prometheus/collectors/jenkins/NodesOfflineGaugeTest.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,74 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.jenkins; | ||
|
||
import hudson.model.Computer; | ||
import hudson.model.Node; | ||
import io.prometheus.client.Collector; | ||
import jenkins.model.Jenkins; | ||
|
||
import org.jenkinsci.plugins.prometheus.collectors.testutils.MockedJenkinsTest; | ||
import org.junit.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class NodesOfflineGaugeTest extends MockedJenkinsTest { | ||
|
||
@Test | ||
public void testCollectResult() throws Exception { | ||
|
||
|
||
setFinalStatic(Jenkins.class.getDeclaredField("VERSION"), "123"); | ||
|
||
List<Node> nodes = new ArrayList<>(); | ||
nodes.add(mockNode("node1", true)); | ||
nodes.add(mockNode("node2", false)); | ||
nodes.add(mockNode("node3", true)); | ||
nodes.add(mockNode("nullNode", false)); | ||
when(mock.getNodes()).thenReturn(nodes); | ||
|
||
NodesOfflineGauge sut = new NodesOfflineGauge(new String[]{"node"}, getNamespace(), getSubSystem()); | ||
sut.calculateMetric(mock, getLabelValues()); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
validateMetricFamilySampleListSize(collect, 1); | ||
|
||
Collector.MetricFamilySamples samples = collect.get(0); | ||
// we pass 4 nodes but we only get 3 -> there's a node with null computer | ||
validateMetricFamilySampleSize(samples, 3); | ||
for (Collector.MetricFamilySamples.Sample sample : samples.samples) { | ||
System.out.println(sample); | ||
if (sample.labelValues.contains("node1")) { | ||
validateValue(sample, 1.0); | ||
} | ||
if (sample.labelValues.contains("node2")) { | ||
validateValue(sample, 0.0); | ||
} | ||
if (sample.labelValues.contains("node3")) { | ||
validateValue(sample, 1.0); | ||
} | ||
} | ||
System.out.println(samples); | ||
validateNames(samples, new String[]{"default_jenkins_nodes_offline"}); | ||
} | ||
|
||
private Node mockNode(String nodeName, boolean isOnline) { | ||
Node nodeMock = mock(Node.class); | ||
if (!"nullNode".equalsIgnoreCase(nodeName)) { | ||
Computer computerMock = mock(Computer.class); | ||
when(computerMock.isOffline()).thenReturn(isOnline); | ||
when(nodeMock.toComputer()).thenReturn(computerMock); | ||
} | ||
when(nodeMock.getNodeName()).thenReturn(nodeName); | ||
return nodeMock; | ||
} | ||
|
||
static void setFinalStatic(Field field, Object newValue) throws Exception { | ||
field.setAccessible(true); | ||
Check notice Code scanning / Pmd (reported by Codacy) You should not modify visibility of constructors, methods or fields using setAccessible() Note test
You should not modify visibility of constructors, methods or fields using setAccessible()
|
||
field.set(null, newValue); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Pmd (reported by Codacy)
JUnit tests should include assert() or fail() Note test