|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.metrics.jdbc;
|
18 | 18 |
|
| 19 | +import java.sql.SQLException; |
19 | 20 | import java.util.UUID;
|
20 | 21 |
|
21 | 22 | import javax.sql.DataSource;
|
|
26 | 27 | import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
27 | 28 | import org.junit.Test;
|
28 | 29 |
|
| 30 | +import org.springframework.beans.BeansException; |
| 31 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
29 | 32 | import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
|
30 | 33 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
31 | 34 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
32 | 35 | import org.springframework.boot.jdbc.DataSourceBuilder;
|
33 | 36 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
34 | 37 | import org.springframework.context.annotation.Bean;
|
35 | 38 | import org.springframework.context.annotation.Configuration;
|
| 39 | +import org.springframework.core.Ordered; |
| 40 | +import org.springframework.core.PriorityOrdered; |
36 | 41 |
|
37 | 42 | import static org.assertj.core.api.Assertions.assertThat;
|
38 | 43 |
|
@@ -96,6 +101,29 @@ public void autoConfiguredHikariDataSourceIsInstrumented() {
|
96 | 101 | });
|
97 | 102 | }
|
98 | 103 |
|
| 104 | + @Test |
| 105 | + public void autoConfiguredHikariDataSourceIsInstrumentedWhenUsingDataSourceInitialization() { |
| 106 | + this.contextRunner |
| 107 | + .withPropertyValues( |
| 108 | + "spring.datasource.schema:db/create-custom-schema.sql") |
| 109 | + .run((context) -> { |
| 110 | + context.getBean(DataSource.class).getConnection(); |
| 111 | + MeterRegistry registry = context.getBean(MeterRegistry.class); |
| 112 | + registry.get("hikaricp.connections").meter(); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void failureToInstrumentHikariDataSourceIsTolerated() { |
| 118 | + this.contextRunner.withUserConfiguration(HikariSealingConfiguration.class) |
| 119 | + .run((context) -> { |
| 120 | + assertThat(context).hasNotFailed(); |
| 121 | + context.getBean(DataSource.class).getConnection(); |
| 122 | + MeterRegistry registry = context.getBean(MeterRegistry.class); |
| 123 | + assertThat(registry.find("hikaricp.connections").meter()).isNull(); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
99 | 127 | @Test
|
100 | 128 | public void hikariDataSourceInstrumentationCanBeDisabled() {
|
101 | 129 | this.contextRunner.withPropertyValues("management.metrics.enable.hikaricp=false")
|
@@ -215,4 +243,37 @@ private org.apache.tomcat.jdbc.pool.DataSource createTomcatDataSource() {
|
215 | 243 |
|
216 | 244 | }
|
217 | 245 |
|
| 246 | + @Configuration |
| 247 | + static class HikariSealingConfiguration { |
| 248 | + |
| 249 | + @Bean |
| 250 | + public static HikariSealer hikariSealer() { |
| 251 | + return new HikariSealer(); |
| 252 | + } |
| 253 | + |
| 254 | + static class HikariSealer implements BeanPostProcessor, PriorityOrdered { |
| 255 | + |
| 256 | + @Override |
| 257 | + public int getOrder() { |
| 258 | + return Ordered.HIGHEST_PRECEDENCE; |
| 259 | + } |
| 260 | + |
| 261 | + @Override |
| 262 | + public Object postProcessAfterInitialization(Object bean, String beanName) |
| 263 | + throws BeansException { |
| 264 | + if (bean instanceof HikariDataSource) { |
| 265 | + try { |
| 266 | + ((HikariDataSource) bean).getConnection().close(); |
| 267 | + } |
| 268 | + catch (SQLException ex) { |
| 269 | + throw new IllegalStateException(ex); |
| 270 | + } |
| 271 | + } |
| 272 | + return bean; |
| 273 | + } |
| 274 | + |
| 275 | + } |
| 276 | + |
| 277 | + } |
| 278 | + |
218 | 279 | }
|
0 commit comments