From b59179a2764368d0c12552cc7dacecaff3a55db2 Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Fri, 26 Jul 2024 17:28:47 +1000 Subject: [PATCH] [#635][#683] Launch collector registration asynchronously (#685) --- .../service/DefaultPrometheusMetrics.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java b/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java index 1cf59d43a..a7941ea84 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java @@ -4,12 +4,14 @@ import hudson.ExtensionList; import hudson.init.InitMilestone; import hudson.init.Initializer; +import hudson.triggers.SafeTimerTask; import io.prometheus.client.Collector; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.dropwizard.DropwizardExports; import io.prometheus.client.exporter.common.TextFormat; import io.prometheus.client.hotspot.DefaultExports; import jenkins.metrics.api.Metrics; +import jenkins.util.Timer; import org.jenkinsci.plugins.prometheus.CodeCoverageCollector; import org.jenkinsci.plugins.prometheus.DiskUsageCollector; import org.jenkinsci.plugins.prometheus.ExecutorCollector; @@ -24,6 +26,7 @@ import java.io.IOException; import java.io.StringWriter; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; public class DefaultPrometheusMetrics implements PrometheusMetrics { @@ -50,29 +53,41 @@ public static synchronized DefaultPrometheusMetrics get() { } @Restricted(NoExternalUse.class) - private void registerCollector(@NonNull Collector collector) { - collectorRegistry.register(collector); - logger.debug(String.format("Collector %s registered", collector.getClass().getName())); + private void initRegistry() { + this.collectorRegistry.clear(); + DefaultExports.register(this.collectorRegistry); } @Restricted(NoExternalUse.class) - @Initializer(after = InitMilestone.EXTENSIONS_AUGMENTED, before = InitMilestone.JOB_LOADED) - public static void registerCollectors() { - DefaultPrometheusMetrics instance = get(); - instance.registerCollector(new JenkinsStatusCollector()); - instance.registerCollector(new DropwizardExports(Metrics.metricRegistry(), new JenkinsNodeBuildsSampleBuilder())); - instance.registerCollector(new DiskUsageCollector()); - instance.registerCollector(new ExecutorCollector()); + private void registerCollector(@NonNull Collector collector) { + collectorRegistry.register(collector); + logger.debug(String.format("Collector %s registered", collector.getClass().getName())); } @Restricted(NoExternalUse.class) @Initializer(after = InitMilestone.JOB_LOADED, before = InitMilestone.JOB_CONFIG_ADAPTED) - public static void registerJobCollectors() { - DefaultPrometheusMetrics instance = get(); - instance.registerCollector(new JobCollector()); - instance.registerCollector(new CodeCoverageCollector()); - // other collectors from other plugins - ExtensionList.lookup(Collector.class).forEach(instance::registerCollector); + public static void registerCollectors() { + Timer.get() + .schedule( + new SafeTimerTask() { + @Override + public void doRun() throws Exception { + logger.debug("Initializing Collectors"); + DefaultPrometheusMetrics instance = get(); + instance.initRegistry(); + instance.registerCollector(new JenkinsStatusCollector()); + instance.registerCollector(new DropwizardExports(Metrics.metricRegistry(), new JenkinsNodeBuildsSampleBuilder())); + instance.registerCollector(new DiskUsageCollector()); + instance.registerCollector(new ExecutorCollector()); + instance.registerCollector(new JobCollector()); + instance.registerCollector(new CodeCoverageCollector()); + // other collectors from other plugins + ExtensionList.lookup(Collector.class).forEach(instance::registerCollector); + logger.debug("Finished initializing Collectors"); + } + }, + 1, + TimeUnit.SECONDS); } @Override