Skip to content

Commit

Permalink
Use more inclusive naming of config (#93)
Browse files Browse the repository at this point in the history
* Use more inclusive naming

* Add new test

* Update comment

* remove comments

* Fix logic

* Remove unneeded util method
  • Loading branch information
sarah-witt committed Jul 20, 2020
1 parent 15c530b commit f009266
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 66 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ d.setTargetApiURL('https://api.datadoghq.com/api/')
d.setTargetApiKey('<DATADOG_API_KEY>')
// Customization, see dedicated section below
d.setBlacklist('job1,job2')
d.setExcluded('job1,job2')
// If you want to collect logs
d.setLogIntakeUrl('https://http-intake.logs.datadoghq.com/v1/input/')
Expand All @@ -95,7 +95,7 @@ d.setTargetPort(8125)
d.setLogCollectionPort(8125)
// Customization, see dedicated section below
d.setBlacklist('job1,job2')
d.setExcluded('job1,job2')
// Save config
d.save()
Expand Down Expand Up @@ -131,8 +131,8 @@ To customize your global configuration, in Jenkins navigate to `Manage Jenkins -

| Customization | Description | Environment variable |
|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| Blacklisted jobs | A comma-separated list of regex used to exclude job names from monitoring, for example: `susans-job,johns-.*,prod_folder/prod_release`. | `DATADOG_JENKINS_PLUGIN_BLACKLIST` |
| Whitelisted jobs | A comma-separated list of regex used to include job names for monitoring, for example: `susans-job,johns-.*,prod_folder/prod_release`. | `DATADOG_JENKINS_PLUGIN_WHITELIST` |
| Excluded jobs | A comma-separated list of regex used to exclude job names from monitoring, for example: `susans-job,johns-.*,prod_folder/prod_release`. | `DATADOG_JENKINS_PLUGIN_EXCLUDED` |
| Included jobs | A comma-separated list of regex used to include job names for monitoring, for example: `susans-job,johns-.*,prod_folder/prod_release`. | `DATADOG_JENKINS_PLUGIN_INCLUDED` |
| Global tag file | The path to a workspace file containing a comma separated list of tags (not compatible with pipeline jobs). | `DATADOG_JENKINS_PLUGIN_GLOBAL_TAG_FILE` |
| Global tags | A comma-separated list of tags to apply to all metrics, events, and service checks. | `DATADOG_JENKINS_PLUGIN_GLOBAL_TAGS` |
| Global job tags | A comma separated list of regex to match a job and a list of tags to apply to that job. **Note**: Tags can reference match groups in the regex using the `$` symbol, for example: `(.*?)_job_(*?)_release, owner:$1, release_env:$2, optional:Tag3` | `DATADOG_JENKINS_PLUGIN_GLOBAL_JOB_TAGS` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ public class DatadogGlobalConfiguration extends GlobalConfiguration {
private static String TARGET_PORT_PROPERTY = "DATADOG_JENKINS_PLUGIN_TARGET_PORT";
private static String TARGET_LOG_COLLECTION_PORT_PROPERTY = "DATADOG_JENKINS_PLUGIN_TARGET_LOG_COLLECTION_PORT";
private static String HOSTNAME_PROPERTY = "DATADOG_JENKINS_PLUGIN_HOSTNAME";
private static String EXCLUDED_PROPERTY = "DATADOG_JENKINS_PLUGIN_EXCLUDED";
private static String INCLUDED_PROPERTY = "DATADOG_JENKINS_PLUGIN_INCLUDED";
//Deprecated
private static String BLACKLIST_PROPERTY = "DATADOG_JENKINS_PLUGIN_BLACKLIST";
private static String WHITELIST_PROPERTY = "DATADOG_JENKINS_PLUGIN_WHITELIST";

private static String GLOBAL_TAG_FILE_PROPERTY = "DATADOG_JENKINS_PLUGIN_GLOBAL_TAG_FILE";
private static String GLOBAL_TAGS_PROPERTY = "DATADOG_JENKINS_PLUGIN_GLOBAL_TAGS";
private static String GLOBAL_JOB_TAGS_PROPERTY = "DATADOG_JENKINS_PLUGIN_GLOBAL_JOB_TAGS";
Expand All @@ -89,8 +93,8 @@ public class DatadogGlobalConfiguration extends GlobalConfiguration {
private Integer targetPort = DEFAULT_TARGET_PORT_VALUE;
private Integer targetLogCollectionPort = DEFAULT_TARGET_LOG_COLLECTION_PORT_VALUE;
private String hostname = null;
private String blacklist = null;
private String whitelist = null;
private String excluded = null;
private String included = null;
private String globalTagFile = null;
private String globalTags = null;
private String globalJobTags = null;
Expand Down Expand Up @@ -146,15 +150,27 @@ private void loadEnvVariables(){
if(StringUtils.isNotBlank(hostnameEnvVar)){
this.hostname = hostnameEnvVar;
}

String blacklistEnvVar = System.getenv(BLACKLIST_PROPERTY);
if(StringUtils.isNotBlank(blacklistEnvVar)){
this.blacklist = blacklistEnvVar;

String excludedEnvVar = System.getenv(EXCLUDED_PROPERTY);
if(StringUtils.isBlank(excludedEnvVar)){
// backwards compatibility
excludedEnvVar = System.getenv(BLACKLIST_PROPERTY);
if(StringUtils.isNotBlank(excludedEnvVar)){
this.excluded = excludedEnvVar;
}
} else {
this.excluded = excludedEnvVar;
}

String whitelistEnvVar = System.getenv(WHITELIST_PROPERTY);
if(StringUtils.isNotBlank(whitelistEnvVar)){
this.whitelist = whitelistEnvVar;

String includedEnvVar = System.getenv(INCLUDED_PROPERTY);
if(StringUtils.isBlank(includedEnvVar)){
// backwards compatibility
includedEnvVar = System.getenv(WHITELIST_PROPERTY);
if(StringUtils.isNotBlank(includedEnvVar)){
this.included = includedEnvVar;
}
} else {
this.included = includedEnvVar;
}

String globalTagFileEnvVar = System.getenv(GLOBAL_TAG_FILE_PROPERTY);
Expand Down Expand Up @@ -377,8 +393,8 @@ public boolean configure(final StaplerRequest req, final JSONObject formData) th
throw new FormException("Your hostname is invalid, likely because it violates the format set in RFC 1123", "hostname");
}
this.setHostname(formData.getString("hostname"));
this.setBlacklist(formData.getString("blacklist"));
this.setWhitelist(formData.getString("whitelist"));
this.setExcluded(formData.getString("excluded"));
this.setIncluded(formData.getString("included"));
this.setGlobalTagFile(formData.getString("globalTagFile"));
this.setGlobalTags(formData.getString("globalTags"));
this.setGlobalJobTags(formData.getString("globalJobTags"));
Expand Down Expand Up @@ -568,45 +584,79 @@ public void setHostname(final String hostname) {
}

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

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

/**
* Setter function for the blacklist global configuration,
* @deprecated replaced by {@link #setExcluded()}
**/
@Deprecated
@DataBoundSetter
public void setBlacklist(final String jobs) {
this.excluded = jobs;
}

/**
* Setter function for the excluded jobs global configuration,
* accepting a comma-separated string of jobs.
*
* @param jobs - a comma-separated list of jobs to blacklist from monitoring.
* @param jobs - a comma-separated list of jobs to exclude from monitoring.
*/
@DataBoundSetter
public void setBlacklist(final String jobs) {
this.blacklist = jobs;
public void setExcluded(final String jobs) {
this.excluded = jobs;
}

/**
* Getter function for the whitelist global configuration, containing
* a comma-separated list of jobs to whitelist from monitoring.
* @deprecated replaced by {@link #getIncluded()}
**/
@Deprecated
public String getWhitelist() {
return included;
}

/**
* Getter function for the included global configuration, containing
* a comma-separated list of jobs to include for monitoring.
*
* @return a String array containing the whitelist global configuration.
* @return a String array containing the included global configuration.
*/
public String getWhitelist() {
return whitelist;
public String getIncluded() {
return included;
}

/**
* Setter function for the whitelist global configuration,
* @deprecated replaced by {@link #setIncluded()}
**/
@Deprecated
@DataBoundSetter
public void setWhitelist(final String jobs) {
this.included = jobs;
}

/**
* Setter function for the includedd global configuration,
* accepting a comma-separated string of jobs.
*
* @param jobs - a comma-separated list of jobs to whitelist from monitoring.
* @param jobs - a comma-separated list of jobs to include for monitoring.
*/
@DataBoundSetter
public void setWhitelist(final String jobs) {
this.whitelist = jobs;
public void setIncluded(final String jobs) {
this.included = jobs;
}

/**
Expand Down Expand Up @@ -664,7 +714,7 @@ public String getGlobalJobTags() {
* Setter function for the globalJobTags global configuration,
* accepting a comma-separated string of jobs and tags.
*
* @param globalJobTags - a comma-separated list of jobs to whitelist from monitoring.
* @param globalJobTags - a comma-separated list of jobs to include from monitoring.
*/
@DataBoundSetter
public void setGlobalJobTags(String globalJobTags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ public static Map<String, Set<String>> getBuildTags(Run run, EnvVars envVars) {
}

/**
* Checks if a jobName is blacklisted, whitelisted, or neither.
* Checks if a jobName is excluded, included, or neither.
*
* @param jobName - A String containing the name of some job.
* @return a boolean to signify if the jobName is or is not blacklisted or whitelisted.
* @return a boolean to signify if the jobName is or is not excluded or included.
*/
public static boolean isJobTracked(final String jobName) {
return !isJobBlacklisted(jobName) && isJobWhitelisted(jobName);
return !isJobExcluded(jobName) && isJobIncluded(jobName);
}

/**
Expand Down Expand Up @@ -242,21 +242,21 @@ public static Map<String, Set<String>> getTagsFromGlobalTags() {
}

/**
* Checks if a jobName is blacklisted.
* Checks if a jobName is excluded.
*
* @param jobName - A String containing the name of some job.
* @return a boolean to signify if the jobName is or is not blacklisted.
* @return a boolean to signify if the jobName is or is not excluded.
*/
private static boolean isJobBlacklisted(final String jobName) {
private static boolean isJobExcluded(final String jobName) {
final DatadogGlobalConfiguration datadogGlobalConfig = getDatadogGlobalDescriptor();
if (datadogGlobalConfig == null){
return false;
}
final String blacklistProp = datadogGlobalConfig.getBlacklist();
List<String> blacklist = cstrToList(blacklistProp);
for (String blacklistedJob : blacklist){
Pattern blacklistedJobPattern = Pattern.compile(blacklistedJob);
Matcher jobNameMatcher = blacklistedJobPattern.matcher(jobName);
final String excludedProp = datadogGlobalConfig.getExcluded();
List<String> excluded = cstrToList(excludedProp);
for (String excludedJob : excluded){
Pattern excludedJobPattern = Pattern.compile(excludedJob);
Matcher jobNameMatcher = excludedJobPattern.matcher(jobName);
if (jobNameMatcher.matches()) {
return true;
}
Expand All @@ -266,26 +266,26 @@ private static boolean isJobBlacklisted(final String jobName) {
}

/**
* Checks if a jobName is whitelisted.
* Checks if a jobName is included.
*
* @param jobName - A String containing the name of some job.
* @return a boolean to signify if the jobName is or is not whitelisted.
* @return a boolean to signify if the jobName is or is not included.
*/
private static boolean isJobWhitelisted(final String jobName) {
private static boolean isJobIncluded(final String jobName) {
final DatadogGlobalConfiguration datadogGlobalConfig = getDatadogGlobalDescriptor();
if (datadogGlobalConfig == null){
return true;
}
final String whitelistProp = datadogGlobalConfig.getWhitelist();
final List<String> whitelist = cstrToList(whitelistProp);
for (String whitelistedJob : whitelist){
Pattern whitelistedJobPattern = Pattern.compile(whitelistedJob);
Matcher jobNameMatcher = whitelistedJobPattern.matcher(jobName);
final String includedProp = datadogGlobalConfig.getIncluded();
final List<String> included = cstrToList(includedProp);
for (String includedJob : included){
Pattern includedJobPattern = Pattern.compile(includedJob);
Matcher jobNameMatcher = includedJobPattern.matcher(jobName);
if (jobNameMatcher.matches()) {
return true;
}
}
return whitelist.isEmpty();
return included.isEmpty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class DatadogBuildListener extends RunListener<Run> {
@Override
public void onStarted(Run run, TaskListener listener) {
try {
// Process only if job is NOT in blacklist and is in whitelist
// Process only if job is NOT in excluded and is in included
if (!DatadogUtilities.isJobTracked(run.getParent().getFullName())) {
return;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public void onStarted(Run run, TaskListener listener) {
@Override
public void onCompleted(Run run, @Nonnull TaskListener listener) {
try {
// Process only if job in NOT in blacklist and is in whitelist
// Process only if job in NOT in excluded and is in included
if (!DatadogUtilities.isJobTracked(run.getParent().getFullName())) {
return;
}
Expand Down Expand Up @@ -229,7 +229,7 @@ public void onCompleted(Run run, @Nonnull TaskListener listener) {
@Override
public void onDeleted(Run run) {
try {
// Process only if job is NOT in blacklist and is in whitelist
// Process only if job is NOT in excluded and is in included
if (!DatadogUtilities.isJobTracked(run.getParent().getFullName())) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private boolean isMonitored(FlowNode flowNode) {
return false;
}

// Filter the node if the job has been blacklisted from the Datadog plugin configuration.
// Filter the node if the job has been excluded from the Datadog plugin configuration.
WorkflowRun run = getRun(flowNode);
if (run == null || !DatadogUtilities.isJobTracked(run.getParent().getFullName())) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class DatadogSCMListener extends SCMListener {
public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListener listener,
File changelogFile, SCMRevisionState pollingBaseline) throws Exception {
try {
// Process only if job is NOT in blacklist and is in whitelist
// Process only if job is NOT in excluded and is in included
DatadogJobProperty prop = DatadogUtilities.getDatadogJobProperties(build);
if (!(DatadogUtilities.isJobTracked(build.getParent().getFullName())
&& prop != null && prop.isEmitSCMEvents())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@
</f:entry>
<f:validateButton title="${%Test Hostname}" progress="${%Testing...}" method="testHostname" with="hostname" checkMethod="post" />

<f:entry title="Blacklisted Jobs" description="A comma-separated list of job names that should not monitored." >
<f:textarea field="blacklist" optional="true" default="${blacklist}" />
<f:entry title="Excluded Jobs" description="A comma-separated list of job names that should not monitored." >
<f:textarea field="excluded" optional="true" default="${excluded}" />
</f:entry>

<f:entry title="Whitelisted Jobs" description="A list of job names that should be monitored. An empty whitelist permits all jobs not blacklisted." >
<f:textarea field="whitelist" optional="true" default="${whitelist}" />
<f:entry title="Included Jobs" description="A list of job names that should be monitored. An empty included permits all jobs not excluded." >
<f:textarea field="included" optional="true" default="${included}" />
</f:entry>

<f:entry field="globalTagFileEntry" title="Global Tag File" description="Add tags from default file in workspace.">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class DatadogBuildListenerTest {
private Queue queue;
private WorkflowRun workflowRun;
EnvVars envVars;

@Before
public void setUpMocks() {
this.client = new DatadogClientStub();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testPipeline() throws Exception {

@Test
public void testPipelineOnWorkerNode() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "pipelineOnSlave");
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "pipelineOnWorkerNode");
p.setDefinition(new CpsFlowDefinition("node('test') {echo 'foo'}\n", true));
p.scheduleBuild2(0).get();
Assert.assertTrue(stubClient.logLines.contains("foo"));
Expand Down

0 comments on commit f009266

Please sign in to comment.