Skip to content

Commit

Permalink
Revert "Fix initilization potential deadlock (#637)"
Browse files Browse the repository at this point in the history
This reverts commit d79999a.
  • Loading branch information
waschndolos committed Mar 28, 2024
1 parent 0943cc1 commit a24a8d1
Showing 1 changed file with 19 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,62 +1,45 @@
package org.jenkinsci.plugins.prometheus.service;

import hudson.ExtensionList;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
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 java.io.IOException;
import java.io.StringWriter;
import java.util.concurrent.atomic.AtomicReference;
import jenkins.metrics.api.Metrics;
import org.jenkinsci.plugins.prometheus.CodeCoverageCollector;
import org.jenkinsci.plugins.prometheus.DiskUsageCollector;
import org.jenkinsci.plugins.prometheus.ExecutorCollector;
import org.jenkinsci.plugins.prometheus.JenkinsStatusCollector;
import org.jenkinsci.plugins.prometheus.JobCollector;
import org.jenkinsci.plugins.prometheus.*;
import org.jenkinsci.plugins.prometheus.config.disabledmetrics.FilteredMetricEnumeration;
import org.jenkinsci.plugins.prometheus.util.JenkinsNodeBuildsSampleBuilder;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Restricted(NoExternalUse.class)
import java.io.IOException;
import java.io.StringWriter;
import java.util.concurrent.atomic.AtomicReference;

public class DefaultPrometheusMetrics implements PrometheusMetrics {

private static final Logger logger = LoggerFactory.getLogger(DefaultPrometheusMetrics.class);

private volatile boolean initialized = false;
private volatile boolean initializing = false;
private final CollectorRegistry collectorRegistry;
private final AtomicReference<String> cachedMetrics;

public DefaultPrometheusMetrics() {
this.collectorRegistry = CollectorRegistry.defaultRegistry;
this.cachedMetrics = new AtomicReference<>("");
}
CollectorRegistry collectorRegistry = CollectorRegistry.defaultRegistry;
collectorRegistry.register(new JobCollector());
collectorRegistry.register(new JenkinsStatusCollector());
collectorRegistry.register(new DropwizardExports(Metrics.metricRegistry(), new JenkinsNodeBuildsSampleBuilder()));
collectorRegistry.register(new DiskUsageCollector());
collectorRegistry.register(new ExecutorCollector());
collectorRegistry.register(new CodeCoverageCollector());

@Initializer(after = InitMilestone.JOB_LOADED)
public void init() {
if (!initialized && !initializing) {
initializing = true;
logger.debug("Initializing...");
collectorRegistry.register(new JobCollector());
collectorRegistry.register(new JenkinsStatusCollector());
collectorRegistry.register(
new DropwizardExports(Metrics.metricRegistry(), new JenkinsNodeBuildsSampleBuilder()));
collectorRegistry.register(new DiskUsageCollector());
collectorRegistry.register(new ExecutorCollector());
collectorRegistry.register(new CodeCoverageCollector());
// other collectors from other plugins
ExtensionList.lookup(Collector.class).forEach(collectorRegistry::register);
DefaultExports.initialize();
initialized = true;
initializing = false;
}
// other collectors from other plugins
ExtensionList.lookup(Collector.class).forEach(collectorRegistry::register);

DefaultExports.initialize();

this.collectorRegistry = collectorRegistry;
this.cachedMetrics = new AtomicReference<>("");
}

@Override
Expand All @@ -66,10 +49,6 @@ public String getMetrics() {

@Override
public void collectMetrics() {
if(!initialized) {
logger.debug("Not initialized");
return;
}
try (StringWriter buffer = new StringWriter()) {
TextFormat.write004(buffer, new FilteredMetricEnumeration(collectorRegistry.metricFamilySamples().asIterator()));
cachedMetrics.set(buffer.toString());
Expand Down

0 comments on commit a24a8d1

Please sign in to comment.