Fix for empty datasource-name in metrics

If there is more than one DataSource and the non-primary bean-name is
'datasource' an incorrect metric name is chosen.

The metrics are named datasource.active and not datasource.xxx.active. To
avoid this, the shortening of the bean-name only occurs if the bean-name
is longer than 'datasource'.

See gh-2320
pull/2150/merge
Johannes Stelzer 10 years ago committed by Stephane Nicoll
parent a8726c4ae1
commit 05e388012d

@ -102,7 +102,8 @@ public class DataSourcePublicMetrics implements PublicMetrics {
if (primary) {
return "datasource.primary";
}
if (name.toLowerCase().endsWith(DATASOURCE_SUFFIX.toLowerCase())) {
if (name.length() > DATASOURCE_SUFFIX.length()
&& name.toLowerCase().endsWith(DATASOURCE_SUFFIX.toLowerCase())) {
name = name.substring(0, name.length() - DATASOURCE_SUFFIX.length());
}
return "datasource." + name;

@ -168,6 +168,15 @@ public class PublicMetricsAutoConfigurationTests {
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
}
@Test
public void multipleDataSourcesWithCustomPrimary() {
load(MultipleDataSourcesWithCustomPrimaryConfig.class);
PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class);
Collection<Metric<?>> metrics = bean.metrics();
assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage",
"datasource.dataSource.active", "datasource.dataSource.usage");
}
@Test
public void customPrefix() {
load(MultipleDataSourcesWithPrimaryConfig.class,
@ -250,6 +259,22 @@ public class PublicMetricsAutoConfigurationTests {
}
}
@Configuration
static class MultipleDataSourcesWithCustomPrimaryConfig {
@Bean
@Primary
public DataSource myDataSource() {
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class)
.build();
}
@Bean
public DataSource dataSource() {
return initializeBuilder().type(BasicDataSource.class).build();
}
}
@Configuration
static class CustomDataSourcePublicMetrics {

Loading…
Cancel
Save