Skip to content

Commit

Permalink
Avoiding deadlock when jenkins updates the build iterator while acces…
Browse files Browse the repository at this point in the history
…sing the build to count the number of builds
  • Loading branch information
Waschndolos committed Dec 6, 2024
1 parent a72036c commit 6196875
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import io.prometheus.client.SimpleCollector;
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;
import org.jenkinsci.plugins.prometheus.collectors.builds.BuildsMetricCollector;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class NbBuildsGauge extends BuildsMetricCollector<Job<?, ?>, Gauge> {

private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

protected NbBuildsGauge(String[] labelNames, String namespace, String subsystem) {
super(labelNames, namespace, subsystem);
}
Expand All @@ -30,7 +32,12 @@ protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() {

@Override
public void calculateMetric(Job<?, ?> jenkinsObject, String[] labelValues) {
int nbBuilds = jenkinsObject.getBuildsAsMap().size();
this.collector.labels(labelValues).set(nbBuilds);
lock.readLock().lock();
try {
int nbBuilds = jenkinsObject.getBuildsAsMap().size();
this.collector.labels(labelValues).set(nbBuilds);
} finally {
lock.readLock().unlock();
}
}
}

0 comments on commit 6196875

Please sign in to comment.