diff --git a/dhis-2/dhis-support/dhis-support-system/pom.xml b/dhis-2/dhis-support/dhis-support-system/pom.xml index 96b431afe834..bb78e65791f0 100644 --- a/dhis-2/dhis-support/dhis-support-system/pom.xml +++ b/dhis-2/dhis-support/dhis-support-system/pom.xml @@ -254,6 +254,10 @@ org.apache.httpcomponents httpcore + + com.zaxxer + HikariCP + diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/DataSourcePoolMetricsConfig.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/DataSourcePoolMetricsConfig.java index 220ee4978d44..9263ecdd64cd 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/DataSourcePoolMetricsConfig.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/DataSourcePoolMetricsConfig.java @@ -29,18 +29,20 @@ import static org.hisp.dhis.external.conf.ConfigurationKey.MONITORING_DBPOOL_ENABLED; -import com.google.common.collect.Lists; import com.mchange.v2.c3p0.ComboPooledDataSource; +import com.zaxxer.hikari.HikariDataSource; import io.micrometer.core.instrument.MeterRegistry; import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Map; import javax.sql.DataSource; import org.apache.commons.lang3.StringUtils; import org.hisp.dhis.external.conf.ConfigurationKey; import org.hisp.dhis.monitoring.metrics.jdbc.C3p0MetadataProvider; -import org.hisp.dhis.monitoring.metrics.jdbc.DataSourcePoolMetadataProvider; -import org.hisp.dhis.monitoring.metrics.jdbc.DataSourcePoolMetrics; +import org.hisp.dhis.monitoring.metrics.jdbc.HikariMetadataProvider; +import org.hisp.dhis.monitoring.metrics.jdbc.PoolMetadataProvider; +import org.hisp.dhis.monitoring.metrics.jdbc.PoolMetrics; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; @@ -59,10 +61,10 @@ static class DataSourcePoolMetadataMetricsConfiguration { private final MeterRegistry registry; - private final Collection metadataProviders; + private final Collection metadataProviders; DataSourcePoolMetadataMetricsConfiguration( - MeterRegistry registry, Collection metadataProviders) { + MeterRegistry registry, Collection metadataProviders) { this.registry = registry; this.metadataProviders = metadataProviders; } @@ -74,8 +76,7 @@ public void bindDataSourcesToRegistry(Map dataSources) { private void bindDataSourceToRegistry(String beanName, DataSource dataSource) { String dataSourceName = getDataSourceName(beanName); - new DataSourcePoolMetrics( - dataSource, this.metadataProviders, dataSourceName, Collections.emptyList()) + new PoolMetrics(dataSource, this.metadataProviders, dataSourceName, Collections.emptyList()) .bindTo(this.registry); } @@ -95,11 +96,18 @@ private String getDataSourceName(String beanName) { } @Bean - public Collection dataSourceMetadataProvider() { - DataSourcePoolMetadataProvider provider = - dataSource -> new C3p0MetadataProvider((ComboPooledDataSource) dataSource); - - return Lists.newArrayList(provider); + public Collection dataSourceMetadataProvider() { + return List.of( + dataSource -> { + if (dataSource instanceof ComboPooledDataSource comboPooledDataSource) { + return new C3p0MetadataProvider(comboPooledDataSource); + } else if (dataSource instanceof HikariDataSource hikariDataSource) { + return new HikariMetadataProvider(hikariDataSource); + } else { + throw new IllegalArgumentException( + "Unsupported DataSource type: " + dataSource.getClass().getName()); + } + }); } static class DataSourcePoolMetricsEnabledCondition extends MetricsEnabler { diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/AbstractDataSourcePoolMetadata.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/AbstractPoolMetadata.java similarity index 90% rename from dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/AbstractDataSourcePoolMetadata.java rename to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/AbstractPoolMetadata.java index 787f90478476..56f1d2a978f6 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/AbstractDataSourcePoolMetadata.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/AbstractPoolMetadata.java @@ -30,14 +30,13 @@ import javax.sql.DataSource; /** - * A base {@link DataSourcePoolMetadata} implementation. + * A base {@link PoolMetadata} implementation. * * @param the data source type * @author Stephane Nicoll * @since 2.0.0 */ -public abstract class AbstractDataSourcePoolMetadata - implements DataSourcePoolMetadata { +public abstract class AbstractPoolMetadata implements PoolMetadata { private final T dataSource; @@ -46,7 +45,7 @@ public abstract class AbstractDataSourcePoolMetadata * * @param dataSource the data source */ - protected AbstractDataSourcePoolMetadata(T dataSource) { + protected AbstractPoolMetadata(T dataSource) { this.dataSource = dataSource; } diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/C3p0MetadataProvider.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/C3p0MetadataProvider.java index 1104e812705d..46bbb7fbb5cd 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/C3p0MetadataProvider.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/C3p0MetadataProvider.java @@ -35,7 +35,7 @@ * @author Luciano Fiandesio */ @Slf4j -public class C3p0MetadataProvider extends AbstractDataSourcePoolMetadata { +public class C3p0MetadataProvider extends AbstractPoolMetadata { /** * Create an instance with the data source to use. * diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/CompositeDataSourcePoolMetadataProvider.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/CompositePoolMetadataProvider.java similarity index 72% rename from dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/CompositeDataSourcePoolMetadataProvider.java rename to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/CompositePoolMetadataProvider.java index 33eaff860991..67739d46f3b1 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/CompositeDataSourcePoolMetadataProvider.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/CompositePoolMetadataProvider.java @@ -34,23 +34,22 @@ import javax.sql.DataSource; /** - * A {@link DataSourcePoolMetadataProvider} implementation that returns the first {@link - * DataSourcePoolMetadata} that is found by one of its delegate. + * A {@link PoolMetadataProvider} implementation that returns the first {@link PoolMetadata} that is + * found by one of its delegate. * * @author Stephane Nicoll * @since 2.0.0 */ -public class CompositeDataSourcePoolMetadataProvider implements DataSourcePoolMetadataProvider { - private final List providers; +public class CompositePoolMetadataProvider implements PoolMetadataProvider { + private final List providers; /** - * Create a {@link CompositeDataSourcePoolMetadataProvider} instance with an initial collection of - * delegates to use. + * Create a {@link CompositePoolMetadataProvider} instance with an initial collection of delegates + * to use. * * @param providers the data source pool metadata providers */ - public CompositeDataSourcePoolMetadataProvider( - Collection providers) { + public CompositePoolMetadataProvider(Collection providers) { this.providers = (providers != null) ? Collections.unmodifiableList(new ArrayList<>(providers)) @@ -58,9 +57,9 @@ public CompositeDataSourcePoolMetadataProvider( } @Override - public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) { - for (DataSourcePoolMetadataProvider provider : this.providers) { - DataSourcePoolMetadata metadata = provider.getDataSourcePoolMetadata(dataSource); + public PoolMetadata getDataSourcePoolMetadata(DataSource dataSource) { + for (PoolMetadataProvider provider : this.providers) { + PoolMetadata metadata = provider.getDataSourcePoolMetadata(dataSource); if (metadata != null) { return metadata; } diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/HikariMetadataProvider.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/HikariMetadataProvider.java new file mode 100644 index 000000000000..217e3304f5dd --- /dev/null +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/HikariMetadataProvider.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2004-2022, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.monitoring.metrics.jdbc; + +import com.zaxxer.hikari.HikariDataSource; + +/** + * @author Morten Svanæs + */ +public class HikariMetadataProvider extends AbstractPoolMetadata { + + private HikariPoolMetadataAccessor hikariDataSourcePoolMetadata; + + /** + * Create an instance with the data source to use. + * + * @param dataSource the data source + */ + public HikariMetadataProvider(HikariDataSource dataSource) { + super(dataSource); + this.hikariDataSourcePoolMetadata = new HikariPoolMetadataAccessor(getDataSource()); + } + + @Override + public Integer getActive() { + return hikariDataSourcePoolMetadata.getActive(); + } + + @Override + public Integer getMax() { + return hikariDataSourcePoolMetadata.getMax(); + } + + @Override + public Integer getMin() { + return hikariDataSourcePoolMetadata.getMin(); + } + + @Override + public String getValidationQuery() { + return ""; + } + + @Override + public Boolean getDefaultAutoCommit() { + return hikariDataSourcePoolMetadata.getDefaultAutoCommit(); + } + + @Override + public Integer getIdle() { + return hikariDataSourcePoolMetadata.getIdle(); + } +} diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/HikariPoolMetadataAccessor.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/HikariPoolMetadataAccessor.java new file mode 100644 index 000000000000..40039148a65e --- /dev/null +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/HikariPoolMetadataAccessor.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2004-2022, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.monitoring.metrics.jdbc; + +import com.zaxxer.hikari.HikariDataSource; +import com.zaxxer.hikari.pool.HikariPool; +import org.springframework.beans.DirectFieldAccessor; + +/** + * @author Morten Svanæs + */ +public class HikariPoolMetadataAccessor extends AbstractPoolMetadata { + + public HikariPoolMetadataAccessor(HikariDataSource dataSource) { + super(dataSource); + } + + @Override + public Integer getActive() { + try { + return getHikariPool().getActiveConnections(); + } catch (Exception ex) { + return null; + } + } + + @Override + public Integer getIdle() { + try { + return getHikariPool().getIdleConnections(); + } catch (Exception ex) { + return null; + } + } + + private HikariPool getHikariPool() { + return (HikariPool) new DirectFieldAccessor(getDataSource()).getPropertyValue("pool"); + } + + @Override + public Integer getMax() { + return getDataSource().getMaximumPoolSize(); + } + + @Override + public Integer getMin() { + return getDataSource().getMinimumIdle(); + } + + @Override + public String getValidationQuery() { + return getDataSource().getConnectionTestQuery(); + } + + @Override + public Boolean getDefaultAutoCommit() { + return getDataSource().isAutoCommit(); + } +} diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetadata.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetadata.java similarity index 98% rename from dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetadata.java rename to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetadata.java index f429d819895e..6cd51ba5f28c 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetadata.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetadata.java @@ -36,7 +36,7 @@ * @author Stephane Nicoll * @since 2.0.0 */ -public interface DataSourcePoolMetadata { +public interface PoolMetadata { /** * Return the usage of the pool as value between 0 and 1 (or -1 if the pool is not limited). diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetadataProvider.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetadataProvider.java similarity index 85% rename from dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetadataProvider.java rename to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetadataProvider.java index 221e014571fb..0affbd065749 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetadataProvider.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetadataProvider.java @@ -33,13 +33,13 @@ * @author Luciano Fiandesio */ @FunctionalInterface -public interface DataSourcePoolMetadataProvider { +public interface PoolMetadataProvider { /** - * Return the {@link DataSourcePoolMetadata} instance able to manage the specified {@link - * DataSource} or {@code null} if the given data source could not be handled. + * Return the {@link PoolMetadata} instance able to manage the specified {@link DataSource} or + * {@code null} if the given data source could not be handled. * * @param dataSource the data source. * @return the data source pool metadata. */ - DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource); + PoolMetadata getDataSourcePoolMetadata(DataSource dataSource); } diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetadataProviders.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetadataProviders.java similarity index 72% rename from dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetadataProviders.java rename to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetadataProviders.java index e284f9a440dc..cea99e86252d 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetadataProviders.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetadataProviders.java @@ -34,31 +34,29 @@ import javax.sql.DataSource; /** - * A {@link DataSourcePoolMetadataProvider} implementation that returns the first {@link - * DataSourcePoolMetadata} that is found by one of its delegate. + * A {@link PoolMetadataProvider} implementation that returns the first {@link PoolMetadata} that is + * found by one of its delegate. * * @author Stephane Nicoll * @since 1.2.0 */ -public class DataSourcePoolMetadataProviders implements DataSourcePoolMetadataProvider { +public class PoolMetadataProviders implements PoolMetadataProvider { - private final List providers; + private final List providers; /** - * Create a {@link DataSourcePoolMetadataProviders} instance with an initial collection of - * delegates to use. + * Create a {@link PoolMetadataProviders} instance with an initial collection of delegates to use. * * @param providers the data source pool metadata providers */ - public DataSourcePoolMetadataProviders( - Collection providers) { + public PoolMetadataProviders(Collection providers) { this.providers = (providers == null ? Collections.emptyList() : new ArrayList<>(providers)); } @Override - public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) { - for (DataSourcePoolMetadataProvider provider : this.providers) { - DataSourcePoolMetadata metadata = provider.getDataSourcePoolMetadata(dataSource); + public PoolMetadata getDataSourcePoolMetadata(DataSource dataSource) { + for (PoolMetadataProvider provider : this.providers) { + PoolMetadata metadata = provider.getDataSourcePoolMetadata(dataSource); if (metadata != null) { return metadata; } diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetrics.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetrics.java similarity index 67% rename from dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetrics.java rename to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetrics.java index 1d49677a361d..b53791432f5b 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/DataSourcePoolMetrics.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/monitoring/metrics/jdbc/PoolMetrics.java @@ -41,28 +41,24 @@ /** * @author Jon Schneider */ -public class DataSourcePoolMetrics implements MeterBinder { +public class PoolMetrics implements MeterBinder { private final DataSource dataSource; private final CachingDataSourcePoolMetadataProvider metadataProvider; private final Iterable tags; - public DataSourcePoolMetrics( + public PoolMetrics( DataSource dataSource, - Collection metadataProviders, + Collection metadataProviders, String dataSourceName, Iterable tags) { - this( - dataSource, - new CompositeDataSourcePoolMetadataProvider(metadataProviders), - dataSourceName, - tags); + this(dataSource, new CompositePoolMetadataProvider(metadataProviders), dataSourceName, tags); } - public DataSourcePoolMetrics( + public PoolMetrics( DataSource dataSource, - DataSourcePoolMetadataProvider metadataProvider, + PoolMetadataProvider metadataProvider, String name, Iterable tags) { Assert.notNull(dataSource, "DataSource must not be null"); @@ -75,15 +71,15 @@ public DataSourcePoolMetrics( @Override public void bindTo(MeterRegistry registry) { if (this.metadataProvider.getDataSourcePoolMetadata(this.dataSource) != null) { - bindPoolMetadata(registry, "active", DataSourcePoolMetadata::getActive); - bindPoolMetadata(registry, "idle", DataSourcePoolMetadata::getIdle); - bindPoolMetadata(registry, "max", DataSourcePoolMetadata::getMax); - bindPoolMetadata(registry, "min", DataSourcePoolMetadata::getMin); + bindPoolMetadata(registry, "active", PoolMetadata::getActive); + bindPoolMetadata(registry, "idle", PoolMetadata::getIdle); + bindPoolMetadata(registry, "max", PoolMetadata::getMax); + bindPoolMetadata(registry, "min", PoolMetadata::getMin); } } private void bindPoolMetadata( - MeterRegistry registry, String metricName, Function function) { + MeterRegistry registry, String metricName, Function function) { bindDataSource(registry, metricName, this.metadataProvider.getValueFunction(function)); } @@ -94,35 +90,28 @@ private void bindDataSource( "jdbc.connections." + metricName, this.tags, this.dataSource, - (m) -> function.apply(m).doubleValue()); + m -> function.apply(m).doubleValue()); } } - private static class CachingDataSourcePoolMetadataProvider - implements DataSourcePoolMetadataProvider { + private static class CachingDataSourcePoolMetadataProvider implements PoolMetadataProvider { - private static final Map cache = - new ConcurrentReferenceHashMap<>(); + private static final Map cache = new ConcurrentReferenceHashMap<>(); - private final DataSourcePoolMetadataProvider metadataProvider; + private final PoolMetadataProvider metadataProvider; - CachingDataSourcePoolMetadataProvider(DataSourcePoolMetadataProvider metadataProvider) { + CachingDataSourcePoolMetadataProvider(PoolMetadataProvider metadataProvider) { this.metadataProvider = metadataProvider; } Function getValueFunction( - Function function) { - return (dataSource) -> function.apply(getDataSourcePoolMetadata(dataSource)); + Function function) { + return dS -> function.apply(getDataSourcePoolMetadata(dS)); } @Override - public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) { - DataSourcePoolMetadata metadata = cache.get(dataSource); - if (metadata == null) { - metadata = this.metadataProvider.getDataSourcePoolMetadata(dataSource); - cache.put(dataSource, metadata); - } - return metadata; + public PoolMetadata getDataSourcePoolMetadata(DataSource dataSource) { + return cache.computeIfAbsent(dataSource, this.metadataProvider::getDataSourcePoolMetadata); } } }