Skip to content
This repository has been archived by the owner on Jan 23, 2020. It is now read-only.

Commit

Permalink
Add ability to blacklist jobs from being reported
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnLZeller committed Oct 16, 2015
1 parent ea17e44 commit 9fde32a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,39 @@ public DatadogBuildListener() { }
@Override
public final void onStarted(final Run run, final TaskListener listener) {
logger = listener.getLogger();
printLog("Started build!");
String jobname = run.getParent().getDisplayName();
String[] blacklist = blacklistStringtoArray( getDescriptor().getBlacklist() );

// Grab environment variables
EnvVars envVars = null;
try {
envVars = run.getEnvironment(listener);
} catch (IOException e) {
printLog("ERROR: " + e.getMessage());
} catch (InterruptedException e) {
printLog("ERROR: " + e.getMessage());
}

// Gather pre-build metadata
JSONObject builddata = new JSONObject();
builddata.put("hostname", getHostname(envVars)); // string
builddata.put("job", run.getParent().getDisplayName()); // string
builddata.put("number", run.number); // int
builddata.put("result", null); // null
builddata.put("duration", null); // null
builddata.put("buildurl", envVars.get("BUILD_URL")); // string
long starttime = run.getStartTimeInMillis() / this.THOUSAND_LONG; // adjusted from ms to s
builddata.put("timestamp", starttime); // string
// Process only if job is NOT in blacklist
if ( (blacklist == null) || !Arrays.asList(blacklist).contains(jobname.toLowerCase()) ) {
printLog("Started build!");

// Add event_type to assist in roll-ups
builddata.put("event_type", "build start"); // string
// Grab environment variables
EnvVars envVars = null;
try {
envVars = run.getEnvironment(listener);
} catch (IOException e) {
printLog("ERROR: " + e.getMessage());
} catch (InterruptedException e) {
printLog("ERROR: " + e.getMessage());
}

event(builddata);
// Gather pre-build metadata
JSONObject builddata = new JSONObject();
builddata.put("hostname", getHostname(envVars)); // string
builddata.put("job", jobname); // string
builddata.put("number", run.number); // int
builddata.put("result", null); // null
builddata.put("duration", null); // null
builddata.put("buildurl", envVars.get("BUILD_URL")); // string
long starttime = run.getStartTimeInMillis() / this.THOUSAND_LONG; // adjusted from ms to s
builddata.put("timestamp", starttime); // string

// Add event_type to assist in roll-ups
builddata.put("event_type", "build start"); // string

event(builddata);
}
}

/**
Expand All @@ -133,21 +139,27 @@ public final void onStarted(final Run run, final TaskListener listener) {
@Override
public final void onCompleted(final Run run, @Nonnull final TaskListener listener) {
logger = listener.getLogger();
printLog("Completed build!");
String jobname = run.getParent().getDisplayName();
String[] blacklist = blacklistStringtoArray( getDescriptor().getBlacklist() );

// Collect Data
JSONObject builddata = gatherBuildMetadata(run, listener);
// Process only if job in NOT in blacklist
if ( (blacklist == null) || !Arrays.asList(blacklist).contains(jobname.toLowerCase()) ) {
printLog("Completed build!");

// Add event_type to assist in roll-ups
builddata.put("event_type", "build result"); // string
// Collect Data
JSONObject builddata = gatherBuildMetadata(run, listener);

// Report Data
event(builddata);
gauge("jenkins.job.duration", builddata, "duration");
if ( "SUCCESS".equals(builddata.get("result")) ) {
serviceCheck("jenkins.job.status", this.OK, builddata);
} else {
serviceCheck("jenkins.job.status", this.CRITICAL, builddata);
// Add event_type to assist in roll-ups
builddata.put("event_type", "build result"); // string

// Report Data
event(builddata);
gauge("jenkins.job.duration", builddata, "duration");
if ( "SUCCESS".equals(builddata.get("result")) ) {
serviceCheck("jenkins.job.status", this.OK, builddata);
} else {
serviceCheck("jenkins.job.status", this.CRITICAL, builddata);
}
}
}

Expand Down Expand Up @@ -579,6 +591,20 @@ public final String nullSafeGetString(final JSONObject data, final String key) {
}
}

/**
* Converts a blacklist string into a String array.
*
* @param blacklist - A String containing a set of key/value pairs.
* @return a String array representing the job names to be blacklisted. Returns
* empty string if blacklist is null.
*/
public final String[] blacklistStringtoArray(final String blacklist) {
if ( blacklist != null ) {
return blacklist.split(",");
}
return ( new String[0] );
}

/**
* Getter function for the {@link DescriptorImpl} class.
*
Expand All @@ -603,6 +629,7 @@ public static final class DescriptorImpl extends Descriptor<DatadogBuildListener
*/
private Secret apiKey = null;
private String hostname = null;
private String blacklist = null;
private Boolean tagNode = null;

/**
Expand Down Expand Up @@ -719,14 +746,25 @@ public String getDisplayName() {
@Override
public boolean configure(final StaplerRequest req, final JSONObject formData)
throws FormException {
// Grab apiKey and hostname
apiKey = Secret.fromString(fixEmptyAndTrim(formData.getString("apiKey")));
hostname = formData.getString("hostname");

// Grab blacklist, strip whitespace, remove duplicate commas, and make lowercase
blacklist = formData.getString("blacklist")
.replaceAll("\\s","")
.replaceAll(",,","")
.toLowerCase();

// Grab tagNode and coerse to a boolean
if ( formData.getString("tagNode").equals("true") ) {
tagNode = true;
} else {
tagNode = false;
}
save(); // persist global configuration information

// Persist global configuration information
save();
return super.configure(req, formData);
}

Expand All @@ -748,6 +786,16 @@ public String getHostname() {
return hostname;
}

/**
* Getter function for the {@link blacklist} global configuration, containing
* a comma-separated list of jobs to blacklist from monitoring.
*
* @return a String array containing the {@link blacklist} global configuration.
*/
public String getBlacklist() {
return blacklist;
}

/**
* Getter function for the optional tag {@link node} global configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
</f:entry>
<f:validateButton title="${%Test Hostname}" progress="${%Testing...}"
method="testHostname" with="hostname" />
<f:entry title="Blacklisted Jobs"
description="A comma-separated list of job names that should not monitored. (eg: susans-job,johns-job,prod-release)." >
<f:textarea field="blacklist" optional="true" default="${blacklist}" />
</f:entry>
<f:entry title="Optional Tags"
description="Name of node the current build is running on (i.e. 'master' for master node)." >
<f:checkbox title="node" field="tagNode" optional="true" />
Expand Down

0 comments on commit 9fde32a

Please sign in to comment.