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.
Adding metric collector for node related items
- Loading branch information
waschndolos
committed
Oct 25, 2023
1 parent
2e350ec
commit c03713c
Showing
7 changed files
with
136 additions
and
10 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
54 changes: 54 additions & 0 deletions
54
src/main/java/org/jenkinsci/plugins/prometheus/NodeCollector.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; | ||
|
||
import hudson.model.Computer; | ||
import hudson.model.Executor; | ||
import hudson.model.Node; | ||
import io.prometheus.client.Collector; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.prometheus.collectors.CollectorFactory; | ||
import org.jenkinsci.plugins.prometheus.collectors.CollectorType; | ||
import org.jenkinsci.plugins.prometheus.collectors.MetricCollector; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class NodeCollector extends Collector { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(NodeCollector.class); | ||
|
||
@Override | ||
public List<MetricFamilySamples> collect() { | ||
LOGGER.debug("Collecting node metrics for prometheus"); | ||
String[] labelNameArray = {"computerName"}; | ||
|
||
CollectorFactory factory = new CollectorFactory(); | ||
|
||
MetricCollector<Executor, ? extends Collector> likelyStuckCollector = factory.createExecutorStatisticsCollector(CollectorType.EXECUTOR_LIKELY_STUCK_GAUGE, labelNameArray); | ||
|
||
List<? extends MetricCollector<Executor, ? extends Collector>> collectors = List.of(likelyStuckCollector); | ||
|
||
List<Computer> computers = Jenkins.get().getNodes().parallelStream() | ||
.map(Node::toComputer) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toList()); | ||
|
||
for (Computer computer : computers) { | ||
String computerName = computer.getName(); | ||
List<Executor> executors = computer.getExecutors(); | ||
if (executors != null) { | ||
for (Executor ex : executors) { | ||
likelyStuckCollector.calculateMetric(ex, new String[]{computerName}); | ||
} | ||
} | ||
} | ||
|
||
return collectors.stream() | ||
.map(MetricCollector::collect) | ||
.flatMap(Collection::stream) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
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
...main/java/org/jenkinsci/plugins/prometheus/collectors/nodes/ExecutorLikelyStuckGauge.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.nodes; | ||
|
||
import hudson.model.Executor; | ||
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 ExecutorLikelyStuckGauge extends BaseMetricCollector<Executor, Gauge> { | ||
|
||
protected ExecutorLikelyStuckGauge(String[] labelNames, String namespace, String subsystem) { | ||
super(labelNames, namespace, subsystem); | ||
} | ||
|
||
@Override | ||
protected CollectorType getCollectorType() { | ||
return CollectorType.EXECUTOR_LIKELY_STUCK_GAUGE; | ||
} | ||
|
||
@Override | ||
protected String getHelpText() { | ||
return "Returns an indication if an executor of a node is likely stuck"; | ||
} | ||
|
||
@Override | ||
protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() { | ||
return Gauge.build(); | ||
} | ||
|
||
@Override | ||
public void calculateMetric(Executor executor, String[] labelValues) { | ||
if (executor == null) { | ||
return; | ||
} | ||
boolean likelyStuck = executor.isLikelyStuck(); | ||
collector.labels(labelValues).set(likelyStuck ? 1.0 : 0.0); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/jenkinsci/plugins/prometheus/collectors/nodes/NodeCollectorFactory.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,23 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.nodes; | ||
|
||
import hudson.model.Executor; | ||
import org.jenkinsci.plugins.prometheus.collectors.BaseCollectorFactory; | ||
import org.jenkinsci.plugins.prometheus.collectors.CollectorType; | ||
import org.jenkinsci.plugins.prometheus.collectors.MetricCollector; | ||
import org.jenkinsci.plugins.prometheus.collectors.NoOpMetricCollector; | ||
|
||
import java.util.stream.Collector; | ||
|
||
public class NodeCollectorFactory extends BaseCollectorFactory { | ||
|
||
public NodeCollectorFactory(){super();} | ||
Check warning Code scanning / Pmd (reported by Codacy) Avoid unnecessary constructors - the compiler will generate these for you Warning
Avoid unnecessary constructors - the compiler will generate these for you
|
||
|
||
public MetricCollector<Executor, ? extends Collector> createExecutorCollector(CollectorType type, String[] labelNames) { | ||
switch (type) { | ||
case EXECUTOR_LIKELY_STUCK_GAUGE: | ||
return saveBuildCollector(new ExecutorLikelyStuckGauge(labelNames, namespace, subsystem)); | ||
default: | ||
return new NoOpMetricCollector<>(); | ||
} | ||
} | ||
} |
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