|
5 | 5 | import com.clickhouse.client.ClickHouseProtocol; |
6 | 6 | import com.clickhouse.client.ClickHouseServerForTest; |
7 | 7 | import com.clickhouse.client.api.Client; |
| 8 | +import com.clickhouse.client.api.ClientConfigProperties; |
8 | 9 | import com.clickhouse.client.api.enums.Protocol; |
9 | 10 | import com.clickhouse.client.api.internal.ServerSettings; |
| 11 | +import com.clickhouse.client.api.query.QueryResponse; |
| 12 | +import io.micrometer.core.instrument.Gauge; |
10 | 13 | import io.micrometer.core.instrument.MeterRegistry; |
11 | 14 | import io.micrometer.core.instrument.Metrics; |
12 | 15 | import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
| 16 | +import org.testng.Assert; |
13 | 17 | import org.testng.annotations.AfterMethod; |
14 | 18 | import org.testng.annotations.BeforeMethod; |
15 | 19 | import org.testng.annotations.Test; |
|
18 | 22 |
|
19 | 23 | public class MetricsTest extends BaseIntegrationTest { |
20 | 24 | private MeterRegistry meterRegistry; |
21 | | - @BeforeMethod |
| 25 | + @BeforeMethod(groups = {"integration"}) |
22 | 26 | void setUp() { |
23 | 27 | meterRegistry = new SimpleMeterRegistry(); |
24 | 28 | Metrics.globalRegistry.add(meterRegistry); |
25 | 29 | } |
26 | 30 |
|
27 | | - @AfterMethod |
| 31 | + @AfterMethod(groups = {"integration"}) |
28 | 32 | void tearDown() { |
29 | 33 | meterRegistry.clear(); |
30 | 34 | Metrics.globalRegistry.clear(); |
31 | 35 | } |
32 | 36 |
|
33 | 37 | @Test(groups = { "integration" }, enabled = true) |
34 | | - public void testRegisterMetrics() { |
| 38 | + public void testRegisterMetrics() throws Exception { |
35 | 39 | ClickHouseNode node = getServer(ClickHouseProtocol.HTTP); |
36 | 40 | boolean isSecure = isCloud(); |
37 | | - Client client = new Client.Builder() |
| 41 | + |
| 42 | + try (Client client = new Client.Builder() |
38 | 43 | .addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), isSecure) |
39 | 44 | .setUsername("default") |
40 | 45 | .setPassword(ClickHouseServerForTest.getPassword()) |
41 | 46 | .setDefaultDatabase(ClickHouseServerForTest.getDatabase()) |
42 | 47 | .serverSetting(ServerSettings.ASYNC_INSERT, "0") |
43 | 48 | .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 | + } |
47 | 69 | // currently there are only 5 metrics that are monitored by micrometer (out of the box) |
48 | 70 | assertEquals(meterRegistry.getMeters().size(), 5); |
49 | 71 | } |
|
0 commit comments