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 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 @@ -86,6 +86,11 @@ protected void doRun() throws Exception {

Task task = item.task;
String job_name = getJobName(task);

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
Expand Up @@ -185,6 +185,10 @@ public boolean assertMetricValuesMin(String name, double value, String hostname,
return false;
}

public List<DatadogMetric> getMetrics() {
return Collections.unmodifiableList(this.metrics);
}

public boolean assertMetric(String name, String hostname, String[] tags) {
// Assert that a metric with the same name and tags has already been submitted without checking the value.
DatadogMetric m = new DatadogMetric(name, 0, hostname, Arrays.asList(tags));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.datadog.jenkins.plugins.datadog.publishers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import hudson.model.Cause;
import hudson.model.Computer;
import hudson.model.FreeStyleProject;
Expand All @@ -9,9 +12,13 @@
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.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Before;
import org.junit.ClassRule;
Expand Down Expand Up @@ -73,6 +80,35 @@ public void testQueuePipeline() throws Exception {
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
public void testQueueMetricsMultipleBuilds() throws Exception {
Expand Down Expand Up @@ -150,7 +186,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