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

Commit

Permalink
Merge pull request #182 from DataDog/gzu/single_tag_global_conf
Browse files Browse the repository at this point in the history
  • Loading branch information
gzussa authored Dec 16, 2019
2 parents 7976116 + eaea4c6 commit 0bb7413
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ From the global configuration page, at `Manage Jenkins -> Configure System`.
* A comma-separated list of regex to match job names that should not be monitored. (eg: susans-job,johns-.*,prod_folder/prod_release).
* Whitelisted Jobs
* A comma-separated list of regex to match job names that should be monitored. (eg: susans-job,johns-.*,prod_folder/prod_release).
* Global Tag File
* Path to the workspace file containing a comma separated list of tags (not compatible with Pipeline jobs).
* Global Job Tags
* A regex to match a job, and a list of tags to apply to that job, all separated by a comma.
* tags can reference match groups in the regex using the $ symbol
Expand All @@ -145,8 +147,8 @@ From the global configuration page, at `Manage Jenkins -> Configure System`.

From a job specific configuration page
* Custom tags
* From a file in the job workspace (not compatible with Pipeline jobs).
* As text properties directly from the configuration page.
* From a `File` in the job workspace (not compatible with Pipeline jobs). If set, it will override the `Global Job Tags` configuration.
* As text `Properties` directly from the configuration page.
* Send Source Control Management events
* Enabled by default, it submits `Source Control Management Events Type` of events and metrics.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class DatadogGlobalConfiguration extends GlobalConfiguration {
private String hostname = null;
private String blacklist = null;
private String whitelist = null;
private String globalTagFile = null;
private String globalTags = null;
private String globalJobTags = null;
private String targetMetricURL = "https://api.datadoghq.com/api/";
Expand Down Expand Up @@ -151,6 +152,7 @@ public boolean configure(final StaplerRequest req, final JSONObject formData) th
this.setHostname(formData.getString("hostname"));
this.setBlacklist(formData.getString("blacklist"));
this.setWhitelist(formData.getString("whitelist"));
this.setGlobalTagFile(formData.getString("globalTagFile"));
this.setGlobalTags(formData.getString("globalTags"));
this.setGlobalJobTags(formData.getString("globalJobTags"));
this.setTargetMetricURL(formData.getString("targetMetricURL"));
Expand Down Expand Up @@ -250,6 +252,26 @@ public void setWhitelist(final String jobs) {
this.whitelist = jobs;
}

/**
* Gets the globalTagFile set in the job configuration.
*
* @return a String representing the relative path to a globalTagFile
*/
public String getGlobalTagFile() {
return globalTagFile;
}

/**
* Setter function for the globalFile global configuration,
* accepting a comma-separated string of tags.
*
* @param globalTagFile - a comma-separated list of tags.
*/
@DataBoundSetter
public void setGlobalTagFile(String globalTagFile) {
this.globalTagFile = globalTagFile;
}

/**
* Getter function for the globalTags global configuration, containing
* a comma-separated list of tags that should be applied everywhere.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static DatadogGlobalConfiguration getDatadogGlobalDescriptor() {
* @param r - Current build.
* @return - The configured {@link DatadogJobProperty}. Null if not there
*/
public static DatadogJobProperty retrieveProperty(@Nonnull Run r) {
public static DatadogJobProperty getDatadogJobProperties(@Nonnull Run r) {
return (DatadogJobProperty) r.getParent().getProperty(DatadogJobProperty.class);
}

Expand All @@ -64,8 +64,12 @@ public static Map<String, Set<String>> getBuildTags(Run run, @Nonnull TaskListen
Map<String, Set<String>> result = new HashMap<>();
String jobName = run.getParent().getFullName();
final String globalJobTags = getDatadogGlobalDescriptor().getGlobalJobTags();
final DatadogJobProperty property = DatadogUtilities.retrieveProperty(run);
final String workspaceTagFile = property.readTagFile(run);
final DatadogJobProperty property = DatadogUtilities.getDatadogJobProperties(run);
String workspaceTagFile = property.readTagFile(run);
// If job doesn't have a workspace Tag File set we check if one has been defined globally
if(workspaceTagFile == null){
workspaceTagFile = getDatadogGlobalDescriptor().getGlobalTagFile();
}
try {
final EnvVars envVars = run.getEnvironment(listener);
if (workspaceTagFile != null) {
Expand Down Expand Up @@ -125,7 +129,7 @@ private static Map<String, Set<String>> getTagsFromGlobalJobTags(String jobName,
Matcher jobNameMatcher = jobNamePattern.matcher(jobName);
if (jobNameMatcher.matches()) {
for (int i = 1; i < jobInfo.size(); i++) {
String[] tagItem = jobInfo.get(i).replaceAll(" ", "").split(":");
String[] tagItem = jobInfo.get(i).replaceAll(" ", "").split(":", 2);
if (tagItem.length == 2) {
String tagName = tagItem[0];
String tagValue = tagItem[1];
Expand All @@ -143,6 +147,13 @@ private static Map<String, Set<String>> getTagsFromGlobalJobTags(String jobName,
Set<String> tagValues = tags.containsKey(tagName) ? tags.get(tagName) : new HashSet<String>();
tagValues.add(tagValue.toLowerCase());
tags.put(tagName, tagValues);
} else if(tagItem.length == 1) {
String tagName = tagItem[0];
Set<String> tagValues = tags.containsKey(tagName) ? tags.get(tagName) : new HashSet<String>();
tagValues.add(""); // no values
tags.put(tagName, tagValues);
} else {
logger.fine(String.format("Ignoring the tag %s. It is empty.", tagItem));
}
}
}
Expand All @@ -169,13 +180,20 @@ public static Map<String, Set<String>> getTagsFromGlobalTags() {
}

for (int i = 0; i < tagList.size(); i++) {
String[] tagItem = tagList.get(i).replaceAll(" ", "").split(":");
String[] tagItem = tagList.get(i).replaceAll(" ", "").split(":", 2);
if(tagItem.length == 2) {
String tagName = tagItem[0];
String tagValue = tagItem[1];
Set<String> tagValues = tags.containsKey(tagName) ? tags.get(tagName) : new HashSet<String>();
tagValues.add(tagValue.toLowerCase());
tags.put(tagName, tagValues);
} else if(tagItem.length == 1) {
String tagName = tagItem[0];
Set<String> tagValues = tags.containsKey(tagName) ? tags.get(tagName) : new HashSet<String>();
tagValues.add(""); // no values
tags.put(tagName, tagValues);
} else {
logger.fine(String.format("Ignoring the tag %s. It is empty.", tagItem));
}
}
}
Expand Down Expand Up @@ -271,14 +289,19 @@ public static Map<String, Set<String>> computeTagListFromVarList(EnvVars envVars
}
for (int i = 0; i < tagList.size(); i++) {
String tag = tagList.get(i).replaceAll(" ", "");
String[]expanded = envVars.expand(tag).split("=");
if (expanded.length > 1) {
String[] expanded = envVars.expand(tag).split("=", 2);
if (expanded.length == 2) {
String name = expanded[0];
String value = expanded[1];
Set<String> values = result.containsKey(name) ? result.get(name) : new HashSet<String>();
values.add(value);
result.put(name, values);
logger.fine(String.format("Emitted tag %s:%s", expanded[0], expanded[1]));
logger.fine(String.format("Emitted tag %s:%s", name, value));
} else if(expanded.length == 1) {
String name = expanded[0];
Set<String> values = result.containsKey(name) ? result.get(name) : new HashSet<String>();
values.add(""); // no values
result.put(name, values);
} else {
logger.fine(String.format("Ignoring the tag %s. It is empty.", tag));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListene
File changelogFile, SCMRevisionState pollingBaseline) throws Exception {
try {
// Process only if job is NOT in blacklist and is in whitelist
DatadogJobProperty prop = DatadogUtilities.retrieveProperty(build);
DatadogJobProperty prop = DatadogUtilities.getDatadogJobProperties(build);
if (!(DatadogUtilities.isJobTracked(build.getParent().getFullName())
&& prop != null && prop.isEmitSCMEvents())) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public static JSONArray convertTagsToJSONArray(Map<String, Set<String>> tags){
for (String name : tags.keySet()) {
Set<String> values = tags.get(name);
for (String value : values){
result.add(String.format("%s:%s", name, value));
if ("".equals(value)){
result.add(name); // Tag with no value
}else{
result.add(String.format("%s:%s", name, value));
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<f:textarea field="whitelist" optional="true" default="${whitelist}" />
</f:entry>

<f:entry field="globalTagFileEntry" title="Global Tag File" description="Add tags from default file in workspace">
<f:textbox field="globalTagFile" optional="true" default="${globalTagFile}"/>
</f:entry>

<f:entry title="Global Tags" field="globalTagsEntry" description="A list of tags to apply globally to all submissions." >
<f:textarea field="globalTags" optional="true" default="${globalTags}" />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
<p>Read a file from workspace as a newline separated list of tags to push to Datadog. Location is relative to workspace.</p>
<p>Example: <em>os=${OS_PARAM}</em>.</p>
<p>The value can either be something that is automatically expanded by Jenkins (environment and/or build variable), or a static value. The OS_PARAM in this case is expanded by Jenkins.</p>
<p><em>Note:</em> Tags set this way, in the workspace, won't appear in started events. To provide visibility of a jobs lifespan, check the option for sending an extra event after each successful checkout.</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:section title="Datadog Tagging">
<f:optionalBlock field="enableFile" checked="${enableFile}" title="Add tags from file in workspace" inline="true">
<f:entry field="tagFileEntry" title="File" description="Add tags from file in workspace">
<f:entry field="tagFileEntry" title="File" description="Add tags from file in workspace">
<f:textbox field="tagFile" optional="true" default="${tagFile}"/>
</f:entry>
</f:optionalBlock>

<f:optionalBlock field="enableProperty" checked="${enableProperty}" title="Add tags from list of properties" inline="true">
<f:entry field="tagPropertiesEntry" title="Properties" description="Add tags from list of properties">
<f:entry field="tagPropertiesEntry" title="Properties" description="Add tags from list of properties">
<f:textarea field="tagProperties" optional="true" default="${tagProperties}"/>
</f:entry>
</f:optionalBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<p>Example: <em>os=${OS_PARAM}</em>.</p>
<p>The value can either be something that is automatically expanded by Jenkins (environment and/or build variable), or a static value. The OS_PARAM in this case is expanded by Jenkins.</p>
<p><em>Note:</em> Tags set this way, in the workspace, won't appear in started events. To provide visibility of a jobs lifespan, check the option for sending an extra event after each successful checkout.</p>
<p><em>Note:</em> Setting a value here will override the value you may have set for <em>Global Tag File</em> in <em>Manage Jenkins</em> -> <em>Configure System</em>.</p>
</div>

0 comments on commit 0bb7413

Please sign in to comment.