Skip to content

Commit

Permalink
Switch to codahale-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Feb 14, 2014
1 parent d0aed9b commit 5905c4f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 205 deletions.
43 changes: 6 additions & 37 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
</scm>

<properties>
<metrics.version>3.0.1</metrics.version>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -107,42 +106,7 @@
</dependency>
<!-- regular dependencies -->
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-servlet</artifactId>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-jvm</artifactId>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-json</artifactId>
<version>${metrics.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>net.sf.uadetector</groupId>
<groupId>net.sf.uadetector</groupId>
<artifactId>uadetector-resources</artifactId>
<version>2013.09</version>
</dependency>
Expand All @@ -152,6 +116,11 @@
<version>1.9</version>
</dependency>
<!-- plugin dependencies -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>codahale-metrics</artifactId>
<version>3.0.1-1-SNAPSHOT</version>
</dependency>
<!-- jenkins dependencies -->
<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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();
}
}
155 changes: 5 additions & 150 deletions src/main/java/com/cloudbees/jenkins/support/SupportPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Computer, Timer> computerBuildDurations = new HashMap<Computer, Timer>();
private transient Logger rootLogger;

private SupportProvider supportProvider;
Expand Down Expand Up @@ -193,19 +186,19 @@ public void setExcludedComponents(Set<String> 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() {
Expand Down Expand Up @@ -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<Computer> forRetention = new HashSet<Computer>();
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<Computer, Timer> 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
Expand Down Expand Up @@ -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();
}
Expand All @@ -522,16 +416,6 @@ public List<LogRecord> 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);
}
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -714,34 +597,6 @@ public boolean accept(File dir, String name) {

}

@Extension
public static class RunListenerImpl extends RunListener<Run> {
private Map<Run, List<Timer.Context>> contexts = new HashMap<Run, List<Timer.Context>>();

@Override
public synchronized void onStarted(Run run, TaskListener listener) {
List<Timer.Context> contextList = new ArrayList<Timer.Context>();
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<Timer.Context> contextList = contexts.remove(run);
if (contextList != null) {
for (Timer.Context context : contextList) {
context.stop();
}
}
}
}

@Extension
public static class GlobalConfigurationImpl extends GlobalConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
10 changes: 1 addition & 9 deletions src/main/java/com/cloudbees/jenkins/support/impl/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
@Extension
public class Metrics extends Component {

private MetricRegistry metricsRegistry;

@Override
@NonNull
public String getDisplayName() {
Expand All @@ -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;
Expand Down

0 comments on commit 5905c4f

Please sign in to comment.