From f009266317b55df24d11acf730395d6f13d858bd Mon Sep 17 00:00:00 2001 From: Sarah Witt Date: Mon, 20 Jul 2020 15:40:45 -0400 Subject: [PATCH] Use more inclusive naming of config (#93) * Use more inclusive naming * Add new test * Update comment * remove comments * Fix logic * Remove unneeded util method --- README.md | 8 +- .../datadog/DatadogGlobalConfiguration.java | 112 +++++++++++++----- .../plugins/datadog/DatadogUtilities.java | 40 +++---- .../listeners/DatadogBuildListener.java | 6 +- .../listeners/DatadogGraphListener.java | 2 +- .../datadog/listeners/DatadogSCMListener.java | 2 +- .../DatadogGlobalConfiguration/config.jelly | 8 +- .../listeners/DatadogBuildListenerTest.java | 2 +- .../datadog/logs/LogCollectionTest.java | 2 +- 9 files changed, 116 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 9454a1903..672416935 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ d.setTargetApiURL('https://api.datadoghq.com/api/') d.setTargetApiKey('') // 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/') @@ -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() @@ -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` | diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java index 8dbdbd959..8a5e03a09 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java @@ -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"; @@ -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; @@ -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); @@ -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")); @@ -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; } /** @@ -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) { diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java index f2c60718a..2c1996dae 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java @@ -121,13 +121,13 @@ public static Map> 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); } /** @@ -242,21 +242,21 @@ public static Map> 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 blacklist = cstrToList(blacklistProp); - for (String blacklistedJob : blacklist){ - Pattern blacklistedJobPattern = Pattern.compile(blacklistedJob); - Matcher jobNameMatcher = blacklistedJobPattern.matcher(jobName); + final String excludedProp = datadogGlobalConfig.getExcluded(); + List excluded = cstrToList(excludedProp); + for (String excludedJob : excluded){ + Pattern excludedJobPattern = Pattern.compile(excludedJob); + Matcher jobNameMatcher = excludedJobPattern.matcher(jobName); if (jobNameMatcher.matches()) { return true; } @@ -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 whitelist = cstrToList(whitelistProp); - for (String whitelistedJob : whitelist){ - Pattern whitelistedJobPattern = Pattern.compile(whitelistedJob); - Matcher jobNameMatcher = whitelistedJobPattern.matcher(jobName); + final String includedProp = datadogGlobalConfig.getIncluded(); + final List 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(); } /** diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListener.java b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListener.java index 8ddfb7c58..0700a389d 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListener.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListener.java @@ -70,7 +70,7 @@ public class DatadogBuildListener extends RunListener { @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; } @@ -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; } @@ -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; } diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogGraphListener.java b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogGraphListener.java index e442bdeb1..82ebf2469 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogGraphListener.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogGraphListener.java @@ -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; diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogSCMListener.java b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogSCMListener.java index bd108dff3..3b671ed85 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogSCMListener.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogSCMListener.java @@ -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())) { diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/config.jelly b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/config.jelly index a2297a535..26fbb6850 100644 --- a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/config.jelly +++ b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/config.jelly @@ -61,12 +61,12 @@ - - + + - - + + diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListenerTest.java b/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListenerTest.java index 279426c23..540798cb0 100644 --- a/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListenerTest.java +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/listeners/DatadogBuildListenerTest.java @@ -54,7 +54,7 @@ public class DatadogBuildListenerTest { private Queue queue; private WorkflowRun workflowRun; EnvVars envVars; - + @Before public void setUpMocks() { this.client = new DatadogClientStub(); diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/logs/LogCollectionTest.java b/src/test/java/org/datadog/jenkins/plugins/datadog/logs/LogCollectionTest.java index 8ea66448f..c8f284fe4 100644 --- a/src/test/java/org/datadog/jenkins/plugins/datadog/logs/LogCollectionTest.java +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/logs/LogCollectionTest.java @@ -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"));