Skip to content

Commit

Permalink
Fixing duplicate entry when using "Collect metrics for each run" for …
Browse files Browse the repository at this point in the history
…running_build_duration_milliseconds (#513)
  • Loading branch information
Waschndolos authored Apr 11, 2023
1 parent 6a9c81d commit 00c6fc1
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class JobCollector extends Collector {
private NbBuildsGauge nbBuildsGauge;

private BuildDiscardGauge buildDiscardGauge;

private CurrentRunDurationGauge currentRunDurationGauge;

private static class BuildMetrics {

Expand All @@ -58,8 +58,6 @@ private static class BuildMetrics {
public Gauge jobBuildTestsSkipped;
public Gauge jobBuildTestsFailing;

public CurrentRunDurationGauge currentRunDurationGauge;

private String buildPrefix;

public BuildMetrics(String buildPrefix) {
Expand Down Expand Up @@ -122,7 +120,7 @@ public void initCollectors(String fullname, String subsystem, String namespace,
.help("Summary of Jenkins build times by Job and Stage in the last build")
.create();

this.currentRunDurationGauge = new CurrentRunDurationGauge(labelNameArray, namespace, subsystem);

}
}

Expand Down Expand Up @@ -207,6 +205,8 @@ public List<MetricFamilySamples> collect() {

buildDiscardGauge = new BuildDiscardGauge(labelBaseNameArray, namespace, subsystem);

currentRunDurationGauge = new CurrentRunDurationGauge(labelBaseNameArray, namespace, subsystem);

if (PrometheusConfiguration.get().isPerBuildMetrics()) {
labelNameArray = Arrays.copyOf(labelNameArray, labelNameArray.length + 1);
labelNameArray[labelNameArray.length - 1] = "number";
Expand Down Expand Up @@ -241,7 +241,7 @@ public List<MetricFamilySamples> collect() {
addSamples(samples, jobHealthScoreGauge.collect(), "Adding [{}] samples from gauge ({})");
addSamples(samples, nbBuildsGauge.collect(), "Adding [{}] samples from gauge ({})");
addSamples(samples, buildDiscardGauge.collect(), "Adding [{}] samples from gauge ({})");

addSamples(samples, currentRunDurationGauge.collect(), "Adding [{}] samples from gauge ({})");
addSamples(samples, lastBuildMetrics);
if (PrometheusConfiguration.get().isPerBuildMetrics()) {
addSamples(samples, perBuildMetrics);
Expand All @@ -264,7 +264,6 @@ private void addSamples(List<MetricFamilySamples> allSamples, BuildMetrics build
addSamples(allSamples, buildMetrics.jobBuildResultOrdinal.collect(), "Adding [{}] samples from gauge ({})");
addSamples(allSamples, buildMetrics.jobBuildResult.collect(), "Adding [{}] samples from gauge ({})");
addSamples(allSamples, buildMetrics.jobBuildDuration.collect(), "Adding [{}] samples from gauge ({})");
addSamples(allSamples, buildMetrics.currentRunDurationGauge.collect(), "Adding [{}] samples from gauge ({})");
addSamples(allSamples, buildMetrics.jobBuildStartMillis.collect(), "Adding [{}] samples from gauge ({})");
addSamples(allSamples, buildMetrics.jobBuildTestsTotal.collect(), "Adding [{}] samples from gauge ({})");
addSamples(allSamples, buildMetrics.jobBuildTestsSkipped.collect(), "Adding [{}] samples from gauge ({})");
Expand Down Expand Up @@ -295,7 +294,7 @@ protected void appendJobMetrics(Job job) {
nbBuildsGauge.calculateMetric(job, baseLabelValueArray);
jobHealthScoreGauge.calculateMetric(job, baseLabelValueArray);
buildDiscardGauge.calculateMetric(job, baseLabelValueArray);

currentRunDurationGauge.calculateMetric(job, baseLabelValueArray);
processRun(job, lastBuild, baseLabelValueArray, lastBuildMetrics);

Run run = lastBuild;
Expand Down Expand Up @@ -381,7 +380,7 @@ private void processRun(Job job, Run run, String[] buildLabelValueArray, BuildMe

buildMetrics.jobBuildStartMillis.labels(buildLabelValueArray).set(millis);

buildMetrics.currentRunDurationGauge.calculateMetric(job, buildLabelValueArray);

if (!run.isBuilding()) {
buildMetrics.jobBuildDuration.labels(buildLabelValueArray).set(duration);
processRunTestsResults(run, buildLabelValueArray, buildMetrics);
Expand Down

0 comments on commit 00c6fc1

Please sign in to comment.