Debian Codegraph @

src/test/java/com/zaxxer/hikari/metrics/prometheus/PrometheusMetricsTrackerTest.java

raw
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 com.zaxxer.hikari.HikariConfig;
20 import com.zaxxer.hikari.HikariDataSource;
21 import io.prometheus.client.CollectorRegistry;
22 import org.junit.Test;
23
24 import java.sql.Connection;
25 import java.sql.SQLTransientConnectionException;
26
27 import static com.zaxxer.hikari.pool.TestElf.newHikariConfig;
28 import static org.hamcrest.CoreMatchers.is;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertThat;
31
32 public class PrometheusMetricsTrackerTest {
33
34 private CollectorRegistry collectorRegistry = CollectorRegistry.defaultRegistry;
35
36 private static final String POOL_LABEL_NAME = "pool";
37 private static final String QUANTILE_LABEL_NAME = "quantile";
38 private static final String[] QUANTILE_LABEL_VALUES = new String[]{"0.5", "0.95", "0.99"};
39
40 @Test
41 public void recordConnectionTimeout() throws Exception {
42 HikariConfig config = newHikariConfig();
43 config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
44 config.setJdbcUrl("jdbc:h2:mem:");
45 config.setMaximumPoolSize(2);
46 config.setConnectionTimeout(250);
47
48 String[] labelNames = {POOL_LABEL_NAME};
49 String[] labelValues = {config.getPoolName()};
50
51 try (HikariDataSource hikariDataSource = new HikariDataSource(config)) {
52 try (Connection connection1 = hikariDataSource.getConnection();
53 Connection connection2 = hikariDataSource.getConnection()) {
54 try (Connection connection3 = hikariDataSource.getConnection()) {
55 } catch (SQLTransientConnectionException ignored) {
56 }
57 }
58
59 Double total = collectorRegistry.getSampleValue(
60 "hikaricp_connection_timeout_total",
61 labelNames,
62 labelValues
63 );
64 assertThat(total, is(1.0));
65 }
66 }
67
68 @Test
69 public void connectionAcquisitionMetrics() {
70 checkSummaryMetricFamily("hikaricp_connection_acquired_nanos");
71 }
72
73 @Test
74 public void connectionUsageMetrics() {
75 checkSummaryMetricFamily("hikaricp_connection_usage_millis");
76 }
77
78 @Test
79 public void connectionCreationMetrics() {
80 checkSummaryMetricFamily("hikaricp_connection_creation_millis");
81 }
82
83 @Test
84 public void testMultiplePoolName() throws Exception {
85 String[] labelNames = {POOL_LABEL_NAME};
86
87 HikariConfig config = newHikariConfig();
88 config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
89 config.setPoolName("first");
90 config.setJdbcUrl("jdbc:h2:mem:");
91 config.setMaximumPoolSize(2);
92 config.setConnectionTimeout(250);
93 String[] labelValues1 = {config.getPoolName()};
94
95 try (HikariDataSource ignored = new HikariDataSource(config)) {
96 assertThat(collectorRegistry.getSampleValue(
97 "hikaricp_connection_timeout_total",
98 labelNames,
99 labelValues1), is(0.0));
100
101 HikariConfig config2 = newHikariConfig();
102 config2.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
103 config2.setPoolName("second");
104 config2.setJdbcUrl("jdbc:h2:mem:");
105 config2.setMaximumPoolSize(4);
106 config2.setConnectionTimeout(250);
107 String[] labelValues2 = {config2.getPoolName()};
108
109 try (HikariDataSource ignored2 = new HikariDataSource(config2)) {
110 assertThat(collectorRegistry.getSampleValue(
111 "hikaricp_connection_timeout_total",
112 labelNames,
113 labelValues2), is(0.0));
114 }
115 }
116 }
117
118 private void checkSummaryMetricFamily(String metricName) {
119 HikariConfig config = newHikariConfig();
120 config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
121 config.setJdbcUrl("jdbc:h2:mem:");
122
123 try (HikariDataSource ignored = new HikariDataSource(config)) {
124 Double count = collectorRegistry.getSampleValue(
125 metricName + "_count",
126 new String[]{POOL_LABEL_NAME},
127 new String[]{config.getPoolName()}
128 );
129 assertNotNull(count);
130
131 Double sum = collectorRegistry.getSampleValue(
132 metricName + "_sum",
133 new String[]{POOL_LABEL_NAME},
134 new String[]{config.getPoolName()}
135 );
136 assertNotNull(sum);
137
138 for (String quantileLabelValue : QUANTILE_LABEL_VALUES) {
139 Double quantileValue = collectorRegistry.getSampleValue(
140 metricName,
141 new String[]{POOL_LABEL_NAME, QUANTILE_LABEL_NAME},
142 new String[]{config.getPoolName(), quantileLabelValue}
143 );
144 assertNotNull("q = " + quantileLabelValue, quantileValue);
145 }
146 }
147 }
148 }