Skip to content

Commit

Permalink
[#635][#683] Launch collector registration asynchronously (#685)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dohbedoh authored Jul 26, 2024
1 parent e1c932a commit b59179a
Showing 1 changed file with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit b59179a

Please sign in to comment.