Skip to content

Commit

Permalink
Rework Jetty ThreadPool metrics to work with any ThreadPool
Browse files Browse the repository at this point in the history
Closes gh-911
  • Loading branch information
izeye authored and jkschneider committed Oct 11, 2018
1 parent 0aee9da commit deb6fa7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,55 @@
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.lang.NonNull;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool.SizedThreadPool;

/**
* {@link MeterBinder} for Jetty {@link ThreadPool}.
*
* @author Manabu Matsuzaki
* @author Andy Wilkinson
*/
public class JettyServerThreadPoolMetrics implements MeterBinder {

private final InstrumentedQueuedThreadPool threadPool;
private final ThreadPool threadPool;

private final Iterable<Tag> tags;

public JettyServerThreadPoolMetrics(InstrumentedQueuedThreadPool threadPool, Iterable<Tag> tags) {
public JettyServerThreadPoolMetrics(ThreadPool threadPool, Iterable<Tag> tags) {
this.threadPool = threadPool;
this.tags = tags;
}

@Override
public void bindTo(@NonNull MeterRegistry registry) {
Gauge.builder("jetty.threads.config.min", threadPool, InstrumentedQueuedThreadPool::getMinThreads)
.tags(tags)
.description("The number of min threads")
.register(registry);
Gauge.builder("jetty.threads.config.max", threadPool, InstrumentedQueuedThreadPool::getMaxThreads)
.tags(tags)
.description("The number of max threads")
.register(registry);
Gauge.builder("jetty.threads.current", threadPool, InstrumentedQueuedThreadPool::getThreads)
.tags(tags)
.description("The current number of current threads")
.register(registry);
Gauge.builder("jetty.threads.busy", threadPool, InstrumentedQueuedThreadPool::getBusyThreads)
.tags(tags)
.description("The current number of busy threads")
.register(registry);
public void bindTo(MeterRegistry registry) {
if (this.threadPool instanceof SizedThreadPool) {
SizedThreadPool sizedThreadPool = (SizedThreadPool) this.threadPool;
Gauge.builder("jetty.threads.config.min", sizedThreadPool,
SizedThreadPool::getMinThreads)
.description("The minimum number of threads in the pool")
.tags(this.tags).register(registry);
Gauge.builder("jetty.threads.config.max", sizedThreadPool,
SizedThreadPool::getMaxThreads)
.description("The maximum number of threads in the pool")
.tags(this.tags).register(registry);
if (this.threadPool instanceof QueuedThreadPool) {
QueuedThreadPool queuedThreadPool = (QueuedThreadPool) this.threadPool;
Gauge.builder("jetty.threads.busy", queuedThreadPool,
QueuedThreadPool::getBusyThreads)
.description("The number of busy threads in the pool")
.tags(this.tags).register(registry);
}
}
Gauge.builder("jetty.threads.current", this.threadPool,
ThreadPool::getThreads)
.description("The total number of threads in the pool")
.tags(this.tags).register(registry);
Gauge.builder("jetty.threads.idle", this.threadPool,
ThreadPool::getIdleThreads)
.description("The number of idle threads in the pool").tags(this.tags)
.register(registry);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@
*/
package io.micrometer.spring.autoconfigure.web.jetty;

import java.util.Collections;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jetty.JettyServerThreadPoolMetrics;
import io.micrometer.spring.autoconfigure.MetricsAutoConfiguration;
import io.micrometer.spring.autoconfigure.export.simple.SimpleMetricsExportAutoConfiguration;
import org.eclipse.jetty.server.Server;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
Expand All @@ -37,6 +42,7 @@
* @author Jon Schneider
* @author Michael Weirauch
* @author Johnny Lim
* @author Andy Wilkinson
*/
@Configuration
@AutoConfigureAfter({
Expand All @@ -48,9 +54,23 @@
@Conditional(JettyMetricsAutoConfiguration.JettyMetricsAutoConfigurationConditionalOnBeans.class)
public class JettyMetricsAutoConfiguration {

private volatile Server server;

@Bean
public JettyMetricsPostProcessor jettyMetricsPostProcessor(ApplicationContext context) {
return new JettyMetricsPostProcessor(context);
public EmbeddedServletContainerCustomizer jettyCustomizer() {
return (jetty) -> {
((JettyEmbeddedServletContainerFactory) jetty).setServerCustomizers(Collections.singleton(this::setServer));
};
}

@Bean
@ConditionalOnMissingBean
public JettyServerThreadPoolMetrics jettyThreadPoolMetrics() {
return new JettyServerThreadPoolMetrics(this.server.getThreadPool(), Collections.emptyList());
}

private void setServer(Server server) {
this.server = server;
}

static class JettyMetricsAutoConfigurationConditionalOnBeans extends AllNestedConditions {
Expand Down

This file was deleted.

0 comments on commit deb6fa7

Please sign in to comment.