Class DatadogUtilities
- java.lang.Object
-
- org.datadog.jenkins.plugins.datadog.DatadogUtilities
-
public class DatadogUtilities extends Object
-
-
Constructor Summary
Constructors Constructor Description DatadogUtilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcleanUpTraceActions(Actionable actionable)Removes all actions related to traces for Jenkins pipelines.static Map<String,Set<String>>computeTagListFromVarList(EnvVars envVars, String varList)static List<String>cstrToList(String str)Converts a Comma Separated List into a List Objectstatic <T> List<T>cstrToList(String str, Function<String,T> converter)Converts a comma-separated string into a liststatic longcurrentTimeMillis()static booleanenvVar(String name, boolean defaultValue)static doubleenvVar(String name, double defaultValue)static intenvVar(String name, int defaultValue)static <T> TenvVar(String name, Function<String,T> parser, T defaultValue)static StringgetAwsInstanceID()static Map<String,Set<String>>getBuildTags(Run run, EnvVars envVars)Builds extraTags if any are configured in the Job.static StringgetCatchErrorResult(org.jenkinsci.plugins.workflow.graph.BlockStartNode startNode)static Map<String,Set<String>>getComputerTags(Computer computer)static DatadogGlobalConfigurationgetDatadogGlobalDescriptor()static DatadogJobPropertygetDatadogJobProperties(Run r)static List<DatadogPipelineAction>getDatadogPipelineActions(Run<?,?> run, org.jenkinsci.plugins.workflow.graph.FlowNode node)Gets DatadogPipelineAction instances attached to given run and node.static StringgetDatadogPluginVersion()static org.jenkinsci.plugins.workflow.graph.BlockStartNodegetEnclosingStageNode(org.jenkinsci.plugins.workflow.graph.FlowNode node)Returns enclosing stage node for the given node.static ThrowablegetErrorObj(org.jenkinsci.plugins.workflow.graph.FlowNode flowNode)Returns theThrowableof a certainFlowNode, if it has errors.static BlockingQueue<org.apache.commons.lang3.tuple.Pair<Date,Throwable>>getExceptionsBuffer()static StringgetHostname(EnvVars envVars)Getter function to return either the saved hostname global configuration, or the hostname that is set in the Jenkins host itself.static StringgetItemName(Item item)static StringgetJenkinsUrl()static StringgetNodeHostname(EnvVars envVars, Computer computer)static Set<String>getNodeLabels(Computer computer)static StringgetNodeName(Computer computer)static StringgetResultTag(org.jenkinsci.plugins.workflow.graph.FlowNode node)static LonggetRunStartTimeInMillis(Run run)static Map<String,Set<String>>getTagsFromGlobalTags()Getter function for the globalTags global configuration, containing a comma-separated list of tags that should be applied everywhere.static Map<String,Set<String>>getTagsFromPipelineAction(Run<?,?> run)Pipeline extraTags if any are configured in the Job from DatadogPipelineAction.static Map<String,Set<String>>getTagsFromPipelineAction(Run<?,?> run, org.jenkinsci.plugins.workflow.graph.FlowNode node)static TaskListenergetTaskListener(Run run)static longgetTimeMillis(org.jenkinsci.plugins.workflow.graph.FlowNode flowNode)Returns the startTime of a certainFlowNode, if it has time information.static StringgetUserId()static booleanisJobTracked(Run<?,?> run)Checks inclusion/exclusion filter settings to see if a run should be tracked by the pluginstatic booleanisJobTracked(String jobName)Checks if a jobName is excluded, included, or neither.static booleanisMainNode(String nodeName)static booleanisStageNode(org.jenkinsci.plugins.workflow.graph.FlowNode flowNode)Returns true if aFlowNodeis a Stage node.static BooleanisValidHostname(String hostname)Validator function to ensure that the hostname is valid.static booleanisValidISO8601Date(String date)static List<String>linesToList(String str)Converts a string List into a List Objectstatic <T> StringlistToCstr(List<T> list, Function<T,String> converter)Converts a list to a comma-separated stringstatic voidlogException(Logger logger, Level logLevel, String message, Throwable e)static voidsevere(Logger logger, Throwable e, String message)static booleanshouldSendEvent(String eventName)Checks to see if event should be sent to clientstatic StringstatusFromResult(String result)Returns a normalized result for traces.static inttoInt(boolean b)static StringtoISO8601(Date date)Returns a date as String in the ISO8601 formatstatic StringtoJson(Map<String,String> map)Returns a JSON object string based on the map.static StringtoJson(Set<String> set)Returns a JSON array string based on the set.
-
-
-
Method Detail
-
getDatadogGlobalDescriptor
public static DatadogGlobalConfiguration getDatadogGlobalDescriptor()
- Returns:
- - The descriptor for the Datadog plugin. In this case the global configuration.
-
getDatadogJobProperties
public static DatadogJobProperty getDatadogJobProperties(@Nonnull Run r)
- Parameters:
r- - Current build.- Returns:
- - The configured
DatadogJobProperty. Null if not there
-
getBuildTags
public static Map<String,Set<String>> getBuildTags(Run run, EnvVars envVars)
Builds extraTags if any are configured in the Job.- Parameters:
run- - Current buildenvVars- - Environment Variables- Returns:
- A
HashMapcontaining the key,value pairs of tags if any.
-
getTagsFromPipelineAction
public static Map<String,Set<String>> getTagsFromPipelineAction(Run<?,?> run)
Pipeline extraTags if any are configured in the Job from DatadogPipelineAction.- Parameters:
run- - Current build- Returns:
- A
Mapcontaining the key,value pairs of tags if any.
-
getTagsFromPipelineAction
public static Map<String,Set<String>> getTagsFromPipelineAction(Run<?,?> run, @Nullable org.jenkinsci.plugins.workflow.graph.FlowNode node)
-
getDatadogPipelineActions
public static List<DatadogPipelineAction> getDatadogPipelineActions(Run<?,?> run, @Nullable org.jenkinsci.plugins.workflow.graph.FlowNode node)
Gets DatadogPipelineAction instances attached to given run and node. The list is ordered from the innermost action to the outermost one (that is, actions that are nested deeper go first in the list).
-
isJobTracked
public static boolean isJobTracked(Run<?,?> run)
Checks inclusion/exclusion filter settings to see if a run should be tracked by the plugin- Parameters:
run- The run to be checked- Returns:
trueif the run should be tracked by the plugin,falseotherwise
-
isJobTracked
public static boolean isJobTracked(String jobName)
Checks if a jobName is excluded, included, or neither.- Parameters:
jobName- - A String containing the name of some job.- Returns:
- a boolean to signify if the jobName is or is not excluded or included.
-
getTagsFromGlobalTags
public static Map<String,Set<String>> getTagsFromGlobalTags()
Getter function for the globalTags global configuration, containing a comma-separated list of tags that should be applied everywhere.- Returns:
- a map containing the globalTags global configuration.
-
cstrToList
public static List<String> cstrToList(String str)
Converts a Comma Separated List into a List Object- Parameters:
str- - A String containing a comma separated list of items.- Returns:
- a String List with all items transform with trim and lower case
-
cstrToList
public static <T> List<T> cstrToList(String str, Function<String,T> converter)
Converts a comma-separated string into a list- Parameters:
str- - A string containing a comma separated list of items.converter- - Converter function that is applied to every list element
-
listToCstr
public static <T> String listToCstr(List<T> list, Function<T,String> converter)
Converts a list to a comma-separated string
-
linesToList
public static List<String> linesToList(String str)
Converts a string List into a List Object- Parameters:
str- - A String containing a comma separated list of items.- Returns:
- a String List with all items
-
computeTagListFromVarList
public static Map<String,Set<String>> computeTagListFromVarList(EnvVars envVars, String varList)
-
getAwsInstanceID
public static String getAwsInstanceID() throws IOException
- Throws:
IOException
-
getHostname
public static String getHostname(EnvVars envVars)
Getter function to return either the saved hostname global configuration, or the hostname that is set in the Jenkins host itself. Returns null if no valid hostname is found.Tries, in order: Jenkins configuration Jenkins hostname environment variable AWS instance ID, if enabled System hostname environment variable Unix hostname via `/bin/hostname -f` Localhost hostname
- Parameters:
envVars- - The Jenkins environment variables- Returns:
- a human readable String for the hostname.
-
isValidHostname
public static Boolean isValidHostname(String hostname)
Validator function to ensure that the hostname is valid. Also, fails on empty String.- Parameters:
hostname- - A String object containing the name of a host.- Returns:
- a boolean representing the validity of the hostname
-
isMainNode
public static boolean isMainNode(String nodeName)
-
getNodeHostname
public static String getNodeHostname(@Nullable EnvVars envVars, @Nullable Computer computer)
-
getUserId
public static String getUserId()
-
currentTimeMillis
public static long currentTimeMillis()
-
getJenkinsUrl
public static String getJenkinsUrl()
-
getResultTag
public static String getResultTag(@Nonnull org.jenkinsci.plugins.workflow.graph.FlowNode node)
-
isStageNode
public static boolean isStageNode(org.jenkinsci.plugins.workflow.graph.FlowNode flowNode)
Returns true if aFlowNodeis a Stage node.- Parameters:
flowNode- the flow node to evaluate- Returns:
- flag indicating if a flowNode is a Stage node.
-
getEnclosingStageNode
public static org.jenkinsci.plugins.workflow.graph.BlockStartNode getEnclosingStageNode(org.jenkinsci.plugins.workflow.graph.FlowNode node)
Returns enclosing stage node for the given node. Never returns the node itself.
-
statusFromResult
public static String statusFromResult(String result)
Returns a normalized result for traces.- Parameters:
result- (success, failure, error, aborted, not_build, canceled, skipped, unstable, unknown)- Returns:
- the normalized result for the traces based on the jenkins result
-
logException
public static void logException(Logger logger, Level logLevel, String message, Throwable e)
-
getExceptionsBuffer
public static BlockingQueue<org.apache.commons.lang3.tuple.Pair<Date,Throwable>> getExceptionsBuffer()
-
toInt
public static int toInt(boolean b)
-
toISO8601
public static String toISO8601(Date date)
Returns a date as String in the ISO8601 format- Parameters:
date- the date object to transform- Returns:
- date as String in the ISO8601 format
-
isValidISO8601Date
public static boolean isValidISO8601Date(String date)
-
toJson
public static String toJson(Set<String> set)
Returns a JSON array string based on the set.- Parameters:
set- the set to transform into a JSON- Returns:
- json array string
-
toJson
public static String toJson(Map<String,String> map)
Returns a JSON object string based on the map.- Parameters:
map- the map to transform into a JSON- Returns:
- json object string
-
cleanUpTraceActions
public static void cleanUpTraceActions(Actionable actionable)
Removes all actions related to traces for Jenkins pipelines.- Parameters:
actionable- a domain object that can contain actions, such as run or flow node.
-
getCatchErrorResult
public static String getCatchErrorResult(org.jenkinsci.plugins.workflow.graph.BlockStartNode startNode)
-
shouldSendEvent
public static boolean shouldSendEvent(String eventName)
Checks to see if event should be sent to client- Parameters:
eventName- - the event to check- Returns:
- true if event should be sent to client
-
getErrorObj
public static Throwable getErrorObj(org.jenkinsci.plugins.workflow.graph.FlowNode flowNode)
Returns theThrowableof a certainFlowNode, if it has errors.- Returns:
- throwable associated with a certain flowNode.
-
getTaskListener
@Nullable public static TaskListener getTaskListener(Run run) throws IOException
- Throws:
IOException
-
getTimeMillis
public static long getTimeMillis(org.jenkinsci.plugins.workflow.graph.FlowNode flowNode)
Returns the startTime of a certainFlowNode, if it has time information.- Returns:
- startTime of the flowNode in milliseconds.
-
getDatadogPluginVersion
@Nullable public static String getDatadogPluginVersion()
-
envVar
public static int envVar(String name, int defaultValue)
-
envVar
public static double envVar(String name, double defaultValue)
-
envVar
public static boolean envVar(String name, boolean defaultValue)
-
-