From 48885590b8d95fe81f21698fd1af3dbd9382e56c Mon Sep 17 00:00:00 2001 From: Waschndolos Date: Fri, 1 Nov 2024 07:21:07 +0100 Subject: [PATCH] When a build is building while the prometheus calculating cycle runs it will use the previous build for calculation to ensure continuous results (#709) Co-authored-by: Waschndolos --- .../plugins/prometheus/JobCollector.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java b/src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java index 632233c3e..6badf06e1 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java @@ -224,22 +224,34 @@ protected void appendJobMetrics(Job job) { boolean isPerBuildMetrics = PrometheusConfiguration.get().isPerBuildMetrics(); String[] baseLabelValueArray = JobLabel.getBaseLabelValues(job); - Run lastBuild = job.getLastBuild(); + Run buildToCheck = job.getLastBuild(); + // Never built - if (null == lastBuild) { + if (null == buildToCheck) { LOGGER.debug("job [{}] never built", job.getFullName()); return; } + if (buildToCheck.isBuilding()) { + LOGGER.debug("Build [{}] is currently building. Will calculate previous build.", buildToCheck.number); + buildToCheck = buildToCheck.getPreviousBuild(); + if (buildToCheck == null) { + LOGGER.debug("Previous build does not exist. Skipping calculation for [{}].", job.getFullName()); + return; + } + } + + LOGGER.debug("Calculating job metrics for [{}]", buildToCheck.number); + nbBuildsGauge.calculateMetric(job, baseLabelValueArray); jobHealthScoreGauge.calculateMetric(job, baseLabelValueArray); buildDiscardGauge.calculateMetric(job, baseLabelValueArray); currentRunDurationGauge.calculateMetric(job, baseLabelValueArray); logUpdatedGauge.calculateMetric(job, baseLabelValueArray); - processRun(job, lastBuild, baseLabelValueArray, lastBuildMetrics); + processRun(job, buildToCheck, baseLabelValueArray, lastBuildMetrics); - Run run = lastBuild; + Run run = buildToCheck; while (run != null) { LOGGER.debug("getting metrics for run [{}] from job [{}], include per run metrics [{}]", run.getNumber(), job.getName(), isPerBuildMetrics); if (Runs.includeBuildInMetrics(run)) { @@ -265,7 +277,7 @@ private void processRun(Job job, Run run, String[] buildLabelValueAr buildMetrics.jobBuildResult.calculateMetric(run, buildLabelValueArray); buildMetrics.jobBuildStartMillis.calculateMetric(run, buildLabelValueArray); buildMetrics.jobBuildDuration.calculateMetric(run, buildLabelValueArray); - // Label values are calculated within stageSummary so we pass null here. + // Label values are calculated within stageSummary, so we pass null here. buildMetrics.stageSummary.calculateMetric(run, buildLabelValueArray); buildMetrics.stageBuildResultOrdinal.calculateMetric(run, buildLabelValueArray); buildMetrics.jobBuildTestsTotal.calculateMetric(run, buildLabelValueArray);