From 5905c4f2ba026302de91ce629d72c74cd05a8051 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Fri, 14 Feb 2014 16:57:47 +0000 Subject: [PATCH] Switch to codahale-metrics --- pom.xml | 43 +---- .../jenkins/support/SupportContextImpl.java | 22 ++- .../jenkins/support/SupportPlugin.java | 155 +----------------- .../jenkins/support/api/SupportContext.java | 8 + .../jenkins/support/impl/Metrics.java | 10 +- 5 files changed, 33 insertions(+), 205 deletions(-) diff --git a/pom.xml b/pom.xml index d7530fbd0..fd581f76b 100644 --- a/pom.xml +++ b/pom.xml @@ -68,7 +68,6 @@ - 3.0.1 UTF-8 UTF-8 UTF-8 @@ -107,42 +106,7 @@ - com.codahale.metrics - metrics-core - ${metrics.version} - - - com.codahale.metrics - metrics-servlet - ${metrics.version} - - - com.codahale.metrics - metrics-healthchecks - ${metrics.version} - - - com.codahale.metrics - metrics-jvm - ${metrics.version} - - - com.codahale.metrics - metrics-json - ${metrics.version} - - - com.fasterxml.jackson.core - jackson-core - 2.2.2 - - - com.fasterxml.jackson.core - jackson-databind - 2.2.2 - - - net.sf.uadetector + net.sf.uadetector uadetector-resources 2013.09 @@ -152,6 +116,11 @@ 1.9 + + org.jenkins-ci.plugins + codahale-metrics + 3.0.1-1-SNAPSHOT + diff --git a/src/main/java/com/cloudbees/jenkins/support/SupportContextImpl.java b/src/main/java/com/cloudbees/jenkins/support/SupportContextImpl.java index f31cf2801..d8a0866c2 100644 --- a/src/main/java/com/cloudbees/jenkins/support/SupportContextImpl.java +++ b/src/main/java/com/cloudbees/jenkins/support/SupportContextImpl.java @@ -27,6 +27,7 @@ import com.cloudbees.jenkins.support.api.SupportContext; import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.health.HealthCheckRegistry; +import com.codahale.metrics.jenkins.Metrics; /** * The implementation of {@link SupportContext} @@ -35,24 +36,27 @@ */ public class SupportContextImpl implements SupportContext { - private final MetricRegistry metricsRegistry; - - private final HealthCheckRegistry healthCheckRegistry; - public SupportContextImpl() { - this.metricsRegistry = new MetricRegistry(); - this.healthCheckRegistry = new HealthCheckRegistry(); } + /** + * {@inheritDoc} + * @deprecated + */ + @Deprecated public MetricRegistry getMetricsRegistry() { - return metricsRegistry; + return Metrics.metricRegistry(); } + /** + * {@inheritDoc} + * @deprecated + */ + @Deprecated public HealthCheckRegistry getHealthCheckRegistry() { - return healthCheckRegistry; + return Metrics.healthCheckRegistry(); } public void shutdown() { - // TODO find equivalent of metricsRegistry.shutdown(); } } diff --git a/src/main/java/com/cloudbees/jenkins/support/SupportPlugin.java b/src/main/java/com/cloudbees/jenkins/support/SupportPlugin.java index 71b6bd736..ca95fc3f0 100644 --- a/src/main/java/com/cloudbees/jenkins/support/SupportPlugin.java +++ b/src/main/java/com/cloudbees/jenkins/support/SupportPlugin.java @@ -33,6 +33,7 @@ import com.codahale.metrics.Histogram; import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.Timer; +import com.codahale.metrics.jenkins.impl.JenkinsMetricProviderImpl; import com.codahale.metrics.jvm.BufferPoolMetricSet; import com.codahale.metrics.jvm.FileDescriptorRatioGauge; import com.codahale.metrics.jvm.GarbageCollectorMetricSet; @@ -126,14 +127,6 @@ public class SupportPlugin extends Plugin { private transient final SupportLogHandler handler = new SupportLogHandler(256, 2048, 8); private transient SupportContextImpl context = null; - private transient DefaultSupportMetricsFilter filter; - private transient Histogram jenkinsQueueLength; - private transient Histogram jenkinsNodeTotalCount; - private transient Histogram jenkinsNodeOnlineCount; - private transient Histogram jenkinsExecutorTotalCount; - private transient Histogram jenkinsExecutorUsedCount; - private transient Timer jenkinsBuildDuration; - private transient Map computerBuildDurations = new HashMap(); private transient Logger rootLogger; private SupportProvider supportProvider; @@ -193,19 +186,19 @@ public void setExcludedComponents(Set excludedComponents) throws IOExcep } public Histogram getJenkinsExecutorTotalCount() { - return jenkinsExecutorTotalCount; + return JenkinsMetricProviderImpl.instance().getJenkinsExecutorTotalCount(); } public Histogram getJenkinsExecutorUsedCount() { - return jenkinsExecutorUsedCount; + return JenkinsMetricProviderImpl.instance().getJenkinsExecutorUsedCount(); } public Histogram getJenkinsNodeOnlineCount() { - return jenkinsNodeOnlineCount; + return JenkinsMetricProviderImpl.instance().getJenkinsNodeOnlineCount(); } public Histogram getJenkinsNodeTotalCount() { - return jenkinsNodeTotalCount; + return JenkinsMetricProviderImpl.instance().getJenkinsNodeTotalCount(); } private static Level getLogLevel() { @@ -379,92 +372,6 @@ public synchronized void start() throws Exception { rootLogger = Logger.getLogger(""); rootLogger.addHandler(handler); context = new SupportContextImpl(); - MetricRegistry registry = context.getMetricsRegistry(); - - // add the webapp filter - filter = new DefaultSupportMetricsFilter(registry); - PluginServletFilter.addFilter(filter); - - registry.registerAll(new MemoryUsageGaugeSet()); - registry.registerAll(new GarbageCollectorMetricSet()); - registry.register("file.descriptor.ratio", new FileDescriptorRatioGauge()); - registry.registerAll(new ThreadStatesGaugeSet()); - // add jvm metrics - jenkinsQueueLength = registry.histogram(name(Queue.class, "size")); - jenkinsNodeTotalCount = registry.histogram(name(Node.class, "count")); - jenkinsNodeOnlineCount = registry.histogram(name(Node.class, "online")); - jenkinsExecutorTotalCount = registry.histogram(name(Executor.class, "count")); - jenkinsExecutorUsedCount = registry.histogram(name(Executor.class, "in-use")); - jenkinsBuildDuration = registry.timer(name(Jenkins.class, "builds")); - } - - private synchronized void updateMetrics() { - final Jenkins jenkins = Jenkins.getInstance(); - if (jenkinsQueueLength != null) { - jenkinsQueueLength.update(jenkins.getQueue().getBuildableItems().size()); - } - if (jenkinsNodeTotalCount != null || jenkinsNodeOnlineCount != null || jenkinsExecutorTotalCount != null - || jenkinsExecutorUsedCount != null) { - int nodeTotal = 0; - int nodeOnline = 0; - int executorTotal = 0; - int executorUsed = 0; - if (jenkins.getNumExecutors() > 0) { - nodeTotal++; - Computer computer = jenkins.toComputer(); - if (computer != null) { - if (!computer.isOffline()) { - nodeOnline++; - for (Executor e : computer.getExecutors()) { - executorTotal++; - if (!e.isIdle()) { - executorUsed++; - } - } - } - } - } - Set forRetention = new HashSet(); - for (Node node : jenkins.getNodes()) { - nodeTotal++; - Computer computer = node.toComputer(); - if (computer == null) { - continue; - } - if (!computer.isOffline()) { - nodeOnline++; - for (Executor e : computer.getExecutors()) { - executorTotal++; - if (!e.isIdle()) { - executorUsed++; - } - } - } - forRetention.add(computer); - getOrCreateTimer(computer); - } - for (Map.Entry entry : computerBuildDurations.entrySet()) { - if (forRetention.contains(entry.getKey())) { - continue; - } - // purge dead nodes - getContext().getMetricsRegistry().remove(name(Node.class, "builds", entry.getKey().getName())); - } - computerBuildDurations.keySet().retainAll(forRetention); - if (jenkinsNodeTotalCount != null) { - jenkinsNodeTotalCount.update(nodeTotal); - } - if (jenkinsNodeOnlineCount != null) { - jenkinsNodeOnlineCount.update(nodeOnline); - } - if (jenkinsExecutorTotalCount != null) { - jenkinsExecutorTotalCount.update(executorTotal); - } - if (jenkinsExecutorUsedCount != null) { - jenkinsExecutorUsedCount.update(executorUsed); - } - } - } @NonNull @@ -494,20 +401,7 @@ public synchronized void stop() throws Exception { rootLogger = null; handler.close(); } - if (filter != null) { - PluginServletFilter.removeFilter(filter); - } context.shutdown(); - jenkinsQueueLength = null; - jenkinsNodeTotalCount = null; - jenkinsNodeOnlineCount = null; - jenkinsExecutorTotalCount = null; - jenkinsExecutorUsedCount = null; - jenkinsBuildDuration = null; - if (computerBuildDurations != null) { - computerBuildDurations.clear(); - } - computerBuildDurations = null; super.stop(); } @@ -522,16 +416,6 @@ public List getAllLogRecords(Node node) throws IOException, Interrupt return Collections.emptyList(); } - private synchronized Timer getOrCreateTimer(Computer computer) { - Timer timer = computerBuildDurations.get(computer); - if (timer == null) { - timer = getInstance().getContext().getMetricsRegistry() - .timer(name(Node.class, "builds", computer.getName())); - computerBuildDurations.put(computer, timer); - } - return timer; - } - public static class LogHolder { private static final SupportLogHandler SLAVE_LOG_HANDLER = new SupportLogHandler(256, 2048, 8); } @@ -640,7 +524,6 @@ protected synchronized void doRun() throws Exception { if (plugin == null) { return; } - plugin.updateMetrics(); if (nextBundleWrite.get() < System.currentTimeMillis()) { if (thread != null && thread.isAlive()) { logger.log(Level.INFO, "Periodic bundle generating thread is still running. Execution aborted."); @@ -714,34 +597,6 @@ public boolean accept(File dir, String name) { } - @Extension - public static class RunListenerImpl extends RunListener { - private Map> contexts = new HashMap>(); - - @Override - public synchronized void onStarted(Run run, TaskListener listener) { - List contextList = new ArrayList(); - contextList.add(getInstance().jenkinsBuildDuration.time()); - Executor executor = run.getExecutor(); - if (executor != null) { - Computer computer = executor.getOwner(); - Timer timer = getInstance().getOrCreateTimer(computer); - contextList.add(timer.time()); - } - contexts.put(run, contextList); - } - - @Override - public synchronized void onCompleted(Run run, TaskListener listener) { - List contextList = contexts.remove(run); - if (contextList != null) { - for (Timer.Context context : contextList) { - context.stop(); - } - } - } - } - @Extension public static class GlobalConfigurationImpl extends GlobalConfiguration { diff --git a/src/main/java/com/cloudbees/jenkins/support/api/SupportContext.java b/src/main/java/com/cloudbees/jenkins/support/api/SupportContext.java index a2c286d10..5c563af7d 100644 --- a/src/main/java/com/cloudbees/jenkins/support/api/SupportContext.java +++ b/src/main/java/com/cloudbees/jenkins/support/api/SupportContext.java @@ -34,7 +34,15 @@ * @author Stephen Connolly */ public interface SupportContext { + /** + * @deprecated use {@link com.codahale.metrics.jenkins.Metrics#metricRegistry()} + */ + @Deprecated MetricRegistry getMetricsRegistry(); + /** + * @deprecated use {@link com.codahale.metrics.jenkins.Metrics#healthCheckRegistry()} + */ + @Deprecated HealthCheckRegistry getHealthCheckRegistry(); } diff --git a/src/main/java/com/cloudbees/jenkins/support/impl/Metrics.java b/src/main/java/com/cloudbees/jenkins/support/impl/Metrics.java index 1ce01724e..ab9803cd2 100644 --- a/src/main/java/com/cloudbees/jenkins/support/impl/Metrics.java +++ b/src/main/java/com/cloudbees/jenkins/support/impl/Metrics.java @@ -25,8 +25,6 @@ @Extension public class Metrics extends Component { - private MetricRegistry metricsRegistry; - @Override @NonNull public String getDisplayName() { @@ -35,18 +33,12 @@ public String getDisplayName() { @Override public void addContents(@NonNull Container result) { - result.add(new MetricsContent("nodes/master/metrics.json", metricsRegistry)); + result.add(new MetricsContent("nodes/master/metrics.json", com.codahale.metrics.jenkins.Metrics.metricRegistry())); for (final Node node : Jenkins.getInstance().getNodes()) { result.add(new RemoteMetricsContent("nodes/slave/" + node.getDisplayName() + "/metrics.json", node)); } } - @Override - public void start(@NonNull SupportContext context) { - super.start(context); - metricsRegistry = context.getMetricsRegistry(); - } - private static class RemoteMetricsContent extends Content { private final Node node;