diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfiguration.java b/src/main/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfiguration.java index 2723b55fa..a7dedc76b 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfiguration.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfiguration.java @@ -1,6 +1,7 @@ package org.jenkinsci.plugins.prometheus.config; import hudson.Extension; +import hudson.ExtensionList; import hudson.util.FormValidation; import jenkins.YesNoMaybe; import jenkins.model.GlobalConfiguration; @@ -76,7 +77,7 @@ public PrometheusConfiguration() { } public static PrometheusConfiguration get() { - return (PrometheusConfiguration) Jenkins.get().getDescriptor(PrometheusConfiguration.class); + return ExtensionList.lookupSingleton(PrometheusConfiguration.class); } @Override diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusChecker.java b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusChecker.java index 4dcd870e8..5c860f680 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusChecker.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusChecker.java @@ -18,10 +18,6 @@ public class MetricStatusChecker { public static boolean isEnabled(String metricName) { PrometheusConfiguration configuration = PrometheusConfiguration.get(); - if (configuration == null) { - LOGGER.warn("Cannot check if metric is enabled. Unable to get PrometheusConfiguration"); - return true; - } DisabledMetricConfig disabledMetricConfig = configuration.getDisabledMetricConfig(); if (disabledMetricConfig == null) { diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/init/Initializers.java b/src/main/java/org/jenkinsci/plugins/prometheus/init/Initializers.java new file mode 100644 index 000000000..6de0c8a92 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/init/Initializers.java @@ -0,0 +1,26 @@ +package org.jenkinsci.plugins.prometheus.init; + +import hudson.ExtensionList; +import hudson.init.InitMilestone; +import hudson.init.Initializer; +import org.jenkinsci.plugins.prometheus.service.PrometheusMetrics; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class Initializers { + + private static final Logger LOGGER = LoggerFactory.getLogger(Initializers.class.getName()); + + @Initializer(before = InitMilestone.COMPLETED) + public static void initializePrometheusMetrics() { + LOGGER.debug("Initializing Prometheus Plugin"); + try { + PrometheusMetrics prometheusMetrics = ExtensionList.lookupSingleton(PrometheusMetrics.class); + prometheusMetrics.initialize(); + } catch (IllegalStateException e) { + LOGGER.error("Unable to load Prometheus Plugin. Collecting Metrics won't work", e); + } + } + +} 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 e1f53fb23..12525cd70 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/service/DefaultPrometheusMetrics.java @@ -2,8 +2,6 @@ import hudson.Extension; 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; @@ -30,34 +28,13 @@ public class DefaultPrometheusMetrics implements PrometheusMetrics { private volatile boolean initialized = false; private volatile boolean initializing = false; - private final CollectorRegistry collectorRegistry; + private CollectorRegistry collectorRegistry; private final AtomicReference cachedMetrics; public DefaultPrometheusMetrics() { - this.collectorRegistry = CollectorRegistry.defaultRegistry; this.cachedMetrics = new AtomicReference<>(""); } - @Initializer(after = InitMilestone.JOB_LOADED) - public void registerCollectors() { - 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; - } - } - @Override public String getMetrics() { return cachedMetrics.get(); @@ -76,4 +53,25 @@ public void collectMetrics() { LOGGER.debug("Unable to collect metrics"); } } + + @Override + public void initialize() { + if (!initialized && !initializing) { + this.collectorRegistry = CollectorRegistry.defaultRegistry; + 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; + } + } } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/service/PrometheusMetrics.java b/src/main/java/org/jenkinsci/plugins/prometheus/service/PrometheusMetrics.java index 9dadc68e8..8de9af70f 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/service/PrometheusMetrics.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/service/PrometheusMetrics.java @@ -8,4 +8,6 @@ public interface PrometheusMetrics extends ExtensionPoint { void collectMetrics(); + void initialize(); + } diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusCheckerTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusCheckerTest.java index bc10896bb..0a5bd1cd6 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusCheckerTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/MetricStatusCheckerTest.java @@ -15,26 +15,6 @@ public class MetricStatusCheckerTest { - @Test - void testNoConfigWontFail() { - Jenkins jenkins = mock(Jenkins.class); - - PrometheusConfiguration mockedConfig = mock(PrometheusConfiguration.class); - String namespace = "TestNamespace"; - - when(mockedConfig.getDefaultNamespace()).thenReturn(namespace); - when(mockedConfig.isCollectNodeStatus()).thenReturn(false); - - try (MockedStatic jenkinsStatic = mockStatic(Jenkins.class); - MockedStatic configStatic = mockStatic(PrometheusConfiguration.class)) { - jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); - configStatic.when(PrometheusConfiguration::get).thenReturn(null); - - boolean enabled = MetricStatusChecker.isEnabled("some_metric"); - Assertions.assertTrue(enabled); - } - } - @Test void testNoDisabledMetricConfigWontFail() { Jenkins jenkins = mock(Jenkins.class); diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java index b750b6739..397ba852b 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java @@ -34,18 +34,22 @@ public class PrometheusActionTest { @Mock private PrometheusConfiguration configuration; + private MockedStatic extensionListStatic; private MockedStatic jenkinsStatic; @BeforeEach public void setUp() { jenkinsStatic = mockStatic(Jenkins.class); jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); - when(jenkins.getDescriptor(PrometheusConfiguration.class)).thenReturn(configuration); + when(configuration.getAdditionalPath()).thenReturn("prometheus"); + extensionListStatic = mockStatic(ExtensionList.class); + extensionListStatic.when(() -> ExtensionList.lookupSingleton(PrometheusConfiguration.class)).thenReturn(configuration); } @AfterEach public void tearDown() { + extensionListStatic.close(); jenkinsStatic.close(); } @@ -92,23 +96,23 @@ public void shouldReturnMetrics() throws IOException, ServletException { PrometheusMetrics prometheusMetrics = mock(PrometheusMetrics.class); String responseBody = "testMetric"; when(prometheusMetrics.getMetrics()).thenReturn(responseBody); - try (MockedStatic extensionListMockedStatic = mockStatic(ExtensionList.class)) { - extensionListMockedStatic.when(() -> ExtensionList.lookupSingleton(PrometheusMetrics.class)).thenReturn(prometheusMetrics); - StaplerRequest request = mock(StaplerRequest.class); - String url = "prometheus"; - when(request.getRestOfPath()).thenReturn(url); - - // when - HttpResponse actual = action.doDynamic(request); - - // then - AssertStaplerResponse.from(actual) - .call() - .assertHttpStatus(HTTP_OK) - .assertContentType(TextFormat.CONTENT_TYPE_004) - .assertHttpHeader("Cache-Control", "must-revalidate,no-cache,no-store") - .assertBody(responseBody); - } + + extensionListStatic.when(() -> ExtensionList.lookupSingleton(PrometheusMetrics.class)).thenReturn(prometheusMetrics); + StaplerRequest request = mock(StaplerRequest.class); + String url = "prometheus"; + when(request.getRestOfPath()).thenReturn(url); + + // when + HttpResponse actual = action.doDynamic(request); + + // then + AssertStaplerResponse.from(actual) + .call() + .assertHttpStatus(HTTP_OK) + .assertContentType(TextFormat.CONTENT_TYPE_004) + .assertHttpHeader("Cache-Control", "must-revalidate,no-cache,no-store") + .assertBody(responseBody); + } diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/service/PrometheusAsyncWorkerTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/service/PrometheusAsyncWorkerTest.java index 2d6e9116e..d096e6b4d 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/service/PrometheusAsyncWorkerTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/service/PrometheusAsyncWorkerTest.java @@ -67,5 +67,9 @@ public void collectMetrics() { cachedMetrics.set(metrics); } + @Override + public void initialize() { + + } } }