diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/DataSourcePublicMetrics.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/DataSourcePublicMetrics.java index a0d1310692..b022fec7bf 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/DataSourcePublicMetrics.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/DataSourcePublicMetrics.java @@ -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; diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/PublicMetricsAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/PublicMetricsAutoConfigurationTests.java index e57c478c2f..5fdca7515a 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/PublicMetricsAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/PublicMetricsAutoConfigurationTests.java @@ -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> 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 {