Skip to content

Commit

Permalink
Merge pull request #397 from paychex/jenkins-node-build-type-fix
Browse files Browse the repository at this point in the history
Fix duplicate TYPE lines for jenkins_node_builds metric
  • Loading branch information
Marky Jackson authored May 20, 2022
2 parents 3f44a18 + 39382c8 commit 87c60aa
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.jenkinsci.plugins.prometheus.ExecutorCollector;
import org.jenkinsci.plugins.prometheus.JenkinsStatusCollector;
import org.jenkinsci.plugins.prometheus.JobCollector;
import org.jenkinsci.plugins.prometheus.util.MetricsFormatter;
import org.jenkinsci.plugins.prometheus.util.JenkinsNodeBuildsSampleBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,7 +30,7 @@ public DefaultPrometheusMetrics() {
CollectorRegistry collectorRegistry = CollectorRegistry.defaultRegistry;
collectorRegistry.register(new JobCollector());
collectorRegistry.register(new JenkinsStatusCollector());
collectorRegistry.register(new DropwizardExports(Metrics.metricRegistry()));
collectorRegistry.register(new DropwizardExports(Metrics.metricRegistry(), new JenkinsNodeBuildsSampleBuilder()));
collectorRegistry.register(new DiskUsageCollector());
collectorRegistry.register(new ExecutorCollector());

Expand All @@ -52,7 +52,7 @@ public String getMetrics() {
public void collectMetrics() {
try (StringWriter buffer = new StringWriter()) {
TextFormat.write004(buffer, collectorRegistry.metricFamilySamples());
cachedMetrics.set(MetricsFormatter.formatMetrics(buffer.toString()));
cachedMetrics.set(buffer.toString());
} catch (IOException e) {
logger.debug("Unable to collect metrics");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.jenkinsci.plugins.prometheus.util;

import io.prometheus.client.Collector;
import io.prometheus.client.dropwizard.samplebuilder.DefaultSampleBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A class that converts jenkins.node[.<node_name>].builds to jenkins.node.builds with a label of node=<node_name> (or master if not set)
* before creating a Sample
*/
public class JenkinsNodeBuildsSampleBuilder extends DefaultSampleBuilder {
// Note that nodes can have '.' in their name
static Pattern PATTERN = Pattern.compile("jenkins\\.node(\\.(?<node>.*))?\\.builds");

@Override
public Collector.MetricFamilySamples.Sample createSample(String dropwizardName, String nameSuffix, List<String> additionalLabelNames, List<String> additionalLabelValues, double value) {
Matcher matcher = PATTERN.matcher(dropwizardName);

if (matcher.matches()) {
String processedDropwizardName = "jenkins.node.builds";
String node = matcher.group("node");

if (node == null) {
node = "master";
}

List<String> processedAdditionalLabelNames = new ArrayList<String>();
List<String> processedAdditionalLabelValues = new ArrayList<String>();

processedAdditionalLabelNames.add("node");
processedAdditionalLabelNames.addAll(additionalLabelNames);

processedAdditionalLabelValues.add(node);
processedAdditionalLabelValues.addAll(additionalLabelValues);

return super.createSample(processedDropwizardName, nameSuffix, processedAdditionalLabelNames, processedAdditionalLabelValues, value);
} else {
return super.createSample(dropwizardName, nameSuffix, additionalLabelNames, additionalLabelValues, value);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.jenkinsci.plugins.prometheus.util;

import io.prometheus.client.Collector;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;

public class JenkinsNodeBuildsSampleBuilderTest {
@Test
public void master_node_count_format() {
Assert.assertEquals(
new Collector.MetricFamilySamples.Sample(
"jenkins_node_builds_count",
Arrays.asList("node", "quantile"),
Arrays.asList("master", "0.5"),
0.091670452
),
new JenkinsNodeBuildsSampleBuilder().createSample(
"jenkins.node.builds",
"_count",
Arrays.asList("quantile"),
Arrays.asList("0.5"),
0.091670452
)
);
}

@Test
public void master_node_histogram_format() {
Assert.assertEquals(
new Collector.MetricFamilySamples.Sample(
"jenkins_node_builds",
Arrays.asList("node", "quantile"),
Arrays.asList("master", "0.999"),
0.091670452
),
new JenkinsNodeBuildsSampleBuilder().createSample(
"jenkins.node.builds",
"",
Arrays.asList("quantile"),
Arrays.asList("0.999"),
0.091670452
)
);
}

@Test
public void named_node_count_format() {
Assert.assertEquals(
new Collector.MetricFamilySamples.Sample(
"jenkins_node_builds_count",
Arrays.asList("node", "quantile"),
Arrays.asList("evil node_name.com", "0.5"),
0.091670452
),
new JenkinsNodeBuildsSampleBuilder().createSample(
"jenkins.node.evil node_name.com.builds",
"_count",
Arrays.asList("quantile"),
Arrays.asList("0.5"),
0.091670452
)
);
}

@Test
public void named_node_histogram_format() {
Assert.assertEquals(
new Collector.MetricFamilySamples.Sample(
"jenkins_node_builds",
Arrays.asList("node", "quantile"),
Arrays.asList("evil node_name.com", "0.999"),
0.091670452
),
new JenkinsNodeBuildsSampleBuilder().createSample(
"jenkins.node.evil node_name.com.builds",
"",
Arrays.asList("quantile"),
Arrays.asList("0.999"),
0.091670452
)
);
}
}

This file was deleted.

0 comments on commit 87c60aa

Please sign in to comment.