@@ -0,0 +1,2187 @@
+package org.datadog.jenkins.plugins.datadog;%0A%0Aimport hudson.EnvVars;%0Aimport hudson.Extension;%0Aimport hudson.model.PeriodicWork;%0Aimport hudson.model.Queue;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.logging.Logger;%0Aimport net.sf.json.JSONArray;%0Aimport net.sf.json.JSONObject;%0A%0A/**%0A * This class registers a %7B@link PeriodicWork%7D with Jenkins to run periodically in order to enable%0A * us to compute metrics related to the size of the queue.%0A */%0A@Extension%0Apublic class DatadogQueueListener extends PeriodicWork %7B%0A  private static final String METRIC = %22v1/series%22;%0A  private static final long RECURRENCE_PERIOD = TimeUnit.MINUTES.toMillis(1);%0A%0A  private static final Logger logger = Logger.getLogger(DatadogQueueListener.class.getName());%0A  private static final Queue queue = Queue.getInstance();%0A  private static final EnvVars envVars = new EnvVars();%0A%0A  @Override%0A  public long getRecurrencePeriod() %7B%0A    return RECURRENCE_PERIOD;%0A  %7D%0A%0A  @Override%0A  protected void doRun() throws Exception %7B%0A    logger.fine(%22doRun called: Computing queue metrics%22);%0A    gauge(%22jenkins.queue.size%22, queue.getApproximateItemsQuickly().size());%0A  %7D%0A%0A  private void gauge(String name, int value) %7B%0A    // Setup data point, of type %5B%3Cunix_timestamp%3E, %3Cvalue%3E%5D%0A    JSONArray points = new JSONArray();%0A    JSONArray point = new JSONArray();%0A%0A    long currentTime = System.currentTimeMillis() / DatadogBuildListener.THOUSAND_LONG;%0A    point.add(currentTime); // current time, s%0A    point.add(value);%0A    points.add(point); // api expects a list of points%0A%0A    JSONObject metric = new JSONObject();%0A    metric.put(%22metric%22, name);%0A    metric.put(%22points%22, points);%0A    metric.put(%22type%22, %22gauge%22);%0A    metric.put(%22host%22, DatadogUtilities.getHostname(envVars)); // string%0A%0A    // Place metric as item of series list%0A    JSONArray series = new JSONArray();%0A    series.add(metric);%0A%0A    // Add series to payload%0A    JSONObject payload = new JSONObject();%0A    payload.put(%22series%22, series);%0A%0A    logger.fine(String.format(%22Resulting payload: %25s%22, payload.toString()));%0A%0A    try %7B%0A      DatadogHttpRequests.post(payload, METRIC);%0A    %7D catch (Exception e) %7B%0A      logger.severe(e.toString());%0A    %7D%0A  %7D%0A%7D%0A

Add a gauge to measure the size of the build queue.