Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update queue metrics to correctly calculate pipeline names #378

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,21 +83,9 @@ protected void doRun() throws Exception {
for (Queue.Item item : items) {
Map<String, Set<String>> 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;
Expand Down Expand Up @@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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()));
}
Expand All @@ -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
Expand Down
Loading