Repository: jenkinsci/prometheus-plugin (java)

Issue:
Revert for release
### Jenkins and plugins versions report

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.3.2:jar (attach-javadocs) on project prometheus: MavenReportException: Error while generating Javadoc: 
    [ERROR] Exit code: 1 - /Users/markyjackson/go/src/github.com/markyjackson-taulia/prometheus-plugin/target/checkout/src/main/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilder.java:12: error: unknown tag: node_name
    [ERROR]  * 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)
    [ERROR]                                        ^
    [ERROR] /Users/markyjackson/go/src/github.com/markyjackson-taulia/prometheus-plugin/target/checkout/src/main/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilder.java:12: error: unknown tag: node_name
    [ERROR]  * 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)
    [ERROR]                                                                                                        ^
    [ERROR] 

### What Operating System are you using (both controller, and any agents involved in the problem)?

N/A

### Reproduction steps

N/A

### Expected Results

N/A

### Actual Results

N/A

### Anything else?

_No response_

Pull request description:
Revert "Fix duplicate TYPE lines for jenkins_node_builds metric"
Reverts jenkinsci/prometheus-plugin#397

Fixes #399 

This need more looking at:

```
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.3.2:jar (attach-javadocs) on project prometheus: MavenReportException: Error while generating Javadoc: 
    [ERROR] Exit code: 1 - /Users/markyjackson/go/src/github.com/markyjackson-taulia/prometheus-plugin/target/checkout/src/main/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilder.java:12: error: unknown tag: node_name
    [ERROR]  * 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)
    [ERROR]                                        ^
    [ERROR] /Users/markyjackson/go/src/github.com/markyjackson-taulia/prometheus-plugin/target/checkout/src/main/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilder.java:12: error: unknown tag: node_name
    [ERROR]  * 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)
    [ERROR]                                                                                                        ^
    [ERROR] 
    ```
    

Patch:
diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java b/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java
index f4840b4..e0b6f34 100644
--- a/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java
+++ b/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java
@@ -11,7 +11,7 @@ import org.jenkinsci.plugins.prometheus.DiskUsageCollector;
 import org.jenkinsci.plugins.prometheus.ExecutorCollector;
 import org.jenkinsci.plugins.prometheus.JenkinsStatusCollector;
 import org.jenkinsci.plugins.prometheus.JobCollector;
-import org.jenkinsci.plugins.prometheus.util.JenkinsNodeBuildsSampleBuilder;
+import org.jenkinsci.plugins.prometheus.util.MetricsFormatter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -30,7 +30,7 @@ public class DefaultPrometheusMetrics implements PrometheusMetrics {
         CollectorRegistry collectorRegistry = CollectorRegistry.defaultRegistry;
         collectorRegistry.register(new JobCollector());
         collectorRegistry.register(new JenkinsStatusCollector());
-        collectorRegistry.register(new DropwizardExports(Metrics.metricRegistry(), new JenkinsNodeBuildsSampleBuilder()));
+        collectorRegistry.register(new DropwizardExports(Metrics.metricRegistry()));
         collectorRegistry.register(new DiskUsageCollector());
         collectorRegistry.register(new ExecutorCollector());
 
@@ -52,7 +52,7 @@ public class DefaultPrometheusMetrics implements PrometheusMetrics {
     public void collectMetrics() {
         try (StringWriter buffer = new StringWriter()) {
             TextFormat.write004(buffer, collectorRegistry.metricFamilySamples());
-            cachedMetrics.set(buffer.toString());
+            cachedMetrics.set(MetricsFormatter.formatMetrics(buffer.toString()));
         } catch (IOException e) {
             logger.debug("Unable to collect metrics");
         }
diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilder.java b/src/main/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilder.java
deleted file mode 100644
index 43b905e..0000000
--- a/src/main/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilder.java
+++ /dev/null
@@ -1,45 +0,0 @@
-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);
-        }
-    }
-}
diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/util/MetricsFormatter.java b/src/main/java/org/jenkinsci/plugins/prometheus/util/MetricsFormatter.java
new file mode 100644
index 0000000..5ed0f12
--- /dev/null
+++ b/src/main/java/org/jenkinsci/plugins/prometheus/util/MetricsFormatter.java
@@ -0,0 +1,16 @@
+package org.jenkinsci.plugins.prometheus.util;
+
+public class MetricsFormatter {
+
+    public static String formatMetrics(String formatString) {
+        //node specific build counts
+        formatString = formatString.replaceAll("jenkins_node_builds_count (.*)", "jenkins_node_builds_count{node=\"master\"} $1");
+        formatString = formatString.replaceAll("jenkins_node_(.*)_builds_count (.*)", "jenkins_node_builds_count{node=\"$1\"} $2");
+
+        //node specific histograms
+        formatString = formatString.replaceAll("jenkins_node_builds\\{(.*)} (.*)", "jenkins_node_builds{node=\"master\", $1} $2");
+        formatString = formatString.replaceAll("jenkins_node_(.*)_builds\\{(.*)} (.*)", "jenkins_node_builds{node=\"$1\", $2} $3");
+
+        return formatString;
+    }
+}


Test patch:
diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilderTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilderTest.java
deleted file mode 100644
index 5301f21..0000000
--- a/src/test/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilderTest.java
+++ /dev/null
@@ -1,85 +0,0 @@
-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
-                )
-        );
-    }
-}
diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/util/MetricsFormatterTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/util/MetricsFormatterTest.java
new file mode 100644
index 0000000..86f3d9a
--- /dev/null
+++ b/src/test/java/org/jenkinsci/plugins/prometheus/util/MetricsFormatterTest.java
@@ -0,0 +1,50 @@
+package org.jenkinsci.plugins.prometheus.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MetricsFormatterTest {
+
+    private final String inString = "jenkins_node_builds{quantile=\"0.5\",} 0.091670452\n" +
+            "jenkins_node_builds{quantile=\"0.75\",} 0.091670452\n" +
+            "jenkins_node_builds{quantile=\"0.95\",} 0.091670452\n" +
+            "jenkins_node_builds{quantile=\"0.98\",} 0.091670452\n" +
+            "jenkins_node_builds{quantile=\"0.99\",} 0.091670452\n" +
+            "jenkins_node_builds{quantile=\"0.999\",} 0.091670452\n" +
+            "jenkins_node_my_node_1_builds{quantile=\"0.99\",} 0.091670452\n" +
+            "jenkins_node_my_node_4_builds{quantile=\"0.999\",} 0.091670452\n" +
+            "jenkins_node_builds_count 28458.0\n" +
+            "jenkins_node_my_node_1_builds_count 12345";
+
+    @Test
+    public void master_node_count_format() {
+        String formatString = MetricsFormatter.formatMetrics(inString);
+        //master node count
+        Assert.assertFalse(formatString.contains("jenkins_node_builds_count 28458.0\n"));
+        Assert.assertTrue(formatString.contains("jenkins_node_builds_count{node=\"master\"} 28458.0\n"));
+    }
+
+    @Test
+    public void master_node_histogram_format() {
+        String formatString = MetricsFormatter.formatMetrics(inString);
+        //master node histogram
+        Assert.assertFalse(formatString.contains("jenkins_node_builds{quantile=\"0.999\",} 0.091670452\n"));
+        Assert.assertTrue(formatString.contains("jenkins_node_builds{node=\"master\", quantile=\"0.999\",} 0.091670452\n"));
+    }
+
+    @Test
+    public void named_node_count_format() {
+        String formatString = MetricsFormatter.formatMetrics(inString);
+        //named node count
+        Assert.assertFalse(formatString.contains("jenkins_node_my_node_1_builds_count"));
+        Assert.assertTrue(formatString.contains("jenkins_node_builds_count{node=\"my_node_1\"} 12345"));
+    }
+
+    @Test
+    public void named_node_histogram_format() {
+        String formatString = MetricsFormatter.formatMetrics(inString);
+        //named node histogram
+        Assert.assertFalse(formatString.contains("jenkins_node_my_node_4_builds{quantile=\"0.999\",} 0.091670452\n"));
+        Assert.assertTrue(formatString.contains("jenkins_node_builds{node=\"my_node_4\", quantile=\"0.999\",} 0.091670452\n"));
+    }
+}