@Test public void multipleDataSources() { load(MultipleDataSourcesConfig.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); 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( this.context.getBean("hikariDS", DataSource.class)); jdbcTemplate.execute(new ConnectionCallback<Void>() { @Override 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"); }
@Test public void autoDataSource() { load(DataSourceAutoConfiguration.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage"); }
@Test public void multipleDataSourcesWithPrimary() { load(MultipleDataSourcesWithPrimaryConfig.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage", "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, CustomDataSourcePublicMetrics.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "ds.first.active", "ds.first.usage", "ds.second.active", "ds.second.usage"); }
@Bean public DataSourcePublicMetrics myDataSourcePublicMetrics() { return new DataSourcePublicMetrics() { @Override protected String createPrefix(String dataSourceName, DataSource dataSource, boolean primary) { return (primary ? "ds.first." : "ds.second"); } }; }
@Bean @ConditionalOnMissingBean @ConditionalOnBean(DataSourcePoolMetadataProvider.class) public DataSourcePublicMetrics dataSourcePublicMetrics() { return new DataSourcePublicMetrics(); }
@Test public void noDataSource() { load(); assertThat(this.context.getBeansOfType(DataSourcePublicMetrics.class)).isEmpty(); }
@Test public void noDataSource() { load(); assertEquals(0, this.context.getBeansOfType(DataSourcePublicMetrics.class).size()); }