From 3f46c6c566574c70950d255b2428cd75092d3902 Mon Sep 17 00:00:00 2001 From: Manabu Matsuzaki Date: Tue, 8 May 2018 04:49:36 +0900 Subject: [PATCH 1/8] add Jetty's threadpool metrics --- .../jetty/InstrumentedQueuedThreadPool.java | 38 +++++++++++ .../jetty/JettyServerThreadPoolMetrics.java | 56 ++++++++++++++++ .../JettyServerThreadPoolMetricsTest.java | 65 +++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/InstrumentedQueuedThreadPool.java create mode 100644 micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java create mode 100644 micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/InstrumentedQueuedThreadPool.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/InstrumentedQueuedThreadPool.java new file mode 100644 index 0000000000..186caf93aa --- /dev/null +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/InstrumentedQueuedThreadPool.java @@ -0,0 +1,38 @@ +/** + * Copyright 2017 Pivotal Software, Inc. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micrometer.core.instrument.binder.jetty; + +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Tag; +import org.eclipse.jetty.util.thread.QueuedThreadPool; + +public class InstrumentedQueuedThreadPool extends QueuedThreadPool { + + private final MeterRegistry registry; + private final Iterable tags; + + public InstrumentedQueuedThreadPool(MeterRegistry registry, Iterable tags) { + this.registry = registry; + this.tags = tags; + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + JettyServerThreadPoolMetrics threadPoolMetrics = new JettyServerThreadPoolMetrics(this, tags); + threadPoolMetrics.bindTo(registry); + } +} diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java new file mode 100644 index 0000000000..3f3ee0df58 --- /dev/null +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java @@ -0,0 +1,56 @@ +/** + * Copyright 2017 Pivotal Software, Inc. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micrometer.core.instrument.binder.jetty; + +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.binder.MeterBinder; + +public class JettyServerThreadPoolMetrics implements MeterBinder { + + private final InstrumentedQueuedThreadPool threadPool; + private final Iterable tags; + + public JettyServerThreadPoolMetrics(InstrumentedQueuedThreadPool threadPool, Iterable tags) { + this.threadPool = threadPool; + this.tags = tags; + } + + @Override + public void bindTo(MeterRegistry registry) { + Gauge.builder("jetty.threads.min", threadPool, InstrumentedQueuedThreadPool::getMinThreads) + .tags(tags) + .description("The number of min threads") + .register(registry); + Gauge.builder("jetty.threads.max", threadPool, InstrumentedQueuedThreadPool::getMaxThreads) + .tags(tags) + .description("The number of max threads") + .register(registry); + Gauge.builder("jetty.threads.live", threadPool, InstrumentedQueuedThreadPool::getThreads) + .tags(tags) + .description("The current number of live threads") + .register(registry); + Gauge.builder("jetty.threads.idle", threadPool, InstrumentedQueuedThreadPool::getIdleThreads) + .tags(tags) + .description("The current number of idle threads") + .register(registry); + Gauge.builder("jetty.threads.busy", threadPool, InstrumentedQueuedThreadPool::getBusyThreads) + .tags(tags) + .description("The current number of busy threads") + .register(registry); + } +} diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java new file mode 100644 index 0000000000..6c98931fe5 --- /dev/null +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java @@ -0,0 +1,65 @@ +/** + * Copyright 2017 Pivotal Software, Inc. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micrometer.core.instrument.binder.jetty; + +import io.micrometer.core.instrument.MockClock; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.simple.SimpleConfig; +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; + +public class JettyServerThreadPoolMetricsTest { + private SimpleMeterRegistry registry; + private Server server; + + @BeforeEach + void setup() throws Exception { + registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock()); + + Iterable tags = Collections.singletonList(Tag.of("id", "0")); + QueuedThreadPool threadPool = new InstrumentedQueuedThreadPool(registry, tags); + threadPool.setMinThreads(32); + threadPool.setMaxThreads(100); + server = new Server(threadPool); + ServerConnector connector = new ServerConnector(server); + server.setConnectors(new Connector[] { connector }); + server.start(); + } + + @AfterEach + void teardown() throws Exception { + server.stop(); + } + + @Test + void threadsMetrics() throws Exception { + assertThat(registry.get("jetty.threads.min").gauge().value()).isEqualTo(32.0); + assertThat(registry.get("jetty.threads.max").gauge().value()).isEqualTo(100.0); + assertThat(registry.get("jetty.threads.live").gauge().value()).isEqualTo(32.0); + assertThat(registry.get("jetty.threads.idle").gauge().value()).isEqualTo(27.0); + assertThat(registry.get("jetty.threads.busy").gauge().value()).isEqualTo(5.0); + } +} From d216dde625c94143d9b1032bb5b3b46bf5a33ae8 Mon Sep 17 00:00:00 2001 From: Manabu Matsuzaki Date: Tue, 8 May 2018 05:20:24 +0900 Subject: [PATCH 2/8] fix test --- .../binder/jetty/JettyServerThreadPoolMetricsTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java index 6c98931fe5..3cf9a681fb 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java @@ -58,8 +58,8 @@ void teardown() throws Exception { void threadsMetrics() throws Exception { assertThat(registry.get("jetty.threads.min").gauge().value()).isEqualTo(32.0); assertThat(registry.get("jetty.threads.max").gauge().value()).isEqualTo(100.0); - assertThat(registry.get("jetty.threads.live").gauge().value()).isEqualTo(32.0); - assertThat(registry.get("jetty.threads.idle").gauge().value()).isEqualTo(27.0); - assertThat(registry.get("jetty.threads.busy").gauge().value()).isEqualTo(5.0); + assertThat(registry.get("jetty.threads.live").gauge().value()).isNotEqualTo(0.0); + assertThat(registry.get("jetty.threads.idle").gauge().value()).isNotEqualTo(0.0); + assertThat(registry.get("jetty.threads.busy").gauge().value()).isNotEqualTo(0.0); } } From c24112f406de446c93bac470be346de87131a084 Mon Sep 17 00:00:00 2001 From: Manabu Matsuzaki Date: Tue, 8 May 2018 23:20:59 +0900 Subject: [PATCH 3/8] Update metric name --- .../instrument/binder/jetty/JettyServerThreadPoolMetrics.java | 4 ++-- .../binder/jetty/JettyServerThreadPoolMetricsTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java index 3f3ee0df58..0f048b7aaf 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java @@ -40,9 +40,9 @@ public void bindTo(MeterRegistry registry) { .tags(tags) .description("The number of max threads") .register(registry); - Gauge.builder("jetty.threads.live", threadPool, InstrumentedQueuedThreadPool::getThreads) + Gauge.builder("jetty.threads.current", threadPool, InstrumentedQueuedThreadPool::getThreads) .tags(tags) - .description("The current number of live threads") + .description("The current number of current threads") .register(registry); Gauge.builder("jetty.threads.idle", threadPool, InstrumentedQueuedThreadPool::getIdleThreads) .tags(tags) diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java index 3cf9a681fb..141dd42eb5 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java @@ -58,7 +58,7 @@ void teardown() throws Exception { void threadsMetrics() throws Exception { assertThat(registry.get("jetty.threads.min").gauge().value()).isEqualTo(32.0); assertThat(registry.get("jetty.threads.max").gauge().value()).isEqualTo(100.0); - assertThat(registry.get("jetty.threads.live").gauge().value()).isNotEqualTo(0.0); + assertThat(registry.get("jetty.threads.current").gauge().value()).isNotEqualTo(0.0); assertThat(registry.get("jetty.threads.idle").gauge().value()).isNotEqualTo(0.0); assertThat(registry.get("jetty.threads.busy").gauge().value()).isNotEqualTo(0.0); } From 7c02cc76bad7c7edf035f8881210b741f475989e Mon Sep 17 00:00:00 2001 From: Manabu Matsuzaki Date: Fri, 11 May 2018 01:41:36 +0900 Subject: [PATCH 4/8] update metrics name and delete idle metric --- .../binder/jetty/JettyServerThreadPoolMetrics.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java index 0f048b7aaf..03c89947a3 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetrics.java @@ -32,11 +32,11 @@ public JettyServerThreadPoolMetrics(InstrumentedQueuedThreadPool threadPool, Ite @Override public void bindTo(MeterRegistry registry) { - Gauge.builder("jetty.threads.min", threadPool, InstrumentedQueuedThreadPool::getMinThreads) + Gauge.builder("jetty.threads.config.min", threadPool, InstrumentedQueuedThreadPool::getMinThreads) .tags(tags) .description("The number of min threads") .register(registry); - Gauge.builder("jetty.threads.max", threadPool, InstrumentedQueuedThreadPool::getMaxThreads) + Gauge.builder("jetty.threads.config.max", threadPool, InstrumentedQueuedThreadPool::getMaxThreads) .tags(tags) .description("The number of max threads") .register(registry); @@ -44,10 +44,6 @@ public void bindTo(MeterRegistry registry) { .tags(tags) .description("The current number of current threads") .register(registry); - Gauge.builder("jetty.threads.idle", threadPool, InstrumentedQueuedThreadPool::getIdleThreads) - .tags(tags) - .description("The current number of idle threads") - .register(registry); Gauge.builder("jetty.threads.busy", threadPool, InstrumentedQueuedThreadPool::getBusyThreads) .tags(tags) .description("The current number of busy threads") From 4cec3d590297ee541a788440e610036aee5041e7 Mon Sep 17 00:00:00 2001 From: Manabu Matsuzaki Date: Fri, 11 May 2018 01:51:09 +0900 Subject: [PATCH 5/8] fix test --- .../binder/jetty/JettyServerThreadPoolMetricsTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java index 141dd42eb5..c65e66928b 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jetty/JettyServerThreadPoolMetricsTest.java @@ -56,10 +56,9 @@ void teardown() throws Exception { @Test void threadsMetrics() throws Exception { - assertThat(registry.get("jetty.threads.min").gauge().value()).isEqualTo(32.0); - assertThat(registry.get("jetty.threads.max").gauge().value()).isEqualTo(100.0); + assertThat(registry.get("jetty.threads.config.min").gauge().value()).isEqualTo(32.0); + assertThat(registry.get("jetty.threads.config.max").gauge().value()).isEqualTo(100.0); assertThat(registry.get("jetty.threads.current").gauge().value()).isNotEqualTo(0.0); - assertThat(registry.get("jetty.threads.idle").gauge().value()).isNotEqualTo(0.0); assertThat(registry.get("jetty.threads.busy").gauge().value()).isNotEqualTo(0.0); } } From f64f6abd3664959da25228d51080d1859973fbb8 Mon Sep 17 00:00:00 2001 From: Manabu Matsuzaki Date: Sat, 19 May 2018 23:06:45 +0900 Subject: [PATCH 6/8] Add auto configuration to enable InstrumentedQueuedThreadPool --- .../web/jetty/JettyMetricsConfiguration.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java diff --git a/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java b/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java new file mode 100644 index 0000000000..6564761c2f --- /dev/null +++ b/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java @@ -0,0 +1,37 @@ +/** + * Copyright 2017 Pivotal Software, Inc. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micrometer.spring.autoconfigure.web.jetty; + +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.binder.jetty.InstrumentedQueuedThreadPool; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Collections; + +@Configuration +@ConditionalOnClass(name = "org.eclipse.jetty.server.Server") +public class JettyMetricsConfiguration { + + @Bean + public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(MeterRegistry registry) { + JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(); + factory.setThreadPool(new InstrumentedQueuedThreadPool(registry, Collections.emptyList())); + return factory; + } +} From 02c633eddb2452c38cd00cce7d577e65103f442d Mon Sep 17 00:00:00 2001 From: Manabu Matsuzaki Date: Sat, 19 May 2018 23:19:39 +0900 Subject: [PATCH 7/8] fix test --- .../autoconfigure/web/jetty/JettyMetricsConfiguration.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java b/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java index 6564761c2f..c26a9ee82a 100644 --- a/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java +++ b/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java @@ -25,7 +25,10 @@ import java.util.Collections; @Configuration -@ConditionalOnClass(name = "org.eclipse.jetty.server.Server") +@ConditionalOnClass(name = { + "org.eclipse.jetty.server.Server", + "org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory" +}) public class JettyMetricsConfiguration { @Bean From f89ff6fd5d92ef5c7131dbab9611f95834899e66 Mon Sep 17 00:00:00 2001 From: Manabu Matsuzaki Date: Sat, 19 May 2018 23:36:38 +0900 Subject: [PATCH 8/8] fix test --- .../autoconfigure/web/jetty/JettyMetricsConfiguration.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java b/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java index c26a9ee82a..512ab73faf 100644 --- a/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java +++ b/micrometer-spring-legacy/src/main/java/io/micrometer/spring/autoconfigure/web/jetty/JettyMetricsConfiguration.java @@ -18,6 +18,7 @@ import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.binder.jetty.InstrumentedQueuedThreadPool; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -25,10 +26,8 @@ import java.util.Collections; @Configuration -@ConditionalOnClass(name = { - "org.eclipse.jetty.server.Server", - "org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory" -}) +@ConditionalOnClass(name = "org.eclipse.jetty.server.Server") +@ConditionalOnMissingClass("org.apache.catalina.startup.Tomcat") public class JettyMetricsConfiguration { @Bean