Browse Source
This commits move the Hikari metrics registration to a BeanPostProcessor as the Hikari datasource cannot be modified once its configuration has been sealed (usually happens when `getConnection` is invoked on the pool). Closes gh-12129pull/12135/merge
3 changed files with 81 additions and 62 deletions
@ -1,60 +0,0 @@
@@ -1,60 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.jdbc; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import com.zaxxer.hikari.HikariDataSource; |
||||
import com.zaxxer.hikari.metrics.micrometer.MicrometerMetricsTrackerFactory; |
||||
import io.micrometer.core.instrument.MeterRegistry; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
/** |
||||
* Specific configuration for {@link HikariDataSource} metrics. |
||||
* |
||||
* @author Tommy Ludwig |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
@Configuration |
||||
@ConditionalOnClass(HikariDataSource.class) |
||||
class HikariDataSourceMetricsConfiguration { |
||||
|
||||
private final MeterRegistry registry; |
||||
|
||||
HikariDataSourceMetricsConfiguration(MeterRegistry registry) { |
||||
this.registry = registry; |
||||
} |
||||
|
||||
@Autowired |
||||
public void bindMetricsRegistryToHikariDataSources( |
||||
Collection<DataSource> dataSources) { |
||||
dataSources.stream().filter(HikariDataSource.class::isInstance) |
||||
.map(HikariDataSource.class::cast) |
||||
.forEach(this::bindMetricsRegistryToHikariDataSource); |
||||
} |
||||
|
||||
private void bindMetricsRegistryToHikariDataSource(HikariDataSource hikari) { |
||||
hikari.setMetricsTrackerFactory( |
||||
new MicrometerMetricsTrackerFactory(this.registry)); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,65 @@
@@ -0,0 +1,65 @@
|
||||
/* |
||||
* Copyright 2012-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.jdbc; |
||||
|
||||
import com.zaxxer.hikari.HikariDataSource; |
||||
import com.zaxxer.hikari.metrics.micrometer.MicrometerMetricsTrackerFactory; |
||||
import io.micrometer.core.instrument.MeterRegistry; |
||||
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor; |
||||
import org.springframework.context.ApplicationContext; |
||||
|
||||
/** |
||||
* {@link BeanPostProcessor} that configures Hikari metrics. Such arrangement is necessary |
||||
* because a {@link HikariDataSource} instance cannot be modified once its configuration |
||||
* has completed. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
class HikariDataSourceMetricsPostProcessor implements BeanPostProcessor { |
||||
|
||||
private final ApplicationContext context; |
||||
|
||||
private volatile MeterRegistry meterRegistry; |
||||
|
||||
HikariDataSourceMetricsPostProcessor(ApplicationContext context) { |
||||
this.context = context; |
||||
} |
||||
|
||||
@Override |
||||
public Object postProcessAfterInitialization(Object bean, String beanName) { |
||||
if (bean instanceof HikariDataSource) { |
||||
bindMetricsRegistryToHikariDataSource(getMeterRegistry(), |
||||
(HikariDataSource) bean); |
||||
} |
||||
return bean; |
||||
} |
||||
|
||||
private void bindMetricsRegistryToHikariDataSource(MeterRegistry registry, |
||||
HikariDataSource dataSource) { |
||||
dataSource.setMetricsTrackerFactory( |
||||
new MicrometerMetricsTrackerFactory(registry)); |
||||
} |
||||
|
||||
private MeterRegistry getMeterRegistry() { |
||||
if (this.meterRegistry == null) { |
||||
this.meterRegistry = this.context.getBean(MeterRegistry.class); |
||||
} |
||||
return this.meterRegistry; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue