|
|
@ -16,8 +16,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.actuate.autoconfigure;
|
|
|
|
package org.springframework.boot.actuate.autoconfigure;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collection;
|
|
|
@ -26,11 +24,9 @@ import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
|
|
|
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
|
|
|
|
|
|
import org.apache.commons.dbcp.BasicDataSource;
|
|
|
|
import org.apache.commons.dbcp.BasicDataSource;
|
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics;
|
|
|
|
import org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics;
|
|
|
|
import org.springframework.boot.actuate.endpoint.PublicMetrics;
|
|
|
|
import org.springframework.boot.actuate.endpoint.PublicMetrics;
|
|
|
|
import org.springframework.boot.actuate.metrics.Metric;
|
|
|
|
import org.springframework.boot.actuate.metrics.Metric;
|
|
|
@ -44,8 +40,12 @@ import org.springframework.dao.DataAccessException;
|
|
|
|
import org.springframework.jdbc.core.ConnectionCallback;
|
|
|
|
import org.springframework.jdbc.core.ConnectionCallback;
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class MetricDataSourceAutoConfigurationTests {
|
|
|
|
public class MetricDataSourceAutoConfigurationTests {
|
|
|
@ -78,24 +78,25 @@ public class MetricDataSourceAutoConfigurationTests {
|
|
|
|
load(MultipleDataSourcesConfig.class);
|
|
|
|
load(MultipleDataSourcesConfig.class);
|
|
|
|
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
|
|
|
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
|
|
|
Collection<Metric<?>> metrics = bean.metrics();
|
|
|
|
Collection<Metric<?>> metrics = bean.metrics();
|
|
|
|
assertMetrics(metrics,
|
|
|
|
assertMetrics(metrics, "datasource.tomcat.active", "datasource.tomcat.usage",
|
|
|
|
"datasource.tomcat.active", "datasource.tomcat.usage",
|
|
|
|
|
|
|
|
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
|
|
|
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
|
|
|
|
|
|
|
|
|
|
|
// Hikari won't work unless a first connection has been retrieved
|
|
|
|
// Hikari won't work unless a first connection has been retrieved
|
|
|
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(context.getBean("hikariDS", DataSource.class));
|
|
|
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.context.getBean("hikariDS",
|
|
|
|
|
|
|
|
DataSource.class));
|
|
|
|
jdbcTemplate.execute(new ConnectionCallback<Void>() {
|
|
|
|
jdbcTemplate.execute(new ConnectionCallback<Void>() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
|
|
|
|
public Void doInConnection(Connection connection) throws SQLException,
|
|
|
|
|
|
|
|
DataAccessException {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Collection<Metric<?>> anotherMetrics = bean.metrics();
|
|
|
|
Collection<Metric<?>> anotherMetrics = bean.metrics();
|
|
|
|
assertMetrics(anotherMetrics,
|
|
|
|
assertMetrics(anotherMetrics, "datasource.tomcat.active",
|
|
|
|
"datasource.tomcat.active", "datasource.tomcat.usage",
|
|
|
|
"datasource.tomcat.usage", "datasource.hikariDS.active",
|
|
|
|
"datasource.hikariDS.active", "datasource.hikariDS.usage",
|
|
|
|
"datasource.hikariDS.usage", "datasource.commonsDbcp.active",
|
|
|
|
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
|
|
|
"datasource.commonsDbcp.usage");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -103,19 +104,18 @@ public class MetricDataSourceAutoConfigurationTests {
|
|
|
|
load(MultipleDataSourcesWithPrimaryConfig.class);
|
|
|
|
load(MultipleDataSourcesWithPrimaryConfig.class);
|
|
|
|
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
|
|
|
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
|
|
|
Collection<Metric<?>> metrics = bean.metrics();
|
|
|
|
Collection<Metric<?>> metrics = bean.metrics();
|
|
|
|
assertMetrics(metrics,
|
|
|
|
assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage",
|
|
|
|
"datasource.primary.active", "datasource.primary.usage",
|
|
|
|
|
|
|
|
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
|
|
|
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void customPrefix() {
|
|
|
|
public void customPrefix() {
|
|
|
|
load(MultipleDataSourcesWithPrimaryConfig.class, CustomDataSourcePublicMetrics.class);
|
|
|
|
load(MultipleDataSourcesWithPrimaryConfig.class,
|
|
|
|
|
|
|
|
CustomDataSourcePublicMetrics.class);
|
|
|
|
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
|
|
|
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
|
|
|
Collection<Metric<?>> metrics = bean.metrics();
|
|
|
|
Collection<Metric<?>> metrics = bean.metrics();
|
|
|
|
assertMetrics(metrics,
|
|
|
|
assertMetrics(metrics, "ds.first.active", "ds.first.usage", "ds.second.active",
|
|
|
|
"ds.first.active", "ds.first.usage",
|
|
|
|
"ds.second.usage");
|
|
|
|
"ds.second.active", "ds.second.usage");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -138,13 +138,13 @@ public class MetricDataSourceAutoConfigurationTests {
|
|
|
|
this.context.refresh();
|
|
|
|
this.context.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
@Configuration
|
|
|
|
static class MultipleDataSourcesConfig {
|
|
|
|
static class MultipleDataSourcesConfig {
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public DataSource tomcatDataSource() {
|
|
|
|
public DataSource tomcatDataSource() {
|
|
|
|
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class).build();
|
|
|
|
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class)
|
|
|
|
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
@ -164,7 +164,8 @@ public class MetricDataSourceAutoConfigurationTests {
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
@Primary
|
|
|
|
@Primary
|
|
|
|
public DataSource myDataSource() {
|
|
|
|
public DataSource myDataSource() {
|
|
|
|
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class).build();
|
|
|
|
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class)
|
|
|
|
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
@ -180,7 +181,8 @@ public class MetricDataSourceAutoConfigurationTests {
|
|
|
|
public DataSourcePublicMetrics myDataSourcePublicMetrics() {
|
|
|
|
public DataSourcePublicMetrics myDataSourcePublicMetrics() {
|
|
|
|
return new DataSourcePublicMetrics() {
|
|
|
|
return new DataSourcePublicMetrics() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected String createPrefix(String dataSourceName, DataSource dataSource, boolean primary) {
|
|
|
|
protected String createPrefix(String dataSourceName,
|
|
|
|
|
|
|
|
DataSource dataSource, boolean primary) {
|
|
|
|
return (primary ? "ds.first." : "ds.second");
|
|
|
|
return (primary ? "ds.first." : "ds.second");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -188,9 +190,7 @@ public class MetricDataSourceAutoConfigurationTests {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static DataSourceBuilder initializeBuilder() {
|
|
|
|
private static DataSourceBuilder initializeBuilder() {
|
|
|
|
return DataSourceBuilder.create()
|
|
|
|
return DataSourceBuilder.create().driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
|
|
|
.driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
|
|
|
.url("jdbc:hsqldb:mem:test").username("sa");
|
|
|
|
.url("jdbc:hsqldb:mem:test")
|
|
|
|
|
|
|
|
.username("sa");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|