Apply eclipse formatting rules to 3dc932db
This commit is contained in:
@@ -30,8 +30,8 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} that provides
|
||||
* metrics on dataSource usage.
|
||||
* {@link EnableAutoConfiguration Auto-configuration} that provides metrics on dataSource
|
||||
* usage.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -33,8 +34,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
/**
|
||||
* A {@link PublicMetrics} implementation that provides data source usage
|
||||
* statistics.
|
||||
* A {@link PublicMetrics} implementation that provides data source usage statistics.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
@@ -49,21 +49,23 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
||||
@Autowired
|
||||
private Collection<DataSourceMetadataProvider> dataSourceMetadataProviders;
|
||||
|
||||
private final Map<String, DataSourceMetadata> dataSourceMetadataByPrefix
|
||||
= new HashMap<String, DataSourceMetadata>();
|
||||
private final Map<String, DataSourceMetadata> dataSourceMetadataByPrefix = new HashMap<String, DataSourceMetadata>();
|
||||
|
||||
@PostConstruct
|
||||
public void initialize() {
|
||||
Map<String, DataSource> dataSources = this.applicationContext.getBeansOfType(DataSource.class);
|
||||
Map<String, DataSource> dataSources = this.applicationContext
|
||||
.getBeansOfType(DataSource.class);
|
||||
DataSource primaryDataSource = getPrimaryDataSource();
|
||||
|
||||
|
||||
DataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(this.dataSourceMetadataProviders);
|
||||
DataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(
|
||||
this.dataSourceMetadataProviders);
|
||||
for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
|
||||
String prefix = createPrefix(entry.getKey(), entry.getValue(), entry.getValue().equals(primaryDataSource));
|
||||
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(entry.getValue());
|
||||
String prefix = createPrefix(entry.getKey(), entry.getValue(), entry
|
||||
.getValue().equals(primaryDataSource));
|
||||
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(entry
|
||||
.getValue());
|
||||
if (dataSourceMetadata != null) {
|
||||
dataSourceMetadataByPrefix.put(prefix, dataSourceMetadata);
|
||||
this.dataSourceMetadataByPrefix.put(prefix, dataSourceMetadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,7 +73,8 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
||||
@Override
|
||||
public Collection<Metric<?>> metrics() {
|
||||
Collection<Metric<?>> result = new LinkedHashSet<Metric<?>>();
|
||||
for (Map.Entry<String, DataSourceMetadata> entry : dataSourceMetadataByPrefix.entrySet()) {
|
||||
for (Map.Entry<String, DataSourceMetadata> entry : this.dataSourceMetadataByPrefix
|
||||
.entrySet()) {
|
||||
String prefix = entry.getKey();
|
||||
// Make sure the prefix ends with a dot
|
||||
if (!prefix.endsWith(".")) {
|
||||
@@ -91,19 +94,23 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the prefix to use for the metrics to associate with the given {@link DataSource}.
|
||||
* Create the prefix to use for the metrics to associate with the given
|
||||
* {@link DataSource}.
|
||||
* @param dataSourceName the name of the data source bean
|
||||
* @param dataSource the data source to configure
|
||||
* @param primary if this data source is the primary data source
|
||||
* @return a prefix for the given data source
|
||||
*/
|
||||
protected String createPrefix(String dataSourceName, DataSource dataSource, boolean primary) {
|
||||
protected String createPrefix(String dataSourceName, DataSource dataSource,
|
||||
boolean primary) {
|
||||
StringBuilder sb = new StringBuilder("datasource.");
|
||||
if (primary) {
|
||||
sb.append("primary");
|
||||
}
|
||||
else if (endWithDataSource(dataSourceName)) { // Strip the data source part out of the name
|
||||
sb.append(dataSourceName.substring(0, dataSourceName.length() - DATASOURCE_SUFFIX.length()));
|
||||
else if (endWithDataSource(dataSourceName)) { // Strip the data source part out of
|
||||
// the name
|
||||
sb.append(dataSourceName.substring(0, dataSourceName.length()
|
||||
- DATASOURCE_SUFFIX.length()));
|
||||
}
|
||||
else {
|
||||
sb.append(dataSourceName);
|
||||
@@ -131,7 +138,7 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
||||
*/
|
||||
private DataSource getPrimaryDataSource() {
|
||||
try {
|
||||
return applicationContext.getBean(DataSource.class);
|
||||
return this.applicationContext.getBean(DataSource.class);
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException e) {
|
||||
return null;
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
@@ -26,11 +24,9 @@ import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics;
|
||||
import org.springframework.boot.actuate.endpoint.PublicMetrics;
|
||||
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.JdbcTemplate;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class MetricDataSourceAutoConfigurationTests {
|
||||
@@ -78,24 +78,25 @@ public class MetricDataSourceAutoConfigurationTests {
|
||||
load(MultipleDataSourcesConfig.class);
|
||||
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
||||
Collection<Metric<?>> metrics = bean.metrics();
|
||||
assertMetrics(metrics,
|
||||
"datasource.tomcat.active", "datasource.tomcat.usage",
|
||||
assertMetrics(metrics, "datasource.tomcat.active", "datasource.tomcat.usage",
|
||||
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
||||
|
||||
// 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>() {
|
||||
@Override
|
||||
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
|
||||
public Void doInConnection(Connection connection) throws SQLException,
|
||||
DataAccessException {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
Collection<Metric<?>> anotherMetrics = bean.metrics();
|
||||
assertMetrics(anotherMetrics,
|
||||
"datasource.tomcat.active", "datasource.tomcat.usage",
|
||||
"datasource.hikariDS.active", "datasource.hikariDS.usage",
|
||||
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
||||
assertMetrics(anotherMetrics, "datasource.tomcat.active",
|
||||
"datasource.tomcat.usage", "datasource.hikariDS.active",
|
||||
"datasource.hikariDS.usage", "datasource.commonsDbcp.active",
|
||||
"datasource.commonsDbcp.usage");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -103,19 +104,18 @@ public class MetricDataSourceAutoConfigurationTests {
|
||||
load(MultipleDataSourcesWithPrimaryConfig.class);
|
||||
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
||||
Collection<Metric<?>> metrics = bean.metrics();
|
||||
assertMetrics(metrics,
|
||||
"datasource.primary.active", "datasource.primary.usage",
|
||||
assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage",
|
||||
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customPrefix() {
|
||||
load(MultipleDataSourcesWithPrimaryConfig.class, CustomDataSourcePublicMetrics.class);
|
||||
load(MultipleDataSourcesWithPrimaryConfig.class,
|
||||
CustomDataSourcePublicMetrics.class);
|
||||
PublicMetrics bean = this.context.getBean(PublicMetrics.class);
|
||||
Collection<Metric<?>> metrics = bean.metrics();
|
||||
assertMetrics(metrics,
|
||||
"ds.first.active", "ds.first.usage",
|
||||
"ds.second.active", "ds.second.usage");
|
||||
assertMetrics(metrics, "ds.first.active", "ds.first.usage", "ds.second.active",
|
||||
"ds.second.usage");
|
||||
|
||||
}
|
||||
|
||||
@@ -138,13 +138,13 @@ public class MetricDataSourceAutoConfigurationTests {
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
static class MultipleDataSourcesConfig {
|
||||
|
||||
@Bean
|
||||
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
|
||||
@@ -164,7 +164,8 @@ public class MetricDataSourceAutoConfigurationTests {
|
||||
@Bean
|
||||
@Primary
|
||||
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
|
||||
@@ -180,7 +181,8 @@ public class MetricDataSourceAutoConfigurationTests {
|
||||
public DataSourcePublicMetrics myDataSourcePublicMetrics() {
|
||||
return new DataSourcePublicMetrics() {
|
||||
@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");
|
||||
}
|
||||
};
|
||||
@@ -188,9 +190,7 @@ public class MetricDataSourceAutoConfigurationTests {
|
||||
}
|
||||
|
||||
private static DataSourceBuilder initializeBuilder() {
|
||||
return DataSourceBuilder.create()
|
||||
.driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
||||
.url("jdbc:hsqldb:mem:test")
|
||||
.username("sa");
|
||||
return DataSourceBuilder.create().driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
||||
.url("jdbc:hsqldb:mem:test").username("sa");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user