| 1 |
/* |
| 2 |
* Copyright (C) 2013, 2014 Brett Wooldridge |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.zaxxer.hikari.metrics.prometheus; |
| 18 |
|
| 19 |
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig; |
| 20 |
import static com.zaxxer.hikari.util.UtilityElf.quietlySleep; |
| 21 |
import static org.hamcrest.CoreMatchers.is; |
| 22 |
import static org.junit.Assert.assertThat; |
| 23 |
|
| 24 |
import java.sql.Connection; |
| 25 |
|
| 26 |
import org.junit.Test; |
| 27 |
|
| 28 |
import com.zaxxer.hikari.HikariConfig; |
| 29 |
import com.zaxxer.hikari.HikariDataSource; |
| 30 |
import com.zaxxer.hikari.mocks.StubConnection; |
| 31 |
|
| 32 |
import io.prometheus.client.CollectorRegistry; |
| 33 |
|
| 34 |
public class HikariCPCollectorTest { |
| 35 |
@Test |
| 36 |
public void noConnection() throws Exception { |
| 37 |
HikariConfig config = newHikariConfig(); |
| 38 |
config.setMinimumIdle(0); |
| 39 |
config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory()); |
| 40 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 41 |
|
| 42 |
StubConnection.slowCreate = true; |
| 43 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 44 |
assertThat(getValue("hikaricp_active_connections", "noConnection"), is(0.0)); |
| 45 |
assertThat(getValue("hikaricp_idle_connections", "noConnection"), is(0.0)); |
| 46 |
assertThat(getValue("hikaricp_pending_threads", "noConnection"), is(0.0)); |
| 47 |
assertThat(getValue("hikaricp_connections", "noConnection"), is(0.0)); |
| 48 |
assertThat(getValue("hikaricp_max_connections", "noConnection"), is(10.0)); |
| 49 |
assertThat(getValue("hikaricp_min_connections", "noConnection"), is(0.0)); |
| 50 |
} |
| 51 |
finally { |
| 52 |
StubConnection.slowCreate = false; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void noConnectionWithoutPoolName() throws Exception { |
| 58 |
HikariConfig config = new HikariConfig(); |
| 59 |
config.setMinimumIdle(0); |
| 60 |
config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory()); |
| 61 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 62 |
|
| 63 |
StubConnection.slowCreate = true; |
| 64 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 65 |
String poolName = ds.getHikariConfigMXBean().getPoolName(); |
| 66 |
assertThat(getValue("hikaricp_active_connections", poolName), is(0.0)); |
| 67 |
assertThat(getValue("hikaricp_idle_connections", poolName), is(0.0)); |
| 68 |
assertThat(getValue("hikaricp_pending_threads", poolName), is(0.0)); |
| 69 |
assertThat(getValue("hikaricp_connections", poolName), is(0.0)); |
| 70 |
assertThat(getValue("hikaricp_max_connections", poolName), is(10.0)); |
| 71 |
assertThat(getValue("hikaricp_min_connections", poolName), is(0.0)); |
| 72 |
} |
| 73 |
finally { |
| 74 |
StubConnection.slowCreate = false; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void connection1() throws Exception { |
| 80 |
HikariConfig config = newHikariConfig(); |
| 81 |
config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory()); |
| 82 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 83 |
config.setMaximumPoolSize(1); |
| 84 |
|
| 85 |
StubConnection.slowCreate = true; |
| 86 |
try (HikariDataSource ds = new HikariDataSource(config); |
| 87 |
Connection connection1 = ds.getConnection()) { |
| 88 |
|
| 89 |
quietlySleep(1000); |
| 90 |
|
| 91 |
assertThat(getValue("hikaricp_active_connections", "connection1"), is(1.0)); |
| 92 |
assertThat(getValue("hikaricp_idle_connections", "connection1"), is(0.0)); |
| 93 |
assertThat(getValue("hikaricp_pending_threads", "connection1"), is(0.0)); |
| 94 |
assertThat(getValue("hikaricp_connections", "connection1"), is(1.0)); |
| 95 |
assertThat(getValue("hikaricp_max_connections", "connection1"), is(1.0)); |
| 96 |
assertThat(getValue("hikaricp_min_connections", "connection1"), is(1.0)); |
| 97 |
} |
| 98 |
finally { |
| 99 |
StubConnection.slowCreate = false; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void connectionClosed() throws Exception { |
| 105 |
HikariConfig config = newHikariConfig(); |
| 106 |
config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory()); |
| 107 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 108 |
config.setMaximumPoolSize(1); |
| 109 |
|
| 110 |
StubConnection.slowCreate = true; |
| 111 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 112 |
try (Connection connection1 = ds.getConnection()) { |
| 113 |
// close immediately |
| 114 |
} |
| 115 |
|
| 116 |
assertThat(getValue("hikaricp_active_connections", "connectionClosed"), is(0.0)); |
| 117 |
assertThat(getValue("hikaricp_idle_connections", "connectionClosed"), is(1.0)); |
| 118 |
assertThat(getValue("hikaricp_pending_threads", "connectionClosed"), is(0.0)); |
| 119 |
assertThat(getValue("hikaricp_connections", "connectionClosed"), is(1.0)); |
| 120 |
assertThat(getValue("hikaricp_max_connections", "connectionClosed"), is(1.0)); |
| 121 |
assertThat(getValue("hikaricp_min_connections", "connectionClosed"), is(1.0)); |
| 122 |
} |
| 123 |
finally { |
| 124 |
StubConnection.slowCreate = false; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
private double getValue(String name, String poolName) { |
| 129 |
String[] labelNames = {"pool"}; |
| 130 |
String[] labelValues = {poolName}; |
| 131 |
return CollectorRegistry.defaultRegistry.getSampleValue(name, labelNames, labelValues); |
| 132 |
} |
| 133 |
|
| 134 |
} |