Skip to content

Commit

Permalink
JENKINS-23933: Expose build successful/unstable/failed metrics.
Browse files Browse the repository at this point in the history
We are running a couple of Jenkins masters. We have run into problems because nodes are not able to access vital systems like our internal Maven repository or our SCM, sometimes because the hosts were down or because of new VLAN settings or firewalls between nodes or even because of configuration changes.
In these cases the number of failing builds is increasing immediately. It would be nice if the metrics plugin could provide metrics of successful, unstable and failed builds, so we can react more promptly.
  • Loading branch information
mfriedenhagen-ui authored and mfriedenhagen committed Jul 30, 2014
1 parent 90d2eb5 commit e409352
Showing 1 changed file with 39 additions and 1 deletion.
Expand Up @@ -25,10 +25,12 @@
package jenkins.metrics.impl;

import com.codahale.metrics.CachedGauge;
import com.codahale.metrics.Counter;
import com.codahale.metrics.DerivativeGauge;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricSet;
import com.codahale.metrics.Timer;
Expand All @@ -47,6 +49,7 @@
import hudson.model.Node;
import hudson.model.PeriodicWork;
import hudson.model.Queue;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
Expand Down Expand Up @@ -124,6 +127,10 @@ public class JenkinsMetricProviderImpl extends MetricProvider {
* The amount of time jobs are building.
*/
private Timer jenkinsJobBuildingTime;
/**
* Run Results.
*/
private HashMap<String, Meter> jenkinsRunResults = new HashMap<String, Meter>();
/**
* The amount of time jobs take from initial scheduling to completion.
*/
Expand Down Expand Up @@ -360,10 +367,25 @@ protected Integer loadValue() {
}
return count;
}
})
}),
metric(name("jenkins", "runs"), runCounters())
);
}

private MetricSet runCounters() {
final Map<String, Metric> runCounters = new HashMap<String, Metric>();
for (String resultName : ResultRunListener.ALL) {
Meter counter = new Meter();
jenkinsRunResults.put(resultName, counter);
runCounters.put(resultName, counter);
}
return new MetricSet() {
public Map<String, Metric> getMetrics() {
return runCounters;
}
};
}

public static JenkinsMetricProviderImpl instance() {
return Jenkins.getInstance().getExtensionList(MetricProvider.class).get(JenkinsMetricProviderImpl.class);
}
Expand Down Expand Up @@ -518,6 +540,22 @@ protected synchronized void doRun() throws Exception {

}

@Extension
public static class ResultRunListener extends RunListener<Run> {
static final String[] ALL = new String[]{
"success", "unstable", "failure", "not_built", "aborted", "total"};

@Override
public synchronized void onCompleted(Run run, TaskListener listener) {
JenkinsMetricProviderImpl instance = instance();
if (instance != null) {
instance.jenkinsRunResults.get(String.valueOf(run.getResult()).toLowerCase()).mark();
instance.jenkinsRunResults.get("total").mark();
}
}

}

@Extension
public static class RunListenerImpl extends RunListener<Run> {
private Map<Run, List<Timer.Context>> contexts = new HashMap<Run, List<Timer.Context>>();
Expand Down

0 comments on commit e409352

Please sign in to comment.