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

Apply included/excluded job filters when publishing queue metrics #380

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -99,6 +99,11 @@ protected void doRun() throws Exception {
} else {
job_name = "unknown";
}

if (!DatadogUtilities.isJobTracked(job_name)) {
continue;
}

TagsUtil.addTagToTags(job_tags, "job_name", job_name);
boolean isStuck = false;
boolean isBuildable = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package org.datadog.jenkins.plugins.datadog.publishers;

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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

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.model.Messages;
import hudson.model.ParametersAction;
import hudson.model.Queue;
import hudson.model.StringParameterValue;
import hudson.slaves.OfflineCause;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration;
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.datadog.jenkins.plugins.datadog.clients.DatadogMetric;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.SleepBuilder;

public class DatadogQueuePublisherTest {
@ClassRule
Expand Down Expand Up @@ -54,7 +58,35 @@ public void testQueueMetrics() throws Exception {
expectedTags[1] = "job_name:" + displayName;
queuePublisher.doRun();
client.assertMetric("jenkins.queue.job.in_queue", 1, hostname, expectedTags);

}

@Test
public void testQueueMetricsJobFiltering() throws Exception {
DatadogGlobalConfiguration globalConfiguration = DatadogUtilities.getDatadogGlobalDescriptor();
assertNotNull(globalConfiguration);

String previousExcludedPattern = globalConfiguration.getExcluded();
try {
String filteredJobName = "filtered-project";
globalConfiguration.setExcluded(filteredJobName);

final FreeStyleProject project = jenkins.createFreeStyleProject("filtered-project");
project.getBuildersList().add(new SleepBuilder(10000));

jenkins.jenkins.getQueue().schedule(project);

// 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()));
}

queuePublisher.doRun();

List<DatadogMetric> filteredJobMetrics = client.getMetrics().stream().filter(metric -> metric.getTags().contains("job_name:" + filteredJobName)).collect(Collectors.toList());
assertEquals("Did not expect any metrics for filtered job, found: " + filteredJobMetrics, 0, filteredJobMetrics.size());
} finally {
globalConfiguration.setExcluded(previousExcludedPattern);
}
}

@Test
Expand Down Expand Up @@ -133,7 +165,7 @@ public void testQueueMetricsMultipleProjects() throws Exception {
client.assertMetric("jenkins.queue.job.buildable", 1, hostname, expectedTags1);
client.assertMetric("jenkins.queue.job.buildable", 1, hostname, expectedTags2);
}

@Test
public void testAllQueueMetrics() throws Exception {
String hostname = DatadogUtilities.getHostname(null);
Expand Down
Loading