From e6cf3f49e9b3c903d27a41429d9bf4871a239818 Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko Date: Thu, 18 Jan 2024 13:55:48 +0100 Subject: [PATCH] Update queue metrics to correctly calculate pipeline names --- .../publishers/DatadogQueuePublisher.java | 44 ++++++++-------- .../publishers/DatadogQueuePublisherTest.java | 51 ++++++++++++------- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/publishers/DatadogQueuePublisher.java b/src/main/java/org/datadog/jenkins/plugins/datadog/publishers/DatadogQueuePublisher.java index 5bd56ea4e..01c7fa5f9 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/publishers/DatadogQueuePublisher.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/publishers/DatadogQueuePublisher.java @@ -31,18 +31,17 @@ of this software and associated documentation files (the "Software"), to deal import hudson.model.Queue; import hudson.model.Queue.Task; import hudson.model.Run; - +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; import org.datadog.jenkins.plugins.datadog.DatadogClient; import org.datadog.jenkins.plugins.datadog.DatadogUtilities; import org.datadog.jenkins.plugins.datadog.clients.ClientFactory; import org.datadog.jenkins.plugins.datadog.clients.Metrics; import org.datadog.jenkins.plugins.datadog.util.TagsUtil; - +import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.TimeUnit; -import java.util.logging.Logger; /** * This class registers a {@link PeriodicWork} with Jenkins to run periodically in order to enable @@ -84,21 +83,9 @@ protected void doRun() throws Exception { for (Queue.Item item : items) { Map> job_tags = DatadogUtilities.getTagsFromGlobalTags(); job_tags = TagsUtil.addTagToTags(job_tags, "jenkins_url", DatadogUtilities.getJenkinsUrl()); - - String job_name; + Task task = item.task; - if (task instanceof FreeStyleProject) { - job_name = task.getFullDisplayName(); - } else if (task instanceof ExecutorStepExecution.PlaceholderTask) { - Run run = ((ExecutorStepExecution.PlaceholderTask) task).runForDisplay(); - if (run != null) { - job_name = run.getParent().getFullName(); - } else { - job_name = "unknown"; - } - } else { - job_name = "unknown"; - } + String job_name = getJobName(task); TagsUtil.addTagToTags(job_tags, "job_name", job_name); boolean isStuck = false; boolean isBuildable = false; @@ -140,4 +127,21 @@ protected void doRun() throws Exception { DatadogUtilities.severe(logger, e, "Failed to compute and send queue metrics"); } } + + private static String getJobName(Task task) { + if (task instanceof FreeStyleProject) { + return task.getFullDisplayName(); + } + if (task instanceof ExecutorStepExecution.PlaceholderTask) { + Run run = ((ExecutorStepExecution.PlaceholderTask) task).runForDisplay(); + if (run != null) { + return run.getParent().getFullName(); + } + } + if (task instanceof WorkflowJob) { + WorkflowJob workflowJob = (WorkflowJob) task; + return workflowJob.getFullName(); + } + return "unknown"; + } } diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/publishers/DatadogQueuePublisherTest.java b/src/test/java/org/datadog/jenkins/plugins/datadog/publishers/DatadogQueuePublisherTest.java index b90526209..8257b7509 100644 --- a/src/test/java/org/datadog/jenkins/plugins/datadog/publishers/DatadogQueuePublisherTest.java +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/publishers/DatadogQueuePublisherTest.java @@ -1,26 +1,24 @@ package org.datadog.jenkins.plugins.datadog.publishers; +import hudson.model.Cause; +import hudson.model.Computer; +import hudson.model.FreeStyleProject; +import hudson.model.Messages; +import hudson.model.ParametersAction; +import hudson.model.Queue; +import hudson.model.StringParameterValue; +import hudson.slaves.OfflineCause; +import java.util.Arrays; +import org.datadog.jenkins.plugins.datadog.DatadogUtilities; +import org.datadog.jenkins.plugins.datadog.clients.ClientFactory; +import org.datadog.jenkins.plugins.datadog.clients.DatadogClientStub; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.SleepBuilder; -import java.util.Arrays; - -import org.datadog.jenkins.plugins.datadog.DatadogUtilities; -import org.datadog.jenkins.plugins.datadog.clients.ClientFactory; -import org.datadog.jenkins.plugins.datadog.clients.DatadogClientStub; - -import hudson.model.Messages; -import hudson.model.ParametersAction; -import hudson.model.StringParameterValue; -import hudson.model.Cause; -import hudson.model.Computer; -import hudson.model.Queue; -import hudson.model.FreeStyleProject; -import hudson.slaves.OfflineCause; - public class DatadogQueuePublisherTest { @ClassRule public static JenkinsRule jenkins = new JenkinsRule(); @@ -44,7 +42,27 @@ public void testQueueMetrics() throws Exception { jenkins.jenkins.getQueue().schedule(project); - // set all the computers offline so they can't execute any buils, filling up the queue + // set all the computers offline so they can't execute any buils, filling up the queue + for (Computer computer: jenkins.jenkins.getComputers()){ + computer.setTemporarilyOffline(true, OfflineCause.create(Messages._Hudson_Computer_DisplayName())); + } + + final String[] expectedTags = new String[2]; + expectedTags[0] = "jenkins_url:" + jenkins.getURL().toString(); + expectedTags[1] = "job_name:" + displayName; + queuePublisher.doRun(); + client.assertMetric("jenkins.queue.job.in_queue", 1, hostname, expectedTags); + } + + @Test + public void testQueuePipeline() throws Exception { + String hostname = DatadogUtilities.getHostname(null); + final WorkflowJob project = jenkins.createProject(WorkflowJob.class); + String displayName = project.getDisplayName(); + + project.scheduleBuild(10000, new Cause.RemoteCause("host", "0")); + + // set all the computers offline so they can't execute any builds, filling up the queue for (Computer computer: jenkins.jenkins.getComputers()){ computer.setTemporarilyOffline(true, OfflineCause.create(Messages._Hudson_Computer_DisplayName())); } @@ -54,7 +72,6 @@ public void testQueueMetrics() throws Exception { expectedTags[1] = "job_name:" + displayName; queuePublisher.doRun(); client.assertMetric("jenkins.queue.job.in_queue", 1, hostname, expectedTags); - } @Test