Skip to content

Commit

Permalink
[#635][#683] Launch collector registration asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
Dohbedoh committed Jul 26, 2024
1 parent e1c932a commit c06615c
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 {

Check warning

Code scanning / Jenkins Security Scan

Stapler: Missing POST/RequirePOST annotation Warning

Potential CSRF vulnerability: If #doRun connects to user-specified URLs, modifies state, or is expensive to run, it should be annotated with @POST or @RequirePOST

Check warning

Code scanning / Jenkins Security Scan

Stapler: Missing permission check Warning

Potential missing permission check in #doRun
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 c06615c

Please sign in to comment.