Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#635][#683] Fixing startup problems in certain Jenkins installations #685

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}

@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
Loading