forked from lsjostro/prometheus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #397 from paychex/jenkins-node-build-type-fix
Fix duplicate TYPE lines for jenkins_node_builds metric
- Loading branch information
Showing
5 changed files
with
133 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
src/main/java/org/jenkinsci/plugins/prometheus/util/MetricsFormatter.java
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
src/test/java/org/jenkinsci/plugins/prometheus/util/JenkinsNodeBuildsSampleBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
); | ||
} | ||
} |
50 changes: 0 additions & 50 deletions
50
src/test/java/org/jenkinsci/plugins/prometheus/util/MetricsFormatterTest.java
This file was deleted.
Oops, something went wrong.