Stop configuring metrics if hibernate is not available

Closes gh-15317
pull/15325/head
Stephane Nicoll 6 years ago
parent 5f145ac482
commit 476fe6ed17

@ -23,6 +23,7 @@ import javax.persistence.EntityManagerFactory;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jpa.HibernateMetrics; import io.micrometer.core.instrument.binder.jpa.HibernateMetrics;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
@ -46,7 +47,8 @@ import org.springframework.util.StringUtils;
@Configuration @Configuration
@AutoConfigureAfter({ MetricsAutoConfiguration.class, HibernateJpaAutoConfiguration.class, @AutoConfigureAfter({ MetricsAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class }) SimpleMetricsExportAutoConfiguration.class })
@ConditionalOnClass({ EntityManagerFactory.class, MeterRegistry.class }) @ConditionalOnClass({ EntityManagerFactory.class, SessionFactory.class,
MeterRegistry.class })
@ConditionalOnBean({ EntityManagerFactory.class, MeterRegistry.class }) @ConditionalOnBean({ EntityManagerFactory.class, MeterRegistry.class })
public class HibernateMetricsAutoConfiguration { public class HibernateMetricsAutoConfiguration {

@ -37,6 +37,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -53,6 +54,7 @@ import static org.mockito.Mockito.mock;
* Tests for {@link HibernateMetricsAutoConfiguration}. * Tests for {@link HibernateMetricsAutoConfiguration}.
* *
* @author Rui Figueira * @author Rui Figueira
* @author Stephane Nicoll
*/ */
public class HibernateMetricsAutoConfigurationTests { public class HibernateMetricsAutoConfigurationTests {
@ -137,6 +139,19 @@ public class HibernateMetricsAutoConfigurationTests {
}); });
} }
@Test
public void entityManagerFactoryInstrumentationIsDisabledIfHibernateIsNotAvailable() {
this.contextRunner.withClassLoader(new FilteredClassLoader(SessionFactory.class))
.withUserConfiguration(
NonHibernateEntityManagerFactoryConfiguration.class)
.run((context) -> {
assertThat(context)
.doesNotHaveBean(HibernateMetricsAutoConfiguration.class);
MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("hibernate.statements").meter()).isNull();
});
}
@Configuration @Configuration
static class BaseConfiguration { static class BaseConfiguration {

Loading…
Cancel
Save