forked from lsjostro/prometheus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lsjostro#2 from markyjackson-taulia/documentation
Documentation
- Loading branch information
Showing
6 changed files
with
145 additions
and
51 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
48 changes: 48 additions & 0 deletions
48
src/main/java/org/jenkinsci/plugins/prometheus/JenkinsStatusCollector.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,48 @@ | ||
package org.jenkinsci.plugins.prometheus; | ||
|
||
import hudson.model.Computer; | ||
import io.prometheus.client.Collector; | ||
import io.prometheus.client.Gauge; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.prometheus.util.ConfigurationUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class JenkinsStatusCollector extends Collector { | ||
|
||
@Override | ||
public List<MetricFamilySamples> collect() { | ||
final String subsystem = "jenkins"; | ||
final String namespace = ConfigurationUtils.getNamespace(); | ||
final List<MetricFamilySamples> samples = new ArrayList<>(); | ||
Gauge jenkinsUp = Gauge.build(). | ||
name("up"). | ||
labelNames(). | ||
subsystem(subsystem). | ||
namespace(namespace). | ||
help("Is Jenkins ready to receive requests"). | ||
create(); | ||
Jenkins jenkins = Jenkins.getInstance(); | ||
if (jenkins == null) { | ||
return null; | ||
} | ||
jenkinsUp.set(jenkins.getInitLevel() == hudson.init.InitMilestone.COMPLETED ? | ||
1 : 0); | ||
samples.addAll(jenkinsUp.collect()); | ||
Gauge jenkinsUptime = Gauge.build(). | ||
name("uptime"). | ||
labelNames(). | ||
subsystem(subsystem). | ||
namespace(namespace). | ||
help("Time since Jenkins machine was initialized"). | ||
create(); | ||
Computer computer = jenkins.toComputer(); | ||
if (computer != null) { | ||
long upTime = computer.getConnectTime(); | ||
jenkinsUptime.set(System.currentTimeMillis() - upTime); | ||
samples.addAll(jenkinsUptime.collect()); | ||
} | ||
return samples; | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/org/jenkinsci/plugins/prometheus/util/ConfigurationUtils.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,16 @@ | ||
package org.jenkinsci.plugins.prometheus.util; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration; | ||
|
||
public class ConfigurationUtils { | ||
public static String getNamespace() { | ||
// get the namespace from the environment first | ||
String namespace = System.getenv("PROMETHEUS_NAMESPACE"); | ||
if (StringUtils.isEmpty(namespace)) { | ||
// when the environment variable isn't set, try the system configuration | ||
namespace = PrometheusConfiguration.get().getDefaultNamespace(); | ||
} | ||
return namespace; | ||
} | ||
} |