diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/jobs/NbBuildsGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/jobs/NbBuildsGauge.java index fcfae1c4f..29af287e2 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/jobs/NbBuildsGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/jobs/NbBuildsGauge.java @@ -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, Gauge> { + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + protected NbBuildsGauge(String[] labelNames, String namespace, String subsystem) { super(labelNames, namespace, subsystem); } @@ -30,7 +32,12 @@ protected SimpleCollector.Builder 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(); + } } }