-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Jenkins health metrics * Add Jenkins health metrics * Try fix the build * Improve code readability * Improve code * Improve code * revert CI config * Fix bug * add metric jenkins.plugins.updates * Better code docs --------- Co-authored-by: Ivan Fernandez Calvo <[email protected]>
- Loading branch information
1 parent
e1da710
commit a85780e
Showing
7 changed files
with
500 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/io/jenkins/plugins/opentelemetry/init/PluginMonitoringInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright The Original Author or Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.jenkins.plugins.opentelemetry.init; | ||
|
||
import hudson.Extension; | ||
import hudson.PluginManager; | ||
import io.jenkins.plugins.opentelemetry.JenkinsControllerOpenTelemetry; | ||
import io.jenkins.plugins.opentelemetry.api.OpenTelemetryLifecycleListener; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import io.opentelemetry.api.metrics.ObservableLongMeasurement; | ||
import jenkins.YesNoMaybe; | ||
import jenkins.model.Jenkins; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.inject.Inject; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import static io.jenkins.plugins.opentelemetry.semconv.JenkinsOtelSemanticAttributes.STATUS; | ||
import static io.jenkins.plugins.opentelemetry.semconv.JenkinsSemanticMetrics.JENKINS_PLUGINS; | ||
import static io.jenkins.plugins.opentelemetry.semconv.JenkinsSemanticMetrics.JENKINS_PLUGINS_UPDATES; | ||
|
||
/** | ||
* <p> | ||
* Monitor the Jenkins plugins | ||
* </p> | ||
* <p> | ||
* TODO report on `hasUpdate` plugin count. | ||
* </p> | ||
*/ | ||
@Extension(dynamicLoadable = YesNoMaybe.MAYBE, optional = true) | ||
public class PluginMonitoringInitializer implements OpenTelemetryLifecycleListener { | ||
|
||
private static final Logger logger = Logger.getLogger(PluginMonitoringInitializer.class.getName()); | ||
|
||
@Inject | ||
JenkinsControllerOpenTelemetry jenkinsControllerOpenTelemetry; | ||
|
||
@PostConstruct | ||
public void postConstruct() { | ||
|
||
logger.log(Level.FINE, () -> "Start monitoring Jenkins plugins..."); | ||
|
||
Meter meter = Objects.requireNonNull(jenkinsControllerOpenTelemetry).getDefaultMeter(); | ||
|
||
final ObservableLongMeasurement plugins = meter | ||
.gaugeBuilder(JENKINS_PLUGINS) | ||
.setUnit("${plugins}") | ||
.setDescription("Jenkins plugins") | ||
.ofLongs() | ||
.buildObserver(); | ||
final ObservableLongMeasurement pluginUpdates = meter | ||
.gaugeBuilder(JENKINS_PLUGINS_UPDATES) | ||
.setUnit("${plugins}") | ||
.setDescription("Jenkins plugin updates") | ||
.ofLongs() | ||
.buildObserver(); | ||
meter.batchCallback(() -> { | ||
logger.log(Level.FINE, () -> "Recording Jenkins controller executor pool metrics..."); | ||
|
||
AtomicInteger active = new AtomicInteger(); | ||
AtomicInteger inactive = new AtomicInteger(); | ||
AtomicInteger hasUpdate = new AtomicInteger(); | ||
AtomicInteger isUpToDate = new AtomicInteger(); | ||
|
||
PluginManager pluginManager = Jenkins.get().getPluginManager(); | ||
pluginManager.getPlugins().forEach(plugin -> { | ||
if (plugin.isActive()) { | ||
active.incrementAndGet(); | ||
} else { | ||
inactive.incrementAndGet(); | ||
} | ||
if (plugin.hasUpdate()) { | ||
hasUpdate.incrementAndGet(); | ||
} else { | ||
isUpToDate.incrementAndGet(); | ||
} | ||
}); | ||
int failed = pluginManager.getFailedPlugins().size(); | ||
plugins.record(active.get(), Attributes.of(STATUS, "active")); | ||
plugins.record(inactive.get(), Attributes.of(STATUS, "inactive")); | ||
plugins.record(failed, Attributes.of(STATUS, "failed")); | ||
pluginUpdates.record(hasUpdate.get(), Attributes.of(STATUS, "hasUpdate")); | ||
pluginUpdates.record(isUpToDate.get(), Attributes.of(STATUS, "isUpToDate")); | ||
}, plugins, pluginUpdates); | ||
} | ||
} |
Oops, something went wrong.