Skip to content

Commit

Permalink
When a build is building while the prometheus calculating cycle runs …
Browse files Browse the repository at this point in the history
…it will use the previous build for calculation to ensure continuous results
  • Loading branch information
Waschndolos committed Oct 18, 2024
1 parent 3d25609 commit 3e8f195
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check warning on line 254 in src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 227-254 are not covered by tests
while (run != null) {
LOGGER.debug("getting metrics for run [{}] from job [{}], include per run metrics [{}]", run.getNumber(), job.getName(), isPerBuildMetrics);
if (Runs.includeBuildInMetrics(run)) {
Expand All @@ -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);
Expand Down

0 comments on commit 3e8f195

Please sign in to comment.