|
|
|
@ -28,11 +28,11 @@ import com.zaxxer.hikari.HikariConfigMXBean;
|
|
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
|
|
|
import com.zaxxer.hikari.metrics.micrometer.MicrometerMetricsTrackerFactory;
|
|
|
|
|
import io.micrometer.core.instrument.MeterRegistry;
|
|
|
|
|
import io.micrometer.core.instrument.binder.MeterBinder;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.ObjectProvider;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.actuate.metrics.jdbc.DataSourcePoolMetrics;
|
|
|
|
@ -43,6 +43,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
|
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.jdbc.DataSourceUnwrapper;
|
|
|
|
|
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.core.log.LogMessage;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
@ -66,13 +67,30 @@ public class DataSourcePoolMetricsAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
private static final String DATASOURCE_SUFFIX = "dataSource";
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
void bindDataSourcesToRegistry(Map<String, DataSource> dataSources, MeterRegistry registry,
|
|
|
|
|
@Bean
|
|
|
|
|
DataSourcePoolMetadataMeterBinder dataSourcePoolMetadataMeterBinder(Map<String, DataSource> dataSources,
|
|
|
|
|
ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders) {
|
|
|
|
|
List<DataSourcePoolMetadataProvider> metadataProvidersList = metadataProviders.stream()
|
|
|
|
|
return new DataSourcePoolMetadataMeterBinder(dataSources, metadataProviders);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class DataSourcePoolMetadataMeterBinder implements MeterBinder {
|
|
|
|
|
|
|
|
|
|
private final Map<String, DataSource> dataSources;
|
|
|
|
|
|
|
|
|
|
private final ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders;
|
|
|
|
|
|
|
|
|
|
DataSourcePoolMetadataMeterBinder(Map<String, DataSource> dataSources,
|
|
|
|
|
ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders) {
|
|
|
|
|
this.dataSources = dataSources;
|
|
|
|
|
this.metadataProviders = metadataProviders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void bindTo(MeterRegistry registry) {
|
|
|
|
|
List<DataSourcePoolMetadataProvider> metadataProvidersList = this.metadataProviders.stream()
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
dataSources.forEach(
|
|
|
|
|
(name, dataSource) -> bindDataSourceToRegistry(name, dataSource, metadataProvidersList, registry));
|
|
|
|
|
this.dataSources.forEach((name, dataSource) -> bindDataSourceToRegistry(name, dataSource,
|
|
|
|
|
metadataProvidersList, registry));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void bindDataSourceToRegistry(String beanName, DataSource dataSource,
|
|
|
|
@ -97,33 +115,42 @@ public class DataSourcePoolMetricsAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
|
@ConditionalOnClass(HikariDataSource.class)
|
|
|
|
|
static class HikariDataSourceMetricsConfiguration {
|
|
|
|
|
|
|
|
|
|
private static final Log logger = LogFactory.getLog(HikariDataSourceMetricsConfiguration.class);
|
|
|
|
|
@Bean
|
|
|
|
|
HikariDataSourceMeterBinder hikariDataSourceMeterBinder(ObjectProvider<DataSource> dataSources) {
|
|
|
|
|
return new HikariDataSourceMeterBinder(dataSources);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class HikariDataSourceMeterBinder implements MeterBinder {
|
|
|
|
|
|
|
|
|
|
private final MeterRegistry registry;
|
|
|
|
|
private static final Log logger = LogFactory.getLog(HikariDataSourceMeterBinder.class);
|
|
|
|
|
|
|
|
|
|
HikariDataSourceMetricsConfiguration(MeterRegistry registry) {
|
|
|
|
|
this.registry = registry;
|
|
|
|
|
private final ObjectProvider<DataSource> dataSources;
|
|
|
|
|
|
|
|
|
|
HikariDataSourceMeterBinder(ObjectProvider<DataSource> dataSources) {
|
|
|
|
|
this.dataSources = dataSources;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
void bindMetricsRegistryToHikariDataSources(Collection<DataSource> dataSources) {
|
|
|
|
|
for (DataSource dataSource : dataSources) {
|
|
|
|
|
@Override
|
|
|
|
|
public void bindTo(MeterRegistry registry) {
|
|
|
|
|
for (DataSource dataSource : this.dataSources) {
|
|
|
|
|
HikariDataSource hikariDataSource = DataSourceUnwrapper.unwrap(dataSource, HikariConfigMXBean.class,
|
|
|
|
|
HikariDataSource.class);
|
|
|
|
|
if (hikariDataSource != null) {
|
|
|
|
|
bindMetricsRegistryToHikariDataSource(hikariDataSource);
|
|
|
|
|
bindMetricsRegistryToHikariDataSource(hikariDataSource, registry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void bindMetricsRegistryToHikariDataSource(HikariDataSource hikari) {
|
|
|
|
|
private void bindMetricsRegistryToHikariDataSource(HikariDataSource hikari, MeterRegistry registry) {
|
|
|
|
|
if (hikari.getMetricRegistry() == null && hikari.getMetricsTrackerFactory() == null) {
|
|
|
|
|
try {
|
|
|
|
|
hikari.setMetricsTrackerFactory(new MicrometerMetricsTrackerFactory(this.registry));
|
|
|
|
|
hikari.setMetricsTrackerFactory(new MicrometerMetricsTrackerFactory(registry));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
logger.warn(LogMessage.format("Failed to bind Hikari metrics: %s", ex.getMessage()));
|
|
|
|
@ -133,4 +160,6 @@ public class DataSourcePoolMetricsAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|