From b528c9dfc70d3d011b0de260d53e06aacc518fb3 Mon Sep 17 00:00:00 2001 From: Violeta Georgieva Date: Wed, 19 May 2021 12:55:16 +0300 Subject: [PATCH] Expose a configuration for HttpClient channel pools metrics Fixes gh-2241 --- docs/src/main/asciidoc/_configprops.adoc | 3 ++- .../config/GatewayAutoConfiguration.java | 1 + .../gateway/config/HttpClientProperties.java | 18 ++++++++++++++++-- .../config/GatewayAutoConfigurationTests.java | 4 +++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/src/main/asciidoc/_configprops.adoc b/docs/src/main/asciidoc/_configprops.adoc index 8d6460e7..7247d901 100644 --- a/docs/src/main/asciidoc/_configprops.adoc +++ b/docs/src/main/asciidoc/_configprops.adoc @@ -74,11 +74,12 @@ |spring.cloud.gateway.httpclient.connect-timeout | | The connect timeout in millis, the default is 45s. |spring.cloud.gateway.httpclient.max-header-size | | The max response header size. |spring.cloud.gateway.httpclient.max-initial-line-length | | The max initial line length. -|spring.cloud.gateway.httpclient.pool.acquire-timeout | | Only for type FIXED, the maximum time in millis to wait for aquiring. +|spring.cloud.gateway.httpclient.pool.acquire-timeout | | Only for type FIXED, the maximum time in millis to wait for acquiring. |spring.cloud.gateway.httpclient.pool.eviction-interval | `0` | Perform regular eviction checks in the background at a specified interval. Disabled by default ({@link Duration#ZERO}) |spring.cloud.gateway.httpclient.pool.max-connections | | Only for type FIXED, the maximum number of connections before starting pending acquisition on existing ones. |spring.cloud.gateway.httpclient.pool.max-idle-time | | Time in millis after which the channel will be closed. If NULL, there is no max idle time. |spring.cloud.gateway.httpclient.pool.max-life-time | | Duration after which the channel will be closed. If NULL, there is no max life time. +|spring.cloud.gateway.httpclient.pool.metrics | `false` | Enables channel pools metrics to be collected and registered in Micrometer. Disabled by default. |spring.cloud.gateway.httpclient.pool.name | `proxy` | The channel pool map name, defaults to proxy. |spring.cloud.gateway.httpclient.pool.type | | Type of pool for HttpClient to use, defaults to ELASTIC. |spring.cloud.gateway.httpclient.proxy.host | | Hostname for proxy configuration of Netty HttpClient. diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java index 2ff02781..1feff526 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java @@ -752,6 +752,7 @@ public class GatewayAutoConfiguration { builder.maxLifeTime(pool.getMaxLifeTime()); } builder.evictInBackground(pool.getEvictionInterval()); + builder.metrics(pool.isMetrics()); connectionProvider = builder.build(); } return connectionProvider; diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java index a7054cd4..fbd67665 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java @@ -196,7 +196,7 @@ public class HttpClientProperties { */ private Integer maxConnections = ConnectionProvider.DEFAULT_POOL_MAX_CONNECTIONS; - /** Only for type FIXED, the maximum time in millis to wait for aquiring. */ + /** Only for type FIXED, the maximum time in millis to wait for acquiring. */ private Long acquireTimeout = ConnectionProvider.DEFAULT_POOL_ACQUIRE_TIMEOUT; /** @@ -217,6 +217,12 @@ public class HttpClientProperties { */ private Duration evictionInterval = Duration.ZERO; + /** + * Enables channel pools metrics to be collected and registered in Micrometer. + * Disabled by default. + */ + private boolean metrics = false; + public PoolType getType() { return type; } @@ -273,11 +279,19 @@ public class HttpClientProperties { this.evictionInterval = evictionInterval; } + public boolean isMetrics() { + return metrics; + } + + public void setMetrics(boolean metrics) { + this.metrics = metrics; + } + @Override public String toString() { return "Pool{" + "type=" + type + ", name='" + name + '\'' + ", maxConnections=" + maxConnections + ", acquireTimeout=" + acquireTimeout + ", maxIdleTime=" + maxIdleTime + ", maxLifeTime=" - + maxLifeTime + ", evictionInterval=" + evictionInterval + '}'; + + maxLifeTime + ", evictionInterval=" + evictionInterval + ", metrics=" + metrics + '}'; } public enum PoolType { diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayAutoConfigurationTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayAutoConfigurationTests.java index a39c91b9..3b5c6a71 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayAutoConfigurationTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayAutoConfigurationTests.java @@ -100,8 +100,9 @@ public class GatewayAutoConfigurationTests { "spring.cloud.gateway.httpclient.response-timeout=10s", "spring.cloud.gateway.httpclient.pool.eviction-interval=10s", "spring.cloud.gateway.httpclient.pool.type=fixed", + "spring.cloud.gateway.httpclient.pool.metrics=true", "spring.cloud.gateway.httpclient.compression=true", - // greather than integer max value + // greater than integer max value "spring.cloud.gateway.httpclient.max-initial-line-length=2147483647", "spring.cloud.gateway.httpclient.proxy.host=myhost", "spring.cloud.gateway.httpclient.websocket.max-frame-payload-length=1024") @@ -112,6 +113,7 @@ public class GatewayAutoConfigurationTests { assertThat(properties.getMaxInitialLineLength().toBytes()).isLessThanOrEqualTo(Integer.MAX_VALUE); assertThat(properties.isCompression()).isEqualTo(true); assertThat(properties.getPool().getEvictionInterval()).hasSeconds(10); + assertThat(properties.getPool().isMetrics()).isEqualTo(true); /* * FIXME: 2.1.0 HttpClientOptions options = httpClient.options(); *