Skip to content

Commit 9a6aad4

Browse files
committed
added more test assertions. fixed tests for CI
1 parent cd45d8c commit 9a6aad4

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

client-v2/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@
141141
<scope>test</scope>
142142
</dependency>
143143

144-
<!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-observation -->
145144
<dependency>
146145
<groupId>io.micrometer</groupId>
147146
<artifactId>micrometer-observation</artifactId>

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,10 @@
4040
import com.clickhouse.data.ClickHouseColumn;
4141
import com.clickhouse.data.ClickHouseFormat;
4242
import io.micrometer.core.instrument.MeterRegistry;
43-
import org.apache.hc.client5.http.ConnectTimeoutException;
4443
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
4544
import org.apache.hc.core5.http.ClassicHttpResponse;
46-
import org.apache.hc.core5.http.ConnectionRequestTimeoutException;
4745
import org.apache.hc.core5.http.HttpHeaders;
4846
import org.apache.hc.core5.http.HttpStatus;
49-
import org.apache.hc.core5.http.NoHttpResponseException;
5047
import org.slf4j.Logger;
5148
import org.slf4j.LoggerFactory;
5249

@@ -55,8 +52,6 @@
5552
import java.io.OutputStream;
5653
import java.lang.reflect.InvocationTargetException;
5754
import java.lang.reflect.Method;
58-
import java.net.ConnectException;
59-
import java.net.SocketTimeoutException;
6055
import java.net.URL;
6156
import java.nio.charset.StandardCharsets;
6257
import java.time.Duration;
@@ -957,12 +952,20 @@ public Builder useBearerTokenAuth(String bearerToken) {
957952
this.httpHeader(HttpHeaders.AUTHORIZATION, "Bearer " + bearerToken);
958953
return this;
959954
}
960-
public Builder addMetric(Object metric, String name) {
961-
if (metric instanceof MeterRegistry) {
962-
this.metric = metric;
963-
this.configuration.put(ClientConfigProperties.METRICS_NAME.getKey(), name);
955+
956+
/**
957+
* Registers http client metrics with MeterRegistry.
958+
*
959+
* @param registry - metrics registry
960+
* @param name - name of metrics group
961+
* @return same instance of the builder
962+
*/
963+
public Builder registerClientMetrics(Object registry, String name) {
964+
if (registry instanceof MeterRegistry) {
965+
this.metric = registry;
966+
this.configuration.put(ClientConfigProperties.METRICS_GROUP_NAME.getKey(), name);
964967
} else {
965-
throw new IllegalArgumentException("Unsupported metric type. Only MeterRegistry is supported");
968+
throw new IllegalArgumentException("Unsupported registry type." + registry.getClass());
966969
}
967970
return this;
968971
}

client-v2/src/main/java/com/clickhouse/client/api/ClientConfigProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public enum ClientConfigProperties {
130130
/**
131131
*
132132
*/
133-
METRICS_NAME("metrics_name"),
133+
METRICS_GROUP_NAME("metrics_name"),
134134
;
135135

136136
private String key;

client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private HttpClientConnectionManager poolConnectionManager(SSLContext sslContext,
226226
connMgrBuilder.setDefaultSocketConfig(socketConfig);
227227
PoolingHttpClientConnectionManager phccm = connMgrBuilder.build();
228228
if (metric != null && metric instanceof MeterRegistry) {
229-
String name = chConfiguration.getOrDefault(ClientConfigProperties.METRICS_NAME.getKey(), "http-pool");
229+
String name = chConfiguration.getOrDefault(ClientConfigProperties.METRICS_GROUP_NAME.getKey(), "http-pool");
230230
new PoolingHttpClientConnectionManagerMetricsBinder(phccm, name).bindTo((MeterRegistry) metric);
231231
}
232232
return phccm;

client-v2/src/test/java/com/clickhouse/client/metrics/MetricsTest.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
import com.clickhouse.client.ClickHouseProtocol;
66
import com.clickhouse.client.ClickHouseServerForTest;
77
import com.clickhouse.client.api.Client;
8+
import com.clickhouse.client.api.ClientConfigProperties;
89
import com.clickhouse.client.api.enums.Protocol;
910
import com.clickhouse.client.api.internal.ServerSettings;
11+
import com.clickhouse.client.api.query.QueryResponse;
12+
import io.micrometer.core.instrument.Gauge;
1013
import io.micrometer.core.instrument.MeterRegistry;
1114
import io.micrometer.core.instrument.Metrics;
1215
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
16+
import org.testng.Assert;
1317
import org.testng.annotations.AfterMethod;
1418
import org.testng.annotations.BeforeMethod;
1519
import org.testng.annotations.Test;
@@ -18,32 +22,50 @@
1822

1923
public class MetricsTest extends BaseIntegrationTest {
2024
private MeterRegistry meterRegistry;
21-
@BeforeMethod
25+
@BeforeMethod(groups = {"integration"})
2226
void setUp() {
2327
meterRegistry = new SimpleMeterRegistry();
2428
Metrics.globalRegistry.add(meterRegistry);
2529
}
2630

27-
@AfterMethod
31+
@AfterMethod(groups = {"integration"})
2832
void tearDown() {
2933
meterRegistry.clear();
3034
Metrics.globalRegistry.clear();
3135
}
3236

3337
@Test(groups = { "integration" }, enabled = true)
34-
public void testRegisterMetrics() {
38+
public void testRegisterMetrics() throws Exception {
3539
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
3640
boolean isSecure = isCloud();
37-
Client client = new Client.Builder()
41+
42+
try (Client client = new Client.Builder()
3843
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), isSecure)
3944
.setUsername("default")
4045
.setPassword(ClickHouseServerForTest.getPassword())
4146
.setDefaultDatabase(ClickHouseServerForTest.getDatabase())
4247
.serverSetting(ServerSettings.ASYNC_INSERT, "0")
4348
.serverSetting(ServerSettings.WAIT_END_OF_QUERY, "1")
44-
.useNewImplementation(System.getProperty("client.tests.useNewImplementation", "true").equals("true"))
45-
.addMetric(meterRegistry, "pool-test")
46-
.build();
49+
.registerClientMetrics(meterRegistry, "pool-test")
50+
.build()) {
51+
52+
Gauge totalMax = meterRegistry.get("httpcomponents.httpclient.pool.total.max").gauge();
53+
Gauge available = meterRegistry.get("httpcomponents.httpclient.pool.total.connections").tags("state", "available").gauge();
54+
Gauge leased = meterRegistry.get("httpcomponents.httpclient.pool.total.connections").tags("state", "leased").gauge();
55+
56+
System.out.println("totalMax:" + totalMax.value() + ", available: " + available.value() + ", leased: " + leased.value());
57+
Assert.assertEquals((int)totalMax.value(), Integer.parseInt(ClientConfigProperties.HTTP_MAX_OPEN_CONNECTIONS.getDefaultValue()));
58+
Assert.assertEquals((int)available.value(), 1);
59+
Assert.assertEquals((int)leased.value(), 0);
60+
61+
try (QueryResponse response = client.query("SELECT 1").get()) {
62+
Assert.assertEquals((int)available.value(), 0);
63+
Assert.assertEquals((int)leased.value(), 1);
64+
}
65+
66+
Assert.assertEquals((int)available.value(), 1);
67+
Assert.assertEquals((int)leased.value(), 0);
68+
}
4769
// currently there are only 5 metrics that are monitored by micrometer (out of the box)
4870
assertEquals(meterRegistry.getMeters().size(), 5);
4971
}

0 commit comments

Comments
 (0)