Skip to content
Browse files

JENKINS-23933: Expose build successful/unstable/failed metrics.

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 committed Jul 30, 2014
1 parent 90d2eb5 commit e40935251265732178ce1f605a481cec8d038a41
Showing with 39 additions and 1 deletion.
  1. +39 −1 src/main/java/jenkins/metrics/impl/
@@ -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;
@@ -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;
@@ -124,6 +127,10 @@
* 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.
@@ -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);
@@ -518,6 +540,22 @@ protected synchronized void doRun() throws Exception {


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

public synchronized void onCompleted(Run run, TaskListener listener) {
JenkinsMetricProviderImpl instance = instance();
if (instance != null) {


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

0 comments on commit e409352

Please sign in to comment.