Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate last build if job is currently building #709

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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
Loading