diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingHttpClientConnectionManagerMetricsBinder.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingHttpClientConnectionManagerMetricsBinder.java index 354d286a50..60aade36b1 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingHttpClientConnectionManagerMetricsBinder.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingHttpClientConnectionManagerMetricsBinder.java @@ -15,14 +15,18 @@ */ package io.micrometer.core.instrument.binder.httpcomponents; -import io.micrometer.core.instrument.*; +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.binder.MeterBinder; import io.micrometer.core.lang.NonNull; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.conn.routing.HttpRoute; +import org.apache.http.pool.ConnPoolControl; /** - * Collects metrics from a {@link PoolingHttpClientConnectionManager}. - * + * Collects metrics from a {@link ConnPoolControl}, for example a {@link org.apache.http.impl.conn.PoolingHttpClientConnectionManager}. + *

* It monitors the overall connection pool state. * * @author Benjamin Hubert (benjamin.hubert@willhaben.at) @@ -30,34 +34,33 @@ */ public class PoolingHttpClientConnectionManagerMetricsBinder implements MeterBinder { - private final PoolingHttpClientConnectionManager connectionManager; + private final ConnPoolControl connPoolControl; private final Iterable tags; /** - * Creates a metrics binder for the given pooling connection manager. + * Creates a metrics binder for the given pooling connection pool control. * - * @param connectionManager The connection manager to monitor. - * @param name Name of the connection manager. Will be added as tag with the - * key "httpclient". - * @param tags Tags to apply to all recorded metrics. Must be an even number - * of arguments representing key/value pairs of tags. + * @param connPoolControl The connection pool control to monitor. + * @param name Name of the connection pool control. Will be added as tag with the + * key "httpclient". + * @param tags Tags to apply to all recorded metrics. Must be an even number + * of arguments representing key/value pairs of tags. */ @SuppressWarnings("WeakerAccess") - public PoolingHttpClientConnectionManagerMetricsBinder(PoolingHttpClientConnectionManager connectionManager, String name, String... tags) { - this(connectionManager, name, Tags.of(tags)); + public PoolingHttpClientConnectionManagerMetricsBinder(ConnPoolControl connPoolControl, String name, String... tags) { + this(connPoolControl, name, Tags.of(tags)); } /** - * Creates a metrics binder for the given pooling connection manager. + * Creates a metrics binder for the given connection pool control. * - * @param connectionManager The connection manager to monitor. - * @param name Name of the connection manager. Will be added as tag with the - * key "httpclient". - * @param tags Tags to apply to all recorded metrics. + * @param connPoolControl The connection pool control to monitor. + * @param name Name of the connection pool control. Will be added as tag with the key "httpclient". + * @param tags Tags to apply to all recorded metrics. */ @SuppressWarnings("WeakerAccess") - public PoolingHttpClientConnectionManagerMetricsBinder(PoolingHttpClientConnectionManager connectionManager, String name, Iterable tags) { - this.connectionManager = connectionManager; + public PoolingHttpClientConnectionManagerMetricsBinder(ConnPoolControl connPoolControl, String name, Iterable tags) { + this.connPoolControl = connPoolControl; this.tags = Tags.concat(tags, "httpclient", name); } @@ -68,35 +71,35 @@ public void bindTo(@NonNull MeterRegistry registry) { private void registerTotalMetrics(MeterRegistry registry) { Gauge.builder("httpcomponents.httpclient.pool.total.max", - connectionManager, - (connectionManager) -> connectionManager.getTotalStats().getMax()) - .description("The configured maximum number of allowed persistent connections for all routes.") - .tags(tags) - .register(registry); + connPoolControl, + (connPoolControl) -> connPoolControl.getTotalStats().getMax()) + .description("The configured maximum number of allowed persistent connections for all routes.") + .tags(tags) + .register(registry); Gauge.builder("httpcomponents.httpclient.pool.total.connections", - connectionManager, - (connectionManager) -> connectionManager.getTotalStats().getAvailable()) - .description("The number of persistent and available connections for all routes.") - .tags(tags).tag("state", "available") - .register(registry); + connPoolControl, + (connPoolControl) -> connPoolControl.getTotalStats().getAvailable()) + .description("The number of persistent and available connections for all routes.") + .tags(tags).tag("state", "available") + .register(registry); Gauge.builder("httpcomponents.httpclient.pool.total.connections", - connectionManager, - (connectionManager) -> connectionManager.getTotalStats().getLeased()) - .description("The number of persistent and leased connections for all routes.") - .tags(tags).tag("state", "leased") - .register(registry); + connPoolControl, + (connPoolControl) -> connPoolControl.getTotalStats().getLeased()) + .description("The number of persistent and leased connections for all routes.") + .tags(tags).tag("state", "leased") + .register(registry); Gauge.builder("httpcomponents.httpclient.pool.total.pending", - connectionManager, - (connectionManager) -> connectionManager.getTotalStats().getPending()) - .description("The number of connection requests being blocked awaiting a free connection for all routes.") - .tags(tags) - .register(registry); + connPoolControl, + (connPoolControl) -> connPoolControl.getTotalStats().getPending()) + .description("The number of connection requests being blocked awaiting a free connection for all routes.") + .tags(tags) + .register(registry); Gauge.builder("httpcomponents.httpclient.pool.route.max.default", - connectionManager, - PoolingHttpClientConnectionManager::getDefaultMaxPerRoute) - .description("The configured default maximum number of allowed persistent connections per route.") - .tags(tags) - .register(registry); + connPoolControl, + ConnPoolControl::getDefaultMaxPerRoute) + .description("The configured default maximum number of allowed persistent connections per route.") + .tags(tags) + .register(registry); } } diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingNHttpClientConnectionManagerMetricsBinder.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingNHttpClientConnectionManagerMetricsBinder.java deleted file mode 100644 index 5ca79c3120..0000000000 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingNHttpClientConnectionManagerMetricsBinder.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2019 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 - *

- * https://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.httpcomponents; - -import io.micrometer.core.instrument.*; -import io.micrometer.core.instrument.binder.MeterBinder; -import io.micrometer.core.lang.NonNull; -import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; - -/** - * Collects metrics from a {@link PoolingNHttpClientConnectionManager}. - * - * It monitors the overall connection pool state. - * - * @author Daniel Albuquerque - * @since 1.4.0 - */ -public class PoolingNHttpClientConnectionManagerMetricsBinder implements MeterBinder { - - private final PoolingNHttpClientConnectionManager connectionManager; - private final Iterable tags; - - /** - * Creates a metrics binder for the given pooling connection manager. - * - * @param connectionManager The connection manager to monitor. - * @param name Name of the connection manager. Will be added as tag with the - * key "httpclient". - * @param tags Tags to apply to all recorded metrics. Must be an even number - * of arguments representing key/value pairs of tags. - */ - @SuppressWarnings("WeakerAccess") - public PoolingNHttpClientConnectionManagerMetricsBinder(PoolingNHttpClientConnectionManager connectionManager, String name, String... tags) { - this(connectionManager, name, Tags.of(tags)); - } - - /** - * Creates a metrics binder for the given pooling connection manager. - * - * @param connectionManager The connection manager to monitor. - * @param name Name of the connection manager. Will be added as tag with the - * key "httpclient". - * @param tags Tags to apply to all recorded metrics. - */ - @SuppressWarnings("WeakerAccess") - public PoolingNHttpClientConnectionManagerMetricsBinder(PoolingNHttpClientConnectionManager connectionManager, String name, Iterable tags) { - this.connectionManager = connectionManager; - this.tags = Tags.concat(tags, "httpclient", name); - } - - @Override - public void bindTo(@NonNull MeterRegistry registry) { - registerTotalMetrics(registry); - } - - private void registerTotalMetrics(MeterRegistry registry) { - Gauge.builder("httpcomponents.httpasyncclient.pool.total.max", - connectionManager, - (connectionManager) -> connectionManager.getTotalStats().getMax()) - .description("The configured maximum number of allowed persistent connections for all routes.") - .tags(tags) - .register(registry); - Gauge.builder("httpcomponents.httpasyncclient.pool.total.connections", - connectionManager, - (connectionManager) -> connectionManager.getTotalStats().getAvailable()) - .description("The number of persistent and available connections for all routes.") - .tags(tags).tag("state", "available") - .register(registry); - Gauge.builder("httpcomponents.httpasyncclient.pool.total.connections", - connectionManager, - (connectionManager) -> connectionManager.getTotalStats().getLeased()) - .description("The number of persistent and leased connections for all routes.") - .tags(tags).tag("state", "leased") - .register(registry); - Gauge.builder("httpcomponents.httpasyncclient.pool.total.pending", - connectionManager, - (connectionManager) -> connectionManager.getTotalStats().getPending()) - .description("The number of connection requests being blocked awaiting a free connection for all routes.") - .tags(tags) - .register(registry); - Gauge.builder("httpcomponents.httpasyncclient.pool.route.max.default", - connectionManager, - PoolingNHttpClientConnectionManager::getDefaultMaxPerRoute) - .description("The configured default maximum number of allowed persistent connections per route.") - .tags(tags) - .register(registry); - } -} diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingHttpClientConnectionManagerMetricsBinderTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingHttpClientConnectionManagerMetricsBinderTest.java index d4833a7e99..ec1cd137e8 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingHttpClientConnectionManagerMetricsBinderTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingHttpClientConnectionManagerMetricsBinderTest.java @@ -19,7 +19,7 @@ import io.micrometer.core.instrument.MockClock; import io.micrometer.core.instrument.simple.SimpleConfig; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.pool.ConnPoolControl; import org.apache.http.pool.PoolStats; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -36,13 +36,13 @@ class PoolingHttpClientConnectionManagerMetricsBinderTest { private MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock()); - private PoolingHttpClientConnectionManager connectionManager; + private ConnPoolControl connPoolControl; private PoolingHttpClientConnectionManagerMetricsBinder binder; @BeforeEach void setup() { - connectionManager = mock(PoolingHttpClientConnectionManager.class); - binder = new PoolingHttpClientConnectionManagerMetricsBinder(connectionManager, "test"); + connPoolControl = mock(ConnPoolControl.class); + binder = new PoolingHttpClientConnectionManagerMetricsBinder(connPoolControl, "test"); binder.bindTo(registry); } @@ -50,7 +50,7 @@ void setup() { void totalMax() { PoolStats poolStats = mock(PoolStats.class); when(poolStats.getMax()).thenReturn(13); - when(connectionManager.getTotalStats()).thenReturn(poolStats); + when(connPoolControl.getTotalStats()).thenReturn(poolStats); assertThat(registry.get("httpcomponents.httpclient.pool.total.max") .tags("httpclient", "test") .gauge().value()).isEqualTo(13.0); @@ -60,7 +60,7 @@ void totalMax() { void totalAvailable() { PoolStats poolStats = mock(PoolStats.class); when(poolStats.getAvailable()).thenReturn(17); - when(connectionManager.getTotalStats()).thenReturn(poolStats); + when(connPoolControl.getTotalStats()).thenReturn(poolStats); assertThat(registry.get("httpcomponents.httpclient.pool.total.connections") .tags("httpclient", "test", "state", "available") .gauge().value()).isEqualTo(17.0); @@ -70,7 +70,7 @@ void totalAvailable() { void totalLeased() { PoolStats poolStats = mock(PoolStats.class); when(poolStats.getLeased()).thenReturn(23); - when(connectionManager.getTotalStats()).thenReturn(poolStats); + when(connPoolControl.getTotalStats()).thenReturn(poolStats); assertThat(registry.get("httpcomponents.httpclient.pool.total.connections") .tags("httpclient", "test", "state", "leased") .gauge().value()).isEqualTo(23.0); @@ -80,7 +80,7 @@ void totalLeased() { void totalPending() { PoolStats poolStats = mock(PoolStats.class); when(poolStats.getPending()).thenReturn(37); - when(connectionManager.getTotalStats()).thenReturn(poolStats); + when(connPoolControl.getTotalStats()).thenReturn(poolStats); assertThat(registry.get("httpcomponents.httpclient.pool.total.pending") .tags("httpclient", "test") .gauge().value()).isEqualTo(37.0); @@ -88,7 +88,7 @@ void totalPending() { @Test void routeMaxDefault() { - when(connectionManager.getDefaultMaxPerRoute()).thenReturn(7); + when(connPoolControl.getDefaultMaxPerRoute()).thenReturn(7); assertThat(registry.get("httpcomponents.httpclient.pool.route.max.default") .tags("httpclient", "test") .gauge().value()).isEqualTo(7.0); diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingNHttpClientConnectionManagerMetricsBinderTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingNHttpClientConnectionManagerMetricsBinderTest.java deleted file mode 100644 index 1f00992212..0000000000 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/httpcomponents/PoolingNHttpClientConnectionManagerMetricsBinderTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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 - *

- * https://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.httpcomponents; - -import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.MockClock; -import io.micrometer.core.instrument.simple.SimpleConfig; -import io.micrometer.core.instrument.simple.SimpleMeterRegistry; -import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; -import org.apache.http.pool.PoolStats; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** - * Unit tests for {@link PoolingNHttpClientConnectionManagerMetricsBinder}. - * - * @author Daniel Albuquerque - */ -class PoolingNHttpClientConnectionManagerMetricsBinderTest { - - private MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock()); - private PoolingNHttpClientConnectionManager connectionManager; - private PoolingNHttpClientConnectionManagerMetricsBinder binder; - - @BeforeEach - void setup() { - connectionManager = mock(PoolingNHttpClientConnectionManager.class); - binder = new PoolingNHttpClientConnectionManagerMetricsBinder(connectionManager, "test"); - binder.bindTo(registry); - } - - @Test - void totalMax() { - PoolStats poolStats = mock(PoolStats.class); - when(poolStats.getMax()).thenReturn(13); - when(connectionManager.getTotalStats()).thenReturn(poolStats); - assertThat(registry.get("httpcomponents.httpasyncclient.pool.total.max") - .tags("httpclient", "test") - .gauge().value()).isEqualTo(13.0); - } - - @Test - void totalAvailable() { - PoolStats poolStats = mock(PoolStats.class); - when(poolStats.getAvailable()).thenReturn(17); - when(connectionManager.getTotalStats()).thenReturn(poolStats); - assertThat(registry.get("httpcomponents.httpasyncclient.pool.total.connections") - .tags("httpclient", "test", "state", "available") - .gauge().value()).isEqualTo(17.0); - } - - @Test - void totalLeased() { - PoolStats poolStats = mock(PoolStats.class); - when(poolStats.getLeased()).thenReturn(23); - when(connectionManager.getTotalStats()).thenReturn(poolStats); - assertThat(registry.get("httpcomponents.httpasyncclient.pool.total.connections") - .tags("httpclient", "test", "state", "leased") - .gauge().value()).isEqualTo(23.0); - } - - @Test - void totalPending() { - PoolStats poolStats = mock(PoolStats.class); - when(poolStats.getPending()).thenReturn(37); - when(connectionManager.getTotalStats()).thenReturn(poolStats); - assertThat(registry.get("httpcomponents.httpasyncclient.pool.total.pending") - .tags("httpclient", "test") - .gauge().value()).isEqualTo(37.0); - } - - @Test - void routeMaxDefault() { - when(connectionManager.getDefaultMaxPerRoute()).thenReturn(7); - assertThat(registry.get("httpcomponents.httpasyncclient.pool.route.max.default") - .tags("httpclient", "test") - .gauge().value()).isEqualTo(7.0); - } - -}